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
<!-- 真正可以产生Connection的ConnectionFactory,由对应的 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 可以用来将Connection、Session和MessageProducer池化,这样可以大大的减少我们的资源消耗。 要依赖于 activemq-pool包 --><bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory"> <property name="connectionFactory" ref="targetConnectionFactory" /> <property name="maxConnections" value="50" /></bean><!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory --><bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"> <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory --> <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,在代码中接受消息
<!-- 真正可以产生Connection的ConnectionFactory,由对应的 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用于管理真正的ConnectionFactory的ConnectionFactory --><bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"> <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory --> <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><!-- 可以获取session的MessageListener --><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
- spring整合activemq步骤
- ActiveMQ(四):Spring ActiveMQ 整合
- ActiveMQ和spring整合
- Spring整合ActiveMQ
- SPRING+ACTIVEMQ+TOMCAT整合
- Spring整合activeMQ
- ActiveMQ、Spring整合学习
- activeMq整合spring
- ActiveMQ、Spring整合学习
- spring activemq quartz 整合
- SPRING JMS 整合ACTIVEMQ
- ActiveMQ整合Spring
- 【参考】spring整合activemq
- Spring整合ActiveMQ
- spring+activemq整合学习
- spring 整合 activemq 配置文件
- Spring整合ActiveMQ
- Spring与ActiveMQ整合
- php 设计模式之工厂模式
- Linux文件类型、属性
- 清园 沉没的Atlantis redis.conf配置详细解析
- WebView控件的使用
- android WebView 和 Html 的相互调用
- spring整合activemq步骤
- CentOS7安装MySQL数据库
- Android View事件分发机制
- 脚本实现自动建立swap分区
- Android 平台下使用 i2c-tools
- Neo4j安装
- 算法--快速排序
- 做BLE阶段参考过的博客
- VPN组网不用愁,即插即用功能的路由设备帮你忙