RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
activemqspring客户端

一、dependency

专注于为中小企业提供网站设计、做网站服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业五大连池免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上千企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。


    5.15.4
    4.8
    5.0.7.RELEASE



    
        org.apache.activemq
        activemq-client
        ${activemq.version}
    
    
        org.apache.activemq
        activemq-spring
        ${activemq.version}
    
    
        org.apache.activemq
        activemq-pool
        ${activemq.version}
    
    
        org.apache.activemq
        activemq-broker
        ${activemq.version}
    

    
        org.apache.xbean
        xbean-spring
        ${xbean-spring.version}
    
    
        org.springframework
        spring-jms
        ${spring-jms.version}
    


    
        org.springframework
        spring-core
    
    
        org.springframework
        spring-beans
    
    
        org.springframework
        spring-context
    
    
        org.springframework
        spring-context-support
    

    
        org.apache.commons
        commons-pool2
    

    
        org.slf4j
        slf4j-log4j12
    
    
        org.apache.logging.log4j
        log4j-core
    

二、activemq.properties

active.config.brokerURL=failover:(tcp://localhost:61617,tcp://localhost:61618,tcp://localhost:61619)
active.config.username=admin
active.config.password=admin123

active.destination.queue.name=queue.test01
active.destination.topic.name=topic.test01

三、spring-activemq-producer.xml


      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.xsd
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

   

   
   
       
       
       
       
       
       
       
       
       
       
   

   
   
   
       
           ${active.config.brokerURL}
       

       
           ${active.config.username}
       

       
           ${active.config.password}
       

       
   


   
   
       
   


   
   
       
   


   
   
       
   

   
       
   


   
   
       
       
   


   
   
       
       
   



四、spring-activemq-consumer.xml


      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.xsd
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

   

   
   
       
           ${active.config.brokerURL}
       

       
           ${active.config.username}
       

       
           ${active.config.password}
       

   


   
   
       
   


   
   
       
   


   
   
       
   

   
       
   


   
   
   
   
       
   


   
   
       
       
       
   


   
   
       
       
       
   




五、相关业务实现类

producer相关类:

import org.springframework.jms.core.JmsTemplate;

import javax.jms.Destination;

public abstract class AbstractActivemqProducer {
   private JmsTemplate jmsTemplate;
   private Destination destination;

   public AbstractActivemqProducer(JmsTemplate jmsTemplate, Destination destination) {
       this.jmsTemplate = jmsTemplate;
       this.destination = destination;
   }

   public void send(String msg){
       jmsTemplate.convertAndSend(destination, msg);
   }
}

public class QueueActivemqProducer extends AbstractActivemqProducer {

   public QueueActivemqProducer(JmsTemplate jmsTemplate, Destination destination) {
       super(jmsTemplate, destination);
   }
}

public class TopicActivemqProducer extends AbstractActivemqProducer {

   public TopicActivemqProducer(JmsTemplate jmsTemplate, Destination destination) {
       super(jmsTemplate, destination);
   }
}

customer相关类:

public class CustomerMsgListener implements MessageListener {

    private BusinessHandler businessHandler;

    public CustomerMsgListener(BusinessHandler businessHandler) {
        this.businessHandler = businessHandler;
    }

    @Override
    public void onMessage(Message message) {
        try {
            if (message instanceof TextMessage) {
                businessHandler.handle(((TextMessage) message).getText() );
            }
            if (message instanceof MapMessage) {
                MapMessage mapMessage = (MapMessage) message;
                businessHandler.handle(mapMessage.getString("key01") );
                businessHandler.handle(mapMessage.getString("key02") );
            }
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}

public interface BusinessHandler {

    void handle(String msg);
}

public class QueueHandler implements BusinessHandler {
    @Override
    public void handle(String msg) {
        System.out.println("msg = [" + msg + "]");
    }

}

六、测试

public class XmlActivemqTest {

    public static void main(String[] args) {
        customerXml();
    }

    public static void producerXml(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath*:spring-activemq-producer.xml");
        AbstractActivemqProducer queueActivemqProducer = context.getBean(QueueActivemqProducer.class);
        queueActivemqProducer.send("this is a test");
    }


    public static void customerXml(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath*:spring-activemq-customer.xml");
    }
}

参考地址:

http://activemq.apache.org/spring-support.html

http://docs.spring.io/spring/docs/2.5.x/reference/jms.html#jms-mdp


分享标题:activemqspring客户端
文章转载:http://lswzjz.com/article/jsgggi.html