ITeye博客

ActiveMQ+Spring2.5(转)

rainyear · 2012-02-14 17:59 · 框架开发 · 非技术 · Markdown

ActiveMQ+Spring2.5

ActiveMQJMSmavenSpringBean.

项目环境:

JDK1.5

ActiveMQ5.2

POM文件

Xml代码
1.
2.
3. 4.0.0
4. ActiveMQ-Spring
5. ActiveMQ-Spring
6. war
7.
8. 0.0.1-SNAPSHOT
9.
10.
11. ${basedir}/src
12. ${basedir}/WebRoot/WEB-INF/classes
13.
14.
15. ${basedir}/src
16.
17. **/*.java
18.
19.
20.
21.
22.
23. maven-war-plugin
24.
25. ${basedir}/WebRoot
26. ${basedir}/WebRoot
27.
28.
29.
30. maven-compiler-plugin
31.
32. 1.5
33. 1.5
34.
35.
36.
37.
38.
39.
40. org.apache.openejb
41. javaee-api
42. 5.0-1
43. provided
44.
45.
46. javax.faces
47. jsf-api
48. 1.2_04
49. provided
50.
51.
52. javax.servlet
53. jstl
54. 1.2
55. provided
56.
57.
58. javax.servlet.jsp
59. jsp-api
60. 2.1
61. provided
62.
63.
64. javax.faces
65. jsf-impl
66. 1.2_04
67. provided
68.
69.
70. org.apache.activemq
71. activemq-all
72. 5.2.0
73.
74.
75. org.springframework
76. spring-core
77. 2.5.6
78.
79.
80. org.springframework
81. spring-jms
82. 2.5.6
83.
84.
85.

Java代码
1.package com.jeff.mq;
2.
3.import javax.jms.Destination;
4.import javax.jms.JMSException;
5.import javax.jms.TextMessage;
6.
7.import org.springframework.context.ApplicationContext;
8.import org.springframework.context.support.ClassPathXmlApplicationContext;
9.import org.springframework.jms.core.JmsTemplate;
10.
11./** 12.* 消息接收者 13.* 14.* @author jeff 15.*/
16.public class MyReceiver {
17. public static void main(String args) throws JMSException {
18. ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
19. JmsTemplate template = (JmsTemplate) ctx.getBean("jmsTemplate");
20. Destination destination = (Destination) ctx.getBean("destination");
21. while (true) {
22. TextMessage txtmsg = (TextMessage) template.receive(destination);
23. if (null != txtmsg)
24. System.out.println("收到消息内容为: " + txtmsg.getText());
25. else
26. break;
27. }
28. }
29.}

Java代码
1.package com.jeff.mq;
2.
3.import javax.jms.Destination;
4.import javax.jms.JMSException;
5.import javax.jms.Message;
6.import javax.jms.Session;
7.
8.import org.springframework.context.ApplicationContext;
9.import org.springframework.context.support.ClassPathXmlApplicationContext;
10.import org.springframework.jms.core.JmsTemplate;
11.import org.springframework.jms.core.MessageCreator;
12.
13./** 14.* 消息发送者 15.* 16.* @author jeff 17.*/
18.public class MySender {
19. public static void main(String args) {
20. ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
21. JmsTemplate template = (JmsTemplate) ctx.getBean("jmsTemplate");
22. Destination destination = (Destination) ctx.getBean("destination");
23.
24. template.send(destination, new MessageCreator() {
25. public Message createMessage(Session session) throws JMSException {
26. return session.createTextMessage("发送消息:Hello ActiveMQ Text Message!");
27. }
28. });
29. System.out.println("成功发送了一条JMS消息");
30. }
31.}

Spring配置文件

Xml代码
1.
2.<beans
3. xmlns="http://www.springframework.org/schema/beans"
4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.

运行发送端三次:

成功发送了一条JMS消息

Process finished with exit code 0

然后再运行接收端一次:

收到消息内容为: 发送消息:Hello ActiveMQ Text Message! 收到消息内容为: 发送消息:Hello ActiveMQ Text Message! 收到消息内容为: 发送消息:Hello ActiveMQ Text Message!

继续测试发现,接收端接收一条消息后不退出程序,而是继续等待,一旦有消息发送过来,就获取到,然后输出!

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。