1、首先引入activeMQ和spring的jar包 ,直接上圖
上面的jar包第一個是activeMQ的,還有spring的commos-logging,spring綜合包,裏面包括了spring-jms,另外還要引入slf4j的兩個包 (jar包見管理--文件中的 activeMQ所需jar包 如果不能看的,要麼留下聯繫方式我看到了發給你們,要麼網上找也好找 -.- )
2、spring配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <beans xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context-2.5.xsd"> 10 11 12 <!-- 配置JMS連接工廠 --> 13 14 <bean id="connectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory"> 15 <property name="brokerURL" value="tcp://localhost:61616" /> 16 </bean> 17 18 <!-- 配置JMS模版 --> 19 <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> 20 <property name="connectionFactory" ref="connectionFactory" /> 21 </bean> 22 23 <!-- 發送消息的目的地(一個隊列) --> 24 <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue"> 25 <!-- 設置消息隊列的名字 --> 26 <constructor-arg index="0" value="activeMQQueue" /> 27 </bean> 28 </beans>
2、java代碼 發送者
1 package com.pis.activeMQ.test; 2 3 import org.junit.Test; 4 import org.springframework.context.ApplicationContext; 5 6 import org.springframework.context.support.ClassPathXmlApplicationContext; 7 8 import org.springframework.jms.core.JmsTemplate; 9 10 import org.springframework.jms.core.MessageCreator; 11 12 import javax.jms.Destination; 13 14 import javax.jms.JMSException; 15 16 import javax.jms.Message; 17 18 import javax.jms.Session; 19 20 public class MessageSender implements MessageCreator{ 21 22 //這裏用的是單元測試來寫的,執行時需要引入junit4的jar包,如果嫌麻煩,直接改成main方法也行 23 @Test 24 public void send(){ 25 ApplicationContext ctx = new ClassPathXmlApplicationContext("/spring/spring-jms.xml"); 26 27 JmsTemplate template = (JmsTemplate) ctx.getBean("jmsTemplate"); 28 29 Destination destination = (Destination) ctx.getBean("destination"); 30 31 template.send(destination, this);//此處的this是一個MessageCreator的類型,實質上調用的是MessageCreator的createMessage的方法 32 //也可以用內部類來寫,會更清晰,就不需要實現MessageCreator 33 34 /* 35 template.send(destination, new MessageCreator(){ 36 37 @Override 38 public Message createMessage(Session session) throws JMSException { 39 return session.createTextMessage("sender發送消息!"); 40 } 41 42 });*/ 43 System.out.println("發送JMS消息成功"); 44 } 45 46 @Override 47 public Message createMessage(Session session) throws JMSException { 48 49 return session.createTextMessage("sender發送消息!"); 50 } 51 52 53 54 55 }
3、java代碼 消費者
1 package com.pis.activeMQ.test; 2 3 import org.junit.Test; 4 import org.springframework.context.ApplicationContext; 5 6 import org.springframework.context.support.ClassPathXmlApplicationContext; 7 8 import org.springframework.jms.core.JmsTemplate; 9 10 11 12 import javax.jms.Destination; 13 14 import javax.jms.JMSException; 15 16 import javax.jms.TextMessage; 17 18 public class MessageReceiver { 19 20 21 //這裏用的是單元測試來寫的,執行時需要引入junit4的jar包,如果嫌麻煩,直接改成main方法也行 22 @Test 23 public void Receive() throws JMSException { 24 ApplicationContext ctx = new ClassPathXmlApplicationContext("/spring/spring-jms.xml"); 25 JmsTemplate template = (JmsTemplate) ctx.getBean("jmsTemplate"); 26 Destination destination = (Destination) ctx.getBean("destination"); 27 while (true) { 28 TextMessage msg = (TextMessage) template.receive(destination); 29 if (null != msg) 30 System.out.println("收到消息內容爲: " + msg.getText()); 31 else 32 break; 33 34 } 35 } 36 37 38 39 }
啓動activeMQ服務,連續運行兩次發送者,然後再運行一次接受者,可以從http://localhost:8161/admin/queues.jsp去查看消息隊列的消息發送接受情況,如下圖所示:
運行MessageSender 兩次
查看activeMQ管理界面:可以看到有2條消息
運行MessageReceiver,接受兩條消息
再次查看activeMQ管理界面:可以看到2條消息已經接收
OK! 上面就是最簡單的activeMQ的配置了,絕對可以跑起來,跑起來的代碼纔是王道,大家一起學習了!