spring整合activemq步骤

来源:互联网 发布:c语言abs 编辑:程序博客网 时间:2024/06/09 16:26

1.引入pom依赖

<dependency>   <groupId>org.apache.activemq</groupId>   <artifactId>activemq-all</artifactId>   <version>5.8.0</version></dependency><dependency>   <groupId>org.apache.activemq</groupId>   <artifactId>activemq-pool</artifactId>   <version>5.8.0</version></dependency>
2,在spring配置文件中配置mq

<!-- 真正可以产生ConnectionConnectionFactory,由对应的 JMS服务厂商提供 --><bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">   <!-- ActiveMQ服务地址 -->       <property name="brokerURL" value="${mq.brokerURL}" />       <property name="userName" value="${mq.userName}"></property>       <property name="password" value="${mq.password}"></property> </bean><!-- ActiveMQ为我们提供了一个PooledConnectionFactory,通过往里面注入一个ActiveMQConnectionFactory    可以用来将ConnectionSessionMessageProducer池化,这样可以大大的减少我们的资源消耗。 要依赖于 activemq-pool--><bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory">   <property name="connectionFactory" ref="targetConnectionFactory" />   <property name="maxConnections" value="50" /></bean><!-- Spring用于管理真正的ConnectionFactoryConnectionFactory --><bean id="connectionFactory"   class="org.springframework.jms.connection.SingleConnectionFactory">   <!-- 目标ConnectionFactory对应真实的可以产生JMS ConnectionConnectionFactory -->   <property name="targetConnectionFactory" ref="pooledConnectionFactory" /></bean><!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 --><!-- 商户通知队列模板 --><bean id="notifyJmsTemplate" class="org.springframework.jms.core.JmsTemplate">   <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->   <property name="connectionFactory" ref="connectionFactory" />   <property name="defaultDestinationName" value="${queueName.notify}"></property></bean>
3,在代码中发送消息

@Autowiredprivate JmsTemplate notifyJmsTemplate;/** * 短信内容模板解释. * @param smsTemplatePath 短信模板路径. * @param paramModel 模板中的参数变量. * @return content  * @throws Exception  */public String mergeSmsTemplate(String smsTemplatePath, Map<String, Object> paramModel) throws Exception {   try {      return VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, smsTemplatePath, "UTF-8", paramModel);   } catch (Exception e) {      LOG.error("==>merge " + smsTemplatePath + " exception:", e);      throw e;   }}/** * 发送短信. * @param phone 手机号. * @param content 短信内容. */public void sendSms(String phone, String content) {   final SmsParam smsParam = new SmsParam(phone, content);        notifyJmsTemplate.send(new MessageCreator() {      public Message createMessage(Session session) throws JMSException {         return session.createTextMessage(NotifyUtil.formatSms(smsParam));      }   });}
4,在代码中接受消息

<!-- 真正可以产生ConnectionConnectionFactory,由对应的 JMS服务厂商提供 --><bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">   <property name="brokerURL" value="${mq.brokerURL}"/>   <property name="userName" value="${mq.userName}"></property>   <property name="password" value="${mq.password}"></property> </bean><bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory">   <property name="connectionFactory" ref="targetConnectionFactory" />   <property name="maxConnections" value="${mq.pool.maxConnections}" /></bean><!-- Spring用于管理真正的ConnectionFactoryConnectionFactory --><bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">   <!-- 目标ConnectionFactory对应真实的可以产生JMS ConnectionConnectionFactory -->   <property name="targetConnectionFactory" ref="pooledConnectionFactory" /></bean><!-- 商户通知队列模板 --><bean id="notifyJmsTemplate" class="org.springframework.jms.core.JmsTemplate">   <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->   <property name="connectionFactory" ref="connectionFactory" />   <!-- 目标队列 -->   <property name="defaultDestinationName" value="${queueName.notify}"></property></bean><!--这个是sessionAwareQueue目的地 --><bean id="sessionAwareQueue" class="org.apache.activemq.command.ActiveMQQueue">   <constructor-arg>      <value>${queueName.notify}</value>   </constructor-arg></bean><!-- 可以获取sessionMessageListener --><bean id="consumerSessionAwareMessageListener" class="wusc.edu.pay.app.notify.message.ConsumerSessionAwareMessageListener"></bean><bean id="sessionAwareListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">   <property name="connectionFactory" ref="connectionFactory" />   <property name="destination" ref="sessionAwareQueue" />   <property name="messageListener" ref="consumerSessionAwareMessageListener" /></bean>
5,在代码中写listener

public class ConsumerSessionAwareMessageListener implements SessionAwareMessageListener<Message> {    private static final Log log = LogFactory.getLog(ConsumerSessionAwareMessageListener.class);    @Autowired    private JmsTemplate notifyJmsTemplate;    @Autowired    private Destination sessionAwareQueue;    @Autowired    private NotifyQueue notifyQueue;    @Autowired    private NotifyFacade notifyFacade;    public ConsumerSessionAwareMessageListener() {    }    public synchronized void onMessage(Message message, Session session) {        try {            ActiveMQTextMessage msg = (ActiveMQTextMessage)message;            final String ms = msg.getText();            log.info("== receive message:" + ms);            NotifyRecord notifyRecord = (NotifyRecord)JSONObject.parseObject(ms, NotifyRecord.class);            if(notifyRecord == null) {                return;            }            notifyRecord.setStatus(Integer.valueOf(NotifyStatusEnum.CREATED.getValue()));            if(notifyRecord.getId() == null) {                while(this.notifyFacade == null) {                    Thread.currentThread();                    Thread.sleep(1000L);                }                try {                    long notifyId = NotifyPersist.saveNotifyRecord(notifyRecord);                    notifyRecord.setId(Long.valueOf(notifyId));                    this.notifyQueue.addElementToList(notifyRecord);                } catch (RpcException var8) {                    this.notifyJmsTemplate.send(this.sessionAwareQueue, new MessageCreator() {                        public Message createMessage(Session session) throws JMSException {                            return session.createTextMessage(ms);                        }                    });                    log.error("RpcException :", var8);                } catch (BizException var9) {                    log.error("BizException :", var9);                } catch (Exception var10) {                    var10.printStackTrace();                    log.error(var10);                }            }        } catch (Exception var11) {            var11.printStackTrace();            log.error(var11);        }    }}
===================================================================================================

0 0
原创粉丝点击