`

Spring 和ActionMQ整合JMS开发应用

阅读更多

           Spring 和ActionMQ整合JMS开发应用,实现异步的消息的应用.使用Apache的ActiveMQ发送消息,activemq-all-5.2.jar

spring2.5 jar;jms.jar 等

 

服务段代码:

package com.unutrip.activemq.jms;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
/**
 * 消息发送者
 * 
 * @author longgangbai
 * 
 */
public class SpringJMSProductor {
 private JmsTemplate template;
 private Destination destination;
    /**
     * 发送消息
     * @param message
     */
 public void createMessage(final String message) {
  template.send(destination, new MessageCreator() {
   public Message createMessage(Session session) throws JMSException {
    return session.createTextMessage(message);
   }
  });
  System.out.println(message);
 }
 public JmsTemplate getTemplate() {
  return template;
 }
 public void setTemplate(JmsTemplate template) {
  this.template = template;
 }
 public Destination getDestination() {
  return destination;
 }
 public void setDestination(Destination destination) {
  this.destination = destination;
 }
}

 客户端代码:

package com.unutrip.activemq.jms;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.TextMessage;
import org.springframework.jms.core.JmsTemplate;
/**
 * 消息接收者
 * 
 * @
 */
public class SpringJMSReceiver {
 private JmsTemplate template;
 private Destination destination;
 public JmsTemplate getTemplate() {
  return template;
 }
 public void setTemplate(JmsTemplate template) {
  this.template = template;
 }
 public Destination getDestination() {
  return destination;
 }
 public void setDestination(Destination destination) {
  this.destination = destination;
 }
 public void receive() throws JMSException {
  while (true) {
   TextMessage txtmsg = (TextMessage) template.receive(destination);
   if (null != txtmsg)
    System.out.println("收到消息内容为: " + txtmsg.getText());
   else
    break;
  }
 }
}

 

xml配置如下:

本实例中客户端和服务端在一台电脑上,共用一個 配置JMS模版 和发送消息的目的地(一个队列)

JMS连接工厂和,jMS目标类

<?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.5.xsd">
      
        <!-- 
        <bean id="broker" class="org.apache.activemq.xbean.BrokerFactoryBean">
          <property name="config" value="classpath:org/apache/activemq/xbean/activemq.xml" />
          <property name="start" value="true" />
        </bean>
         -->
        <!-- 配置JMS连接工厂 --> 
        <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> 
                <property name="brokerURL" value="vm://localhost" />
        </bean>
        <!-- 配置JMS模版 --> 
        <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> 
                <property name="connectionFactory" ref="connectionFactory"/> 
        </bean>
        <!-- 发送消息的目的地(一个队列) --> 
        <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue"> 
                <!-- 设置消息队列的名字 --> 
                <constructor-arg index="0" value="HelloWorldQueue"/> 
        </bean> 
        
        <!--服务端用于发送JMS消息 -->
        <bean id="jmsproductor" class="com.unutrip.activemq.jms.SpringJMSProductor">
           <property name="template">
             <ref bean="jmsTemplate"/>
           </property>
           <property name="destination">
               <ref bean="destination"/>
           </property>
        </bean>
        
        <!-- 客户端用于接受JMS消息 -->
        <bean id="jmsreceiver" class="com.unutrip.activemq.jms.SpringJMSReceiver">
           <property name="template">
             <ref bean="jmsTemplate"/>
           </property>
           <property name="destination">
               <ref bean="destination"/>
           </property>
        </bean>
</beans>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics