千家信息网

activemq spring客户端

发表于:2025-01-22 作者:千家信息网编辑
千家信息网最后更新 2025年01月22日,一、dependency 5.15.4 4.8 5.0.7.RELEASE
千家信息网最后更新 2025年01月22日activemq spring客户端

一、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=adminactive.config.password=admin123active.destination.queue.name=queue.test01active.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


0