Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Activemq spring client

Shulou Source: shulou.com Published: 2022-06-03 07:41:10 10月24日 Update

1. 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

II. 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

III. Spring-activemq-producer.xml

${active.config.brokerURL}

${active.config.username}

${active.config.password}

IV. Spring-activemq-consumer.xml

${active.config.brokerURL}

${active.config.username}

${active.config.password}

5. Related business implementation classes

Producer related classes:

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 related classes:

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 + "]");}}

VI. Testing

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") }}

Reference address:

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

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

Tags: Time maximum business multiples address reference delay test customer client Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Linux vpn Docker Shulou Technology