SpringMVC集成ActiveMQ案例实战

  1. 导入jar包activemq-all-5.9.1.jar
  2. 在网上下载apache-activemq-5.10.0服务端到D:\apache-activemq-5.10.0下面。
  3. 在本地的系统中配置activemq的环境变量
  1. 在系统变量中添加:变量名:ACTIVEMQ_HOME;变量值:D:\apache-activemq-5.10.0\apache-activemq-5.10.0
  2. 在系统变量中添加:变量名:PATH;变量值: ;%ACTIVEMQ_HOME%\bin;
  3. 此时activemq就可以加载JAVA_HOME的D:\java1.7
  1. 在CMD命令中启动activemq
  1. activemq start
  2. started;  ActiveMQ WebConsole available at http://127.0.0.1:8161/
  1. 编写发送数据包类的步骤如下:1. 新创建一个连接activemq服务的工厂类ActiveMQConnectionFactory(或者在配置文件ActiveMQ.xml中加载这个bean)2.新建Spring Caching连接工厂类CachingConnectionFactory,依赖注入ActiveMQConnectionFactory类;3.

新建JmsTemplate类,调用org.springframework.jms.core.JmsTemplate来设置发送方式是P2P的队列模式还是订阅分发模式的消息主题,还是持久化方式的sql或者nosql数据库存储方式;4设置发送队列的名称queueName=QueueSport;5.调用jmsTemplate.send发送数据到queueName中

public class ObsDataMsgPublisher implements MessagePublisher {

 

       private static ObsDataMsgPublisher _instance = null;

       private static JmsTemplate jmsTemplate;

       private ObsDataMsgPublisher() {

       };

       public static synchronized ObsDataMsgPublisher getInstance() {

              if (_instance == null) {

                     _instance = new ObsDataMsgPublisher();

                     ActiveMQConnectionFactory mqFactory = new ActiveMQConnectionFactory();

                     mqFactory.setBrokerURL(PropertiesReader.getProp("jms.url"));

                     CachingConnectionFactory cachFactory = new CachingConnectionFactory(

                                   mqFactory);

                     cachFactory.setSessionCacheSize(Integer.parseInt(PropertiesReader

                                   .getProp("jms.cachSessionNum")));

                     jmsTemplate = new JmsTemplate(cachFactory);

                     jmsTemplate.setPubSubDomain(false);// p2p方式

                     jmsTemplate.setDeliveryMode(DeliveryMode.PERSISTENT);// 采用持久化方式

              }

              return _instance;

       }

       @Override

       public synchronized boolean sendByQuene(final String msg,

                     final String queueName) {

              boolean ret = false;

              try {

 

                     log.info(this.getClass().getSimpleName() + "--准备发送JMS消息,queueName:"

                                   + queueName);

                     log.info(this.getClass().getSimpleName() + "--msg :" + msg);

                     jmsTemplate.setDefaultDestinationName(queueName);

                     jmsTemplate.send(new MessageCreator() {

                            @Override

                            public Message createMessage(Session session)

                                          throws JMSException {

                                   try {

                                          return session.createTextMessage(URLEncoder.encode(msg,

                                                        "utf-8"));

                                   } catch (Exception e) {

                                          e.printStackTrace();

                                   }

                                   return null;

                            }

                     });

                     ret = true;

                     log.info(this.getClass().getSimpleName() + "--JMS消息发送成功");

              } catch (JmsException e) {

                     e.printStackTrace();

              }

              return ret;

       }

 

}

  1. 测试验证发送数据的结果

SpringMVC集成ActiveMQ案例实战

 

SpringMVC集成ActiveMQ案例实战

SpringMVC集成ActiveMQ案例实战

发送数据的具体内容如下,具体数字已经被转换为十六进制来加密了:

%7B%22appType%22%3A%22WSYD%22%2C%22dataType%22%3A%22stepCount%22%2C%22collectDate%22%3A%222018-06-23+11%3A22%3A50%22%2C%22phone%22%3A%22p82%22%2C%22dataValue%22%3A%5B%7B%22stepSum%22%3A%2252683000%22%7D%2C%7B%22calSum%22%3A%2252683000%22%7D%2C%7B%22distanceSum%22%3A%22263415%22%7D%2C%7B%22yxbsSum%22%3A%220%22%7D%2C%7B%22weight%22%3A%2270%22%7D%2C%7B%22stride%22%3A%2270%22%7D%2C%7B%22degreeOne%22%3A%225084%22%7D%2C%7B%22degreeTwo%22%3A%225084%22%7D%2C%7B%22degreeThree%22%3A%225084%22%7D%2C%7B%22degreeFour%22%3A%225084%22%7D%2C%7B%22uploadType%22%3A%221%22%7D%2C%7B%22measureTime%22%3A%222018-06-23+11%3A22%3A50%22%7D%5D%7D