一.:要在spring下集成ActiveMQ,那么就需要将jar包导入项目,所需jar包如下:
这些jar包来自“apache-activemq-5.6.0\lib”中。
二:相关的实例
1.实例--1
1):.配置Spring与ActiveMQ的配置文件:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd"
- default-autowire="byName">
- <!-- 配置connectionFactory -->
- <bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory"
- destroy-method="stop">
- <property name="connectionFactory">
- <bean class="org.apache.activemq.ActiveMQConnectionFactory">
- <property name="brokerURL">
- <value>tcp://127.0.0.1:61616</value>
- </property>
- </bean>
- </property>
- <property name="maxConnections" value="100"></property>
- </bean>
- <!-- Spring JMS Template -->
- <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
- <property name="connectionFactory">
- <ref local="jmsFactory" />
- </property>
- <property name="defaultDestinationName" value="subject" />
- <!-- 区别它采用的模式为false是p2p为true是订阅 -->
- <property name="pubSubDomain" value="true" />
- </bean>
- <!-- 发送消息的目的地(一个Topic) -->
- <bean id="destination" class="org.apache.activemq.command.ActiveMQTopic">
- <!-- 设置消息队列的名字 -->
- <constructor-arg index="0" value="subject" />
- </bean>
- <bean id="messageReceiver" class="com.pwc.spring.jms.example.JMSReceiver">
- <property name="jmsTemplate" ref="jmsTemplate"></property>
- </bean>
- </beans>
- package com.pwc.spring.jms.example;
- import javax.jms.Destination;
- import javax.jms.JMSException;
- import javax.jms.Message;
- import javax.jms.Session;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.jms.core.JmsTemplate;
- import org.springframework.jms.core.MessageCreator;
- public class JMSSender {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext-jms.xml");
- JmsTemplate template=(JmsTemplate)applicationContext.getBean("jmsTemplate");
- Destination destination=(Destination)applicationContext.getBean("destination");
- template.send(destination, new MessageCreator(){
- @Override
- public Message createMessage(Session session) throws JMSException {
- // TODO Auto-generated method stub
- return session.createTextMessage("Send Message:Hello ActiveMQ Text Message!");
- }
- });
- System.out.println("Send the JMS Message Successfully!");
- }
- }
A:消息接收的入口:
- package com.pwc.spring.jms.example;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class JMSReceiverMain {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext-jms.xml");
- JMSReceiver proxyJMSConsumer=(JMSReceiver)applicationContext.getBean("messageReceiver");
- proxyJMSConsumer.receive();
- System.out.println("Initial Consumer End! ");
- }
- }
- package com.pwc.spring.jms.example;
- import javax.jms.Destination;
- import javax.jms.JMSException;
- import javax.jms.TextMessage;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.jms.JmsException;
- import org.springframework.jms.core.JmsTemplate;
- public class JMSReceiver {
- public JMSReceiver(){
- }
- private JmsTemplate jmsTemplate;
- public JmsTemplate getJmsTemplate() {
- return jmsTemplate;
- }
- public void setJmsTemplate(JmsTemplate jmsTemplate) {
- this.jmsTemplate = jmsTemplate;
- }
- public void receive(){
- ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext-jms.xml");
- Destination destination=(Destination)applicationContext.getBean("destination");
- while(true){
- try {
- TextMessage txtmsg=(TextMessage)jmsTemplate.receive(destination);
- if(null!=txtmsg){
- System.out.println("DB Proxy: "+txtmsg);
- System.out.println("DB Proxy Message: "+txtmsg.getText());
- }else{
- break;
- }
- } catch (JmsException e) {
- e.printStackTrace();
- } catch (JMSException e) {
- e.printStackTrace();
- }
- }
- }
- }
运行结果:
2. 实例--2
1)Spring与ActiveMQ配置文件信息:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans
- xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
- <!--创建连接工厂-->
- <bean id="connectionFactory"
- class="org.apache.activemq.ActiveMQConnectionFactory">
- <property name="brokerURL" value="tcp://localhost:61616"></property>
- </bean>
- <!-- 声明ActiveMQ消息目标,目标可以是一个队列,也可以是一个主题ActiveMQTopic-->
- <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
- <constructor-arg index="0" value="springactivemq"></constructor-arg>
- </bean>
- <!---->
- <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
- <property name="connectionFactory" ref="connectionFactory"></property>
- <property name="defaultDestination" ref="destination"></property>
- <property name="receiveTimeout" value="600"></property>
- </bean>
- <bean id="sender" class="com.pwc.spring.jms.further.Sender">
- <property name="jmsTemplate" ref="jmsTemplate"></property>
- </bean>
- <bean id="receiver" class="com.pwc.spring.jms.further.Receiver">
- <property name="jmsTemplate" ref="jmsTemplate"></property>
- </bean>
- </beans>
A:发送消息的入口
- package com.pwc.spring.jms.further;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class SenderMain {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
- Sender sender=(Sender)context.getBean("sender");
- sender.sendInfo();
- }
- }
- package com.pwc.spring.jms.further;
- import javax.jms.JMSException;
- import javax.jms.MapMessage;
- import javax.jms.Message;
- import javax.jms.Session;
- import org.springframework.jms.core.JmsTemplate;
- import org.springframework.jms.core.MessageCreator;
- public class Sender {
- private JmsTemplate jmsTemplate;
- public JmsTemplate getJmsTemplate() {
- return jmsTemplate;
- }
- public void setJmsTemplate(JmsTemplate jmsTemplate) {
- this.jmsTemplate = jmsTemplate;
- }
- public void sendInfo(){
- jmsTemplate.send(new MessageCreator(){
- @Override
- public Message createMessage(Session session) throws JMSException {
- MapMessage message=session.createMapMessage();
- message.setString("lastName", "Leio");
- return message;
- }
- });
- }
- }
A:接收消息入口:
- package com.pwc.spring.jms.further;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class ReceiverMain {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
- Receiver receiver=(Receiver) context.getBean("receiver");
- System.out.println(receiver.receiveMessage());
- }
- }
- package com.pwc.spring.jms.further;
- import javax.jms.JMSException;
- import javax.jms.MapMessage;
- import org.springframework.jms.core.JmsTemplate;
- public class Receiver {
- private JmsTemplate jmsTemplate;
- public JmsTemplate getJmsTemplate() {
- return jmsTemplate;
- }
- public void setJmsTemplate(JmsTemplate jmsTemplate) {
- this.jmsTemplate = jmsTemplate;
- }
- public Receiver(){
- }
- public String receiveMessage(){
- String receiveMessage="";
- MapMessage message=(MapMessage)jmsTemplate.receive();
- try {
- receiveMessage=message.getString("lastName");
- } catch (JMSException e) {
- e.printStackTrace();
- }
- return receiveMessage;
- }
- }