博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ActiveMQ JMS 项目 基于 Maven 搭建 部署
阅读量:7025 次
发布时间:2019-06-28

本文共 8424 字,大约阅读时间需要 28 分钟。

 

JAVA版本:

IntellJ IDEA 版本:

IntelliJ IDEA 2017.2

Build #IU-172.3317.76, built on July 15, 2017
Licensed to Administrator

JRE: 1.8.0_131-release-915-b5 amd64

JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Windows 7 6.1

 

一、新建Maven工程

1.选择File => New => Project...

2.或者执行maven 命令行创建工程。

D:\cd D:\JavaSourceCode\JavaSamplesmvn archetype:generate -DgroupId=com.phpdragon -DartifactId=jms-activeme-mq -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

3.或者手动创建如下目录结构:

4.如果是手动创建目录,需设置目录属性让 IDEA 识别为源码包路径

 

二、添加JAR依赖

spring-jms:

spring-test:

activemq-pool:

fastjson:

junit:

testng:

pom.xml配置如下

4.0.0
com.phpdragon
jms-activemq-demo
1.0-SNAPSHOT
jar
jms-activemq-demo
http://maven.apache.org
phpdragon
phpdragon
phpdragon@qq.com
UTF-8
1.2.35
5.15.0
4.3.10.RELEASE
4.12
6.11
com.alibaba
fastjson
${fastjson.vesrion}
org.apache.activemq
activemq-pool
${activemq-pool.version}
org.springframework
spring-jms
${spring.version}
org.springframework
spring-test
${spring.version}
test
junit
junit
${junit.version}
test
org.testng
testng
${testng.version}
test

 

三、编写生产者、消费者

1.添加生产者者MqProducer.java

import com.sun.nio.sctp.MessageInfo;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jms.core.JmsTemplate;import org.springframework.stereotype.Component;@Componentpublic class MqProducer {    @Autowired    private JmsTemplate jmsTemplate;    public void sendMsg(MessageInfo info) {        try {            jmsTemplate.convertAndSend(info);        } catch (Exception e) {            e.printStackTrace();        }    }}

 

2.创建jms消息转换器MqMessageConverter.java

import javax.jms.JMSException;import javax.jms.Message;import javax.jms.Session;import com.phpdragon.jms.pojo.MessagePojo;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.jms.support.converter.MessageConversionException;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import org.springframework.stereotype.Component;@Component("messageConverter")public class MqMessageConverter implements org.springframework.jms.support.converter.MessageConverter {    private static final Logger LOGGER = LoggerFactory.getLogger(MqMessageConverter.class);    public Object fromMessage(Message message) throws JMSException, MessageConversionException {        LOGGER.info("从mq获得message, message内容:" + message);        JSONObject jsonRoot = (JSONObject) JSON.parse(message.getStringProperty("obj"));        JSONObject jsonObj = JSONObject.parseObject(jsonRoot.getString("value"));        MessagePojo info = JSON.toJavaObject(jsonObj.getJSONObject("body"), MessagePojo.class);        return info;    }    public Message toMessage(Object obj, Session session) throws JMSException, MessageConversionException {        LOGGER.info("往mq插入message, message内容:" + obj);        JSONObject jsonRoot = new JSONObject();        JSONObject jsonObj = new JSONObject();        jsonObj.put("body", obj);        jsonRoot.put("value", jsonObj.toJSONString());        Message message = session.createMapMessage();        message.setObjectProperty("obj", jsonRoot.toJSONString());        return message;    }}

 

3.添加spring配置文件, spring-context.xml、app.properties

1)app.properties:

application.main=com.phpdragon.jms.Appapplication.name=jms_activemq_demo_serverapplication.owner=phpdragonmq.queue.name=COM.PHPDRAGON.JMS.DEMO.QUEUEmq.brokerURL=tcp://127.0.0.1:61616

2)spring-context.xml:

4.编写启动程序App.java

import com.phpdragon.jms.activemq.MqProducer;import com.phpdragon.jms.pojo.MessagePojo;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.stereotype.Component;import java.io.IOException;@Componentpublic class App {    public static final String DEFAULT_CONFIG_LOCATION = "/spring-context.xml";    @Autowired    private MqProducer mqProducer;    /**     * 程序入口     *     * @param args     * @throws IOException     */    public static void main(String[] args) throws IOException {        ApplicationContext context = new ClassPathXmlApplicationContext(DEFAULT_CONFIG_LOCATION);        App app = (App) context.getBean("app");        app.run(args);    }    public void run(String[] args) {        MessagePojo msg = new MessagePojo();        msg.setTitle("Test");        msg.setContent("TestContent");        mqProducer.sendMsg(msg);        System.exit(0);    }}

5.到此,一个activeMQ发送程序就写好了,选中App.java的main函数体,鼠标右键点击 debug 运行,执行效果如下:

 

6.登录activeMQ管理后台,http://127.0.0.1:8161/admin/queues.jsp, 默认帐号: admin 密码: admin 

 

 

7.添加消费者

1)创建MqConsumer.java

import com.phpdragon.jms.pojo.MessagePojo;import org.springframework.stereotype.Component;import javax.jms.JMSException;@Componentpublic class MqConsumer{    public void handleMessage(MessagePojo msg) throws JMSException {        System.out.println("handleMessage:" + msg.toString());    }}

 

2)添加activeMQ 监听配置

3) 右键debug运行,效果如下:

4) 查看消费情况

 

 

 

四、集成logback

是否觉得debug日志太简单?那我们引入logback支持。实现丰富日志输出、日志back等

 

1.添加logback-classic Maven依赖

ch.qos.logback
logback-classic
1.2.3

 

2.在resources资源目录中添加logback.xml文件

${LOG_BACK_DIR}/debug.log
${LOG_BACK_DIR}/debug_%d{yyyyMMddHH}.%i.log
256MB
48
%d{yyyy-MM-dd HH:mm:ss.SSS}|%X{threadId}|%level|%C|%M|%L|%.-512msg%n
UTF-8
TRACE
 

 

3.右键debug运行,效果如下:

 

五、集成assembly

 

六、单元测试与自动测试

 

七、编译并上传远程仓库

 

八、项目部署

 

源码地址:

有XML配置版本: 

 spring注解版本:

spring-boot版本:

 

转载地址:http://wxoxl.baihongyu.com/

你可能感兴趣的文章