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

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


新闻标题:activemqspring客户端-创新互联
网页地址:http://scyanting.com/article/pihge.html