您的位置:首页 > 编程语言 > Java开发

(七)jms activeMQ与spring的集成

2015-11-04 15:21 585 查看
 

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的配置了,绝对可以跑起来,跑起来的代码才是王道,大家一起学习了!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  jms mq