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
复制代码
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
- public class ProducerService {
- Logger log = LogManager.getLogger(getClass());
- @Autowired
- private JmsTemplate jmsTemplate;
- @Autowired
- private ActiveMQQueue activeMQQueue;
- http://www.ynmxzx.com/tmqm/20160615/3719.html
- /**
- * 发送Text消息队列
- * @param message
- */
- public void sendTextQueueMessage(final String message) {
- jmsTemplate.send(activeMQQueue, new MessageCreator(){
- @Override
- public Message createMessage(Session session) throws JMSException {
- return session.createTextMessage(message);
- }http://www.ynmxzx.com/tmqm/20160615/3720.html
- });
- }
- http://www.ynmxzx.com/tmqm/20160615/3723.html
- /**
- * 发送消息并处理消息返回值
- * @param message
- * @throws JMSException
- */
- public String sendTextQueueMessageAndReceive(final String message) throws JMSException {
- Message replyMessage = jmsTemplate.sendAndReceive(activeMQQueue, new MessageCreator(){
- @Override
- public Message createMessage(Session session) throws JMSException {
- return session.createTextMessage(message);
- }
- });
- http://www.ynmxzx.com/tmqm/20160615/3721.html
- log.info("sendTextAndReceive: {}", JSON.toJSONString(replyMessage));
- TextMessage textMessage = (TextMessage)replyMessage;
- return textMessage.getText();
- }
- /**
- * 订单入队列
- * @param info
- */http://www.ynmxzx.com/tmqm/20160615/3722.html
- public void sendObjectQueueMessage(final OrderInfo info) {
- jmsTemplate.convertAndSend(activeMQQueue, info);
- }www.ynmxzx.com
- }
- <!-- ActiveMQConnectionFactory -->
- <bean id="activeMQConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
- <constructor-arg index="0" value="tcp://127.0.0.1:61616"/>
- <property name="trustedPackages">
- <list>
- <value>com.lance.mq</value>
- <value>java.util</value>
- </list>
- </property>
- </bean>
- <!-- 创建pooledConnectionFactory连接池, JmsTemlate每次发送消息都重新打开一个Connection -->
- <bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory">
- <property name="connectionFactory" ref="activeMQConnectionFactory"/>
- <property name="maxConnections" value="5"/>
- </bean>
- <!-- CachingConnectionFactory
- <bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
- <constructor-arg index="0" ref="activeMQConnectionFactory"/>
- </bean>
- -->
- <!-- jmsTemplate -->
- <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
- <property name="connectionFactory" ref="pooledConnectionFactory"/>
- <property name="messageConverter">
- <bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/>
- </property>
- </bean>
- <!-- ActiveMQQueue -->
- <bean id="activeMQQueue" class="org.apache.activemq.command.ActiveMQQueue">
- <constructor-arg index="0" value="queue"/>
- </bean>
- <!-- ActiveMQTopic -->
- <bean id="activeMQTopic" class="org.apache.activemq.command.ActiveMQTopic">
- <constructor-arg index="0" value="topic"/>
- </bean>