[Java学习] springmvc集成activemq,消息产生

springmvc集成activemq, 采用JMSTemplate进行消息发送,
jmsTemplate.send(activeMQQueue, new MessageCreator(){

@Override
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(message);
}
});
详见代码:https://github.com/leelance/demo/tree/master/demo-springmvc-activemq
  1. public class ProducerService {
  2. Logger log = LogManager.getLogger(getClass());
  3. @Autowired
  4. private JmsTemplate jmsTemplate;
  5. @Autowired
  6. private ActiveMQQueue activeMQQueue;
  7. http://www.ynmxzx.com/tmqm/20160615/3719.html
  8. /**
  9. * 发送Text消息队列
  10. * @param message
  11. */
  12. public void sendTextQueueMessage(final String message) {
  13. jmsTemplate.send(activeMQQueue, new MessageCreator(){
  14. @Override
  15. public Message createMessage(Session session) throws JMSException {
  16. return session.createTextMessage(message);
  17. }http://www.ynmxzx.com/tmqm/20160615/3720.html
  18. });
  19. }
  20. http://www.ynmxzx.com/tmqm/20160615/3723.html
  21. /**
  22. * 发送消息并处理消息返回值
  23. * @param message
  24. * @throws JMSException
  25. */
  26. public String sendTextQueueMessageAndReceive(final String message) throws JMSException {
  27. Message replyMessage = jmsTemplate.sendAndReceive(activeMQQueue, new MessageCreator(){
  28. @Override
  29. public Message createMessage(Session session) throws JMSException {
  30. return session.createTextMessage(message);
  31. }
  32. });
  33. http://www.ynmxzx.com/tmqm/20160615/3721.html
  34. log.info("sendTextAndReceive: {}", JSON.toJSONString(replyMessage));
  35. TextMessage textMessage = (TextMessage)replyMessage;
  36. return textMessage.getText();
  37. }
  38. /**
  39. * 订单入队列
  40. * @param info
  41. */http://www.ynmxzx.com/tmqm/20160615/3722.html
  42. public void sendObjectQueueMessage(final OrderInfo info) {
  43. jmsTemplate.convertAndSend(activeMQQueue, info);
  44. }www.ynmxzx.com
  45. }
复制代码
  1. <!-- ActiveMQConnectionFactory -->
  2. <bean id="activeMQConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
  3. <constructor-arg index="0" value="tcp://127.0.0.1:61616"/>
  4. <property name="trustedPackages">
  5. <list>
  6. <value>com.lance.mq</value>
  7. <value>java.util</value>
  8. </list>
  9. </property>
  10. </bean>
  11. <!-- 创建pooledConnectionFactory连接池, JmsTemlate每次发送消息都重新打开一个Connection -->
  12. <bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory">
  13. <property name="connectionFactory" ref="activeMQConnectionFactory"/>
  14. <property name="maxConnections" value="5"/>
  15. </bean>
  16. <!-- CachingConnectionFactory
  17. <bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
  18. <constructor-arg index="0" ref="activeMQConnectionFactory"/>
  19. </bean>
  20. -->
  21. <!-- jmsTemplate -->
  22. <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
  23. <property name="connectionFactory" ref="pooledConnectionFactory"/>
  24. <property name="messageConverter">
  25. <bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/>
  26. </property>
  27. </bean>
  28. <!-- ActiveMQQueue -->
  29. <bean id="activeMQQueue" class="org.apache.activemq.command.ActiveMQQueue">
  30. <constructor-arg index="0" value="queue"/>
  31. </bean>
  32. <!-- ActiveMQTopic -->
  33. <bean id="activeMQTopic" class="org.apache.activemq.command.ActiveMQTopic">
  34. <constructor-arg index="0" value="topic"/>
  35. </bean>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值