---
title: "spring集成ActiveMQ, 消息提供者"
author: "lihao312"
date: "2016-06-15T13:47:42Z"
tags: ["ActiveMQ", "Java", "Spring", "SpringMVC"]
categories: ["编程语言"]
slug: "spring-activemq"
draft: false
lastmod: "2016-06-15T13:49:30Z"
id: 2305237
url: "https://lihao312.iteye.com/blog/2305237"
---

1.spring集成ActiveMQ，依赖包 \[code="java"] org.springframework spring-jms 4.2.6.RELEASE org.apache.activemq activemq-client 5.13.3 org.apache.activemq activemq-spring 5.13.3 \[/code] 2.配置spring文件,主要配置ConnectionFactory,和ActiveMQDestination，JSMTemplate \[code="xml"] com.lance.mq java.util \[/code] 3.消息发送 \[code="java"] /\** * 发送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); } }); } /\** * 发送消息并处理消息返回值 * @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); } }); log.info("sendTextAndReceive: {}", JSON.toJSONString(replyMessage)); TextMessage textMessage = (TextMessage)replyMessage; return textMessage.getText(); } /\** * 订单入队列 * @param info * @throws JMSException \*/ public String sendObjectQueueMessage(final OrderInfo info) throws JMSException { Message message = jmsTemplate.sendAndReceive(activeMQQueue, new MessageCreator() { @Override public Message createMessage(Session session) throws JMSException { return session.createObjectMessage(info); } }); log.info("sendTextAndReceive: {}", JSON.toJSONString(message)); TextMessage textMessage = (TextMessage)message; return textMessage.getText(); } \[/code] 5.可以模拟多个生产者，在高并发下，多台服务器请求进入消息队列，交给消费者统一生成订单，详见demo地址： \[url]https://github.com/leelance/demo/tree/master/demo-springmvc-activemq\[/url]