直接上使用步骤
1.官网下载activemq服务端,直接解压,无需安装,
,我用的是windows 64bit环境,直接启动。
2.spring.xml配置
1 <!-- ++++++++++++++++++++++++++++++++++++++++++++消息队列activeMQ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> 2 <!--++++++++++++++++++++++++++++++++++++++++++++++++++++发送者+++++++++++++++++++++++++++++++++++++++++++++++++++++ --> 3 <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 --> 4 <!-- <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> 5 <property name="brokerURL" value="tcp://192.168.1.101:61616" /> 6 </bean> --> 7 8 <!-- Spring用于管理真正的ConnectionFactory --> 9 <!-- <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"> --> 10 <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory --> 11 <!-- <property name="targetConnectionFactory" ref="targetConnectionFactory" /> --> 12 <!-- </bean> --> 13 14 <!-- 使用pool进行链接 --> 15 <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供--> 16 <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> 17 <property name="brokerURL" value="tcp://192.168.1.101:61616"/> 18 <!-- 消息传输监听器 处理网络及服务器异常 --> 19 <property name="transportListener"> 20 <bean class="com.haobai.activeMQ.ActiveMQTransportListener" /> 21 </property> 22 </bean> 23 24 <bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory"> 25 <property name="connectionFactory" ref="targetConnectionFactory"/> 26 <property name="maxConnections" value="10"/> 27 </bean> 28 29 <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"> 30 <property name="targetConnectionFactory" ref="pooledConnectionFactory"/> 31 </bean> 32 33 <!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 --> 34 <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> 35 <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 --> 36 <property name="connectionFactory" ref="connectionFactory" /> 37 </bean> 38 39 <!--这个是队列目的地 --> 40 <bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue"> 41 <constructor-arg> 42 <value>queue</value> 43 </constructor-arg> 44 </bean> 45 46 <!--这个是主题目的地,一对多的--> 47 <bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic"> 48 <constructor-arg value="topic"/> 49 </bean> 50 <!--++++++++++++++++++++++++++++++++++++++++++++++++++++接收者+++++++++++++++++++++++++++++++++++++++++++++++++++++ --> 51 <!-- 消息监听器 --> 52 <bean id="consumerMessageListener" class="com.haobai.activeMQ.ConsumerMessageListener" /> 53 <!-- 消息监听容器 --> 54 <bean id="jmsContainer" 55 class="org.springframework.jms.listener.DefaultMessageListenerContainer"> 56 <property name="connectionFactory" ref="connectionFactory" /> 57 <property name="destination" ref="queueDestination" /> 58 <property name="messageListener" ref="consumerMessageListener" /> 59 </bean>
3.实现消息传输接口TransportListener
1 public class ActiveMQTransportListener implements TransportListener { 2 3 private Logger logger = LoggerFactory.getLogger(this.getClass()); 4 5 /** 6 * 对消息传输命令进行监控 7 * @param o 8 */ 9 public void onCommand(Object o) { 10 logger.info("onCommand -> 消息服务器消息传输正常..."); 11 } 12 13 /** 14 * 监控到异常时触发 15 * @param e 16 */ 17 public void onException(IOException e) { 18 logger.error("onException -> 消息服务器连接错误......", e); 19 } 20 21 /** 22 * 当failover时触发 23 */ 24 public void transportInterupted() { 25 logger.warn("transportInterupted -> 消息服务器连接发生中断..."); 26 } 27 28 /** 29 * 监控到failover恢复后进行触发 30 */ 31 public void transportResumed() { 32 logger.info("transportResumed -> 消息服务器连接已恢复..."); 33 } 34 35 }
4.生产者的配置,用于发消息
4.1JmsTestController 由于发送对象需要实例化,所以这里直接转成String了
@Controller public class JmsTestController { @Autowired private ProducerService producerService; @Autowired @Qualifier("queueDestination") private Destination destination; @RequestMapping("test") @ResponseBody public String testSend() throws Exception { //向message中放2个user对象 List<UserInfo> list = new LinkedList<UserInfo>(); UserInfo en = new UserInfo(); en.setId(100); en.setUsername("name1"); list.add(en); UserInfo en2 = new UserInfo(); en2.setId(1002); en2.setUsername("name2"); list.add(en2); Map<String,Object> mapEntity = new HashMap<String, Object>(); mapEntity.put("user", list); Map<String,Object> map = new HashMap<String, Object>(); map.put("update", mapEntity); System.out.println("发送方发送内容为:" + map.toString()); //发送更新数据请求 System.out.println(destination); producerService.sendMessage(destination, map.toString()); return "jms exute complete"; } }
4.2 ProducerService
@Service("producerService")
public class ProducerService {
@Autowired private JmsTemplate jmsTemplate;
public void sendMessage(Destination destination, final String message) {
jmsTemplate.send(destination, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
TextMessage textMessage = session.createTextMessage(message);
return textMessage;
}
});
}
}
5.消息接收者,需要实现MessageListener接口
1 public class ConsumerMessageListener implements MessageListener { 2 private Logger logger = LoggerFactory.getLogger(this.getClass()); 3 4 /** 5 * 监听发送到消息队列的文本消息 6 * 7 * @param message 8 */ 9 public void onMessage(Message message) { 10 TextMessage textMessage = (TextMessage) message; 11 try { 12 System.out.println("接收到的消息内容是:" + textMessage.getText()); 13 System.out.println("开始进行解析并调用service执行...."); 14 } catch (JMSException e) { 15 logger.error("消息解析失败", e); 16 } 17 } 18 19 }
到此 ,所有配置完成。
tips:如果想要服务器A给服务器B发消息,则需要注意两点:
1.服务器A发消息和 服务器B接受消息需要在同一个队列。
2.服务器A不能实现MessageListener,或者不能和服务器B监听同一个队列,因为activemq的消息队列,消息只能被一个消费者得到,谁先抢到算谁的。A、B同时监听一个队列,有可能A发的消息直接被A抢了,B啥都没有接收到。
好了,暂时就记这么多,以后遇到问题继续增加