ActiveMQ与Spring集成

信息发送者:HelloSender.java

复制代码
package edu.sjtu.erplab.springactivemq; import javax.jms.JMSException; import javax.jms.Session; import javax.jms.Destination; import javax.jms.Message; 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 HelloSender { /** * @param args
     * jmsTemplate和destination都是在spring配置文件中进行配制的
     * Sender只使用了配置文件中的jmsFactory,jmsTemplate,还有destination这三个属性 */ public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-jms.xml");
        JmsTemplate template = (JmsTemplate) applicationContext.getBean("jmsTemplate");
        Destination destination = (Destination) applicationContext.getBean("destination");
        template.send(destination, new MessageCreator() { public Message createMessage(Session session) throws JMSException { return session.createTextMessage("发送消息:Hello ActiveMQ Text Message2!");
            }
        });
        System.out.println("成功发送了一条JMS消息");
    }
}
复制代码

信息接受者:ProxyJMSConsumer.java

复制代码
package edu.sjtu.erplab.springactivemq; import javax.jms.Destination; import javax.jms.TextMessage; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.jms.core.JmsTemplate; /** * JMS消费者
 * 消息题的内容定义
 * 消息对象 接收消息对象后: 接收到的消息体* <p> */ public class ProxyJMSConsumer { public ProxyJMSConsumer() {

    } private JmsTemplate jmsTemplate; public JmsTemplate getJmsTemplate() { return jmsTemplate;
    } public void setJmsTemplate(JmsTemplate jmsTemplate) { this.jmsTemplate = jmsTemplate;
    } /** * 监听到消息目的有消息后自动调用onMessage(Message message)方法 */ public void recive() {
        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] 收到消息内容为: "
                            + txtmsg.getText());
                } else break;
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

}
复制代码

客户端:JMSTest.java

复制代码
package edu.sjtu.erplab.springactivemq; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class JMSTest { /** * @param args */ public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-jms.xml");
        ProxyJMSConsumer proxyJMSConsumer = (ProxyJMSConsumer) applicationContext.getBean("messageReceiver");
        proxyJMSConsumer.recive();
        
        System.out.println("初始化消息消费者");
    }

}
复制代码

Spring配置文件:applicationContext-jms.xml

复制代码
<?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> <!-- 发送消息的目的地(一个队列) --> <bean id="destination" class="org.apache.activemq.command.ActiveMQTopic"> <!-- 设置消息队列的名字 --> <constructor-arg index="0" value="subject" /> </bean> <bean id="messageReceiver" class="edu.sjtu.erplab.springactivemq.ProxyJMSConsumer"> <!--class="edu.sjtu.erplab.springactivemq.ProxyJMSConsumer">--> <property name="jmsTemplate" ref="jmsTemplate"></property> </bean> </beans>
复制代码

测试方法:首先运行JMSTest,然后运行HelloSender。

消息发送者: Sender

复制代码
package edu.sjtu.erplab.springactivemq2; 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; //getter and setter  public JmsTemplate getJmsTemplate() { return jmsTemplate;
    } public void setJmsTemplate(JmsTemplate jmsTemplate) { this.jmsTemplate = jmsTemplate;
    } public void sendInfo() {
        jmsTemplate.send(new MessageCreator() { public Message createMessage(Session session) throws JMSException {
                MapMessage message = session.createMapMessage();
                message.setString("lastName", "ppp"); return message;
            }

        });
    }
}
复制代码

消息发送客户端:SenderTest

复制代码
package edu.sjtu.erplab.springactivemq2; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SenderTest { public static void main(String[] args) { // TODO 自动生成方法存根  ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Sender sender = (Sender) context.getBean("sender");
        sender.sendInfo();
    }
}
复制代码

消息接收者:Receiver

复制代码
package edu.sjtu.erplab.springactivemq2; import javax.jms.JMSException; import javax.jms.MapMessage; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.support.JmsUtils; public class Receiver { private JmsTemplate jmsTemplate; //getter and setter  public JmsTemplate getJmsTemplate() { return jmsTemplate;
    } public void setJmsTemplate(JmsTemplate jmsTemplate) { this.jmsTemplate = jmsTemplate;
    } /** * 构造函数 */ public Receiver() {
    } public String receiveMessage() {
        String my = "";
        MapMessage message = (MapMessage) jmsTemplate.receive(); try {
            my = message.getString("lastName");
        } catch (JMSException e) { throw JmsUtils.convertJmsAccessException(e);
        } return my;
    }


}
复制代码

消息接收客户端:ReceiverTest

复制代码
package edu.sjtu.erplab.springactivemq2; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ReceiverTest { public static void main(String[] args) { // TODO 自动生成方法存根  ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Receiver receiver = (Receiver) context.getBean("receiver");
        System.out.print(receiver.receiveMessage());
    }
}
复制代码

Spring配置文件:applicationContext.xml

复制代码
<?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="edu.sjtu.erplab.springactivemq2"></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="edu.sjtu.erplab.springactivemq2.Sender"> <property name="jmsTemplate" ref="jmsTemplate"></property> </bean> <bean id="receiver" class="edu.sjtu.erplab.springactivemq2.Receiver"> <property name="jmsTemplate" ref="jmsTemplate"></property> </bean> </beans>
 
 
 
 
 

  1. <dependency>  
  2.     <groupId>org.springframework</groupId>  
  3.     <artifactId>spring-jms</artifactId>  
  4.     <version>3.2.9.RELEASE</version>  
  5. </dependency>  
  6.   
  7. <dependency>  
  8.      <groupId>org.apache.activemq</groupId>  
  9.      <artifactId>activemq-all</artifactId>  
  10.      <version>5.5.0</version>  
  11. </dependency>  
  12. <!--  ActiveMQ依赖的日志包 -->  
  13. <dependency>  
  14.     <groupId>log4j</groupId>  
  15.     <artifactId>log4j</artifactId>  
  16.     <version>1.2.14</version>  
  17. </dependency>  
  18.   
  19. <dependency>  
  20.     <groupId>org.slf4j</groupId>  
  21.     <artifactId>slf4j-log4j12</artifactId>  
  22.     <version>1.5.6</version>  
  23. </dependency>  

你可能感兴趣的:(ActiveMQ与Spring集成)