ActiveMQ+Spring2.5
ActiveMQJMSmavenSpringBean.
项目环境:
JDK1.5
ActiveMQ5.2
POM文件
Xml代码
1.<?xml version="1.0" encoding="UTF-8"?>
2.<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3. <modelVersion>4.0.0</modelVersion>
4. <groupId>ActiveMQ-Spring</groupId>
5. <artifactId>ActiveMQ-Spring</artifactId>
6. <packaging>war</packaging>
7. <name />
8. <version>0.0.1-SNAPSHOT</version>
9. <description />
10. <build>
11. <sourceDirectory>${basedir}/src</sourceDirectory>
12. <outputDirectory>${basedir}/WebRoot/WEB-INF/classes</outputDirectory>
13. <resources>
14. <resource>
15. <directory>${basedir}/src</directory>
16. <excludes>
17. <exclude>**/*.java</exclude>
18. </excludes>
19. </resource>
20. </resources>
21. <plugins>
22. <plugin>
23. <artifactId>maven-war-plugin</artifactId>
24. <configuration>
25. <webappDirectory>${basedir}/WebRoot</webappDirectory>
26. <warSourceDirectory>${basedir}/WebRoot</warSourceDirectory>
27. </configuration>
28. </plugin>
29. <plugin>
30. <artifactId>maven-compiler-plugin</artifactId>
31. <configuration>
32. <source>1.5</source>
33. <target>1.5</target>
34. </configuration>
35. </plugin>
36. </plugins>
37. </build>
38. <dependencies>
39. <dependency>
40. <groupId>org.apache.openejb</groupId>
41. <artifactId>javaee-api</artifactId>
42. <version>5.0-1</version>
43. <scope>provided</scope>
44. </dependency>
45. <dependency>
46. <groupId>javax.faces</groupId>
47. <artifactId>jsf-api</artifactId>
48. <version>1.2_04</version>
49. <scope>provided</scope>
50. </dependency>
51. <dependency>
52. <groupId>javax.servlet</groupId>
53. <artifactId>jstl</artifactId>
54. <version>1.2</version>
55. <scope>provided</scope>
56. </dependency>
57. <dependency>
58. <groupId>javax.servlet.jsp</groupId>
59. <artifactId>jsp-api</artifactId>
60. <version>2.1</version>
61. <scope>provided</scope>
62. </dependency>
63. <dependency>
64. <groupId>javax.faces</groupId>
65. <artifactId>jsf-impl</artifactId>
66. <version>1.2_04</version>
67. <scope>provided</scope>
68. </dependency>
69. <dependency>
70. <groupId>org.apache.activemq</groupId>
71. <artifactId>activemq-all</artifactId>
72. <version>5.2.0</version>
73. </dependency>
74. <dependency>
75. <groupId>org.springframework</groupId>
76. <artifactId>spring-core</artifactId>
77. <version>2.5.6</version>
78. </dependency>
79. <dependency>
80. <groupId>org.springframework</groupId>
81. <artifactId>spring-jms</artifactId>
82. <version>2.5.6</version>
83. </dependency>
84. </dependencies>
85.</project>
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.<?xml version="1.0" encoding="UTF-8"?>
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. <!-- 配置JMS连接工厂 -->
7. <bean id="connectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory">
8. <property name="brokerURL" value="tcp://localhost:61616"/>
9. </bean>
10.
11. <!-- 配置JMS模版 -->
12. <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
13. <property name="connectionFactory" ref="connectionFactory"/>
14. </bean>
15.
16. <!-- 发送消息的目的地(一个队列) -->
17. <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
18. <!-- 设置消息队列的名字 -->
19. <constructor-arg index="0" value="HelloWorldQueue"/>
20. </bean>
21.
22.
23.
24.</beans>
运行发送端三次:
成功发送了一条JMS消息
Process finished with exit code 0
然后再运行接收端一次:
收到消息内容为: 发送消息:Hello ActiveMQ Text Message!
收到消息内容为: 发送消息:Hello ActiveMQ Text Message!
收到消息内容为: 发送消息:Hello ActiveMQ Text Message!
继续测试发现,接收端接收一条消息后不退出程序,而是继续等待,一旦有消息发送过来,就获取到,然后输出!
ActiveMQJMSmavenSpringBean.
项目环境:
JDK1.5
ActiveMQ5.2
POM文件
Xml代码
1.<?xml version="1.0" encoding="UTF-8"?>
2.<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3. <modelVersion>4.0.0</modelVersion>
4. <groupId>ActiveMQ-Spring</groupId>
5. <artifactId>ActiveMQ-Spring</artifactId>
6. <packaging>war</packaging>
7. <name />
8. <version>0.0.1-SNAPSHOT</version>
9. <description />
10. <build>
11. <sourceDirectory>${basedir}/src</sourceDirectory>
12. <outputDirectory>${basedir}/WebRoot/WEB-INF/classes</outputDirectory>
13. <resources>
14. <resource>
15. <directory>${basedir}/src</directory>
16. <excludes>
17. <exclude>**/*.java</exclude>
18. </excludes>
19. </resource>
20. </resources>
21. <plugins>
22. <plugin>
23. <artifactId>maven-war-plugin</artifactId>
24. <configuration>
25. <webappDirectory>${basedir}/WebRoot</webappDirectory>
26. <warSourceDirectory>${basedir}/WebRoot</warSourceDirectory>
27. </configuration>
28. </plugin>
29. <plugin>
30. <artifactId>maven-compiler-plugin</artifactId>
31. <configuration>
32. <source>1.5</source>
33. <target>1.5</target>
34. </configuration>
35. </plugin>
36. </plugins>
37. </build>
38. <dependencies>
39. <dependency>
40. <groupId>org.apache.openejb</groupId>
41. <artifactId>javaee-api</artifactId>
42. <version>5.0-1</version>
43. <scope>provided</scope>
44. </dependency>
45. <dependency>
46. <groupId>javax.faces</groupId>
47. <artifactId>jsf-api</artifactId>
48. <version>1.2_04</version>
49. <scope>provided</scope>
50. </dependency>
51. <dependency>
52. <groupId>javax.servlet</groupId>
53. <artifactId>jstl</artifactId>
54. <version>1.2</version>
55. <scope>provided</scope>
56. </dependency>
57. <dependency>
58. <groupId>javax.servlet.jsp</groupId>
59. <artifactId>jsp-api</artifactId>
60. <version>2.1</version>
61. <scope>provided</scope>
62. </dependency>
63. <dependency>
64. <groupId>javax.faces</groupId>
65. <artifactId>jsf-impl</artifactId>
66. <version>1.2_04</version>
67. <scope>provided</scope>
68. </dependency>
69. <dependency>
70. <groupId>org.apache.activemq</groupId>
71. <artifactId>activemq-all</artifactId>
72. <version>5.2.0</version>
73. </dependency>
74. <dependency>
75. <groupId>org.springframework</groupId>
76. <artifactId>spring-core</artifactId>
77. <version>2.5.6</version>
78. </dependency>
79. <dependency>
80. <groupId>org.springframework</groupId>
81. <artifactId>spring-jms</artifactId>
82. <version>2.5.6</version>
83. </dependency>
84. </dependencies>
85.</project>
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.<?xml version="1.0" encoding="UTF-8"?>
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. <!-- 配置JMS连接工厂 -->
7. <bean id="connectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory">
8. <property name="brokerURL" value="tcp://localhost:61616"/>
9. </bean>
10.
11. <!-- 配置JMS模版 -->
12. <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
13. <property name="connectionFactory" ref="connectionFactory"/>
14. </bean>
15.
16. <!-- 发送消息的目的地(一个队列) -->
17. <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
18. <!-- 设置消息队列的名字 -->
19. <constructor-arg index="0" value="HelloWorldQueue"/>
20. </bean>
21.
22.
23.
24.</beans>
运行发送端三次:
成功发送了一条JMS消息
Process finished with exit code 0
然后再运行接收端一次:
收到消息内容为: 发送消息:Hello ActiveMQ Text Message!
收到消息内容为: 发送消息:Hello ActiveMQ Text Message!
收到消息内容为: 发送消息:Hello ActiveMQ Text Message!
继续测试发现,接收端接收一条消息后不退出程序,而是继续等待,一旦有消息发送过来,就获取到,然后输出!