Introduction to ActiveMQ
ActiveMQ is Apache's most popular and powerful open source message bus. ActiveMQ is a JMS Provider implementation that fully supports JMS 1.1 and J2EE 1.4 specifications
JMS: Java Message Service java Message Service
ActiveMQ: Implementing JMS Specification
JMS only gives the interface, the concrete implementation is completed by middleware, AcitveMQ is one of them.
Other message queue products: ActiveMQ, RabbitMQ, Kafka, MetaMQ, etc.
Message queue middleware is an important component of distributed system. It mainly solves the problems of application coupling, asynchronous message, traffic sharpening, and achieves high performance, high availability, scalability and ultimate consistency architecture.
ActiveMQ Download
Official website: http://activemq.apache.org/
directory structure
Start ActiveMQ
Enter the bin directory to start the service.
http://localhost:8161/admin/queues.jsp
http port 8161: web page access port
Tcp Port Connection Service Port: 61616
Default login username, password: admin
ActiveMQ Operating Interface
Common terms
Provider/MessageProvider: Producer Consumer/MessageConsumer: Consumer PTP: Point To Point, Point-to-Point Communication Message Model Pub/Sub: Publish/Subscribe, Publish and Subscribe, Publish and Subscribe Messaging Model Queue: Queue, one of the target types, combined with PTP Topic: Topic, one of the target types, combined with Pub/Sub ConnectionFactory: Connection Factory, which JMS uses to create connections Connnection: JMS Client to JMS Provider Destination: Message destination, created by Session Session: Session, created by Connection, is essentially a thread that sends and receives messages, so both producers and consumers are created by Session
spring Integrates ActeMQ Applications
Configuration of producers
Step 1: Create maven, import spring and activeMQ coordinates or web projects, import corresponding activeMQ jar packages and jar packages integrated with spring
<dependencies> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-all</artifactId> <version>5.2.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>4.2.4.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.apache.xbean</groupId> <artifactId>xbean-spring</artifactId> <version>4.2</version> </dependency> </dependencies>
Step 2: Provide spring configuration files (configuration producer related) to introduce amq,jms namespaces
Step 3: Configure the connection factory (cache session factory), and configure the template object
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:task="http://www.springframework.org/schema/task" xmlns:amq="http://activemq.apache.org/schema/core" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.8.0.xsd"><!-- Configure Connection Factory Objects: Generate Connection Mode 1: Adoption amq Namespace Connection Factory Creation Mode 2: Through bean Tag Creation Object--> <!-- <amq:connectionFactory id="connectionFactory" userName="admin" password="admin" brokerURL="tcp://localhost:61616"> </amq:connectionFactory> --> <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <constructor-arg index="0" value="admin"></constructor-arg> <constructor-arg index="1" value="admin"></constructor-arg> <constructor-arg index="2" value="tcp://localhost:61616"></constructor-arg> </bean> <!-- spring Provide optimized caching session object --> <bean id="cachingConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory"> <property name="targetConnectionFactory" ref="connectionFactory"></property> <property name="sessionCacheSize" value="10"></property> </bean> <!-- spring Provide template objects jmsTemplate:towards mq Server Writes Messages( p2p,pub/sub) --> <!-- Send point-to-point messages --> <bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="cachingConnectionFactory"></property> <!-- Passing attribute pubSubDomain Specify message mode:Default value false --> <property name="pubSubDomain" value="false"></property> </bean> <!-- Send topic mode messages --> <bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="cachingConnectionFactory"></property> <property name="pubSubDomain" value="true"></property> </bean> </beans>
Step 4: Write the unit test method and inject the template object JmsTemplate into the class. Send messages to queues through this object
ProductTest@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class ProduceTest { // @Resource("") @Autowired @Qualifier("jmsQueueTemplate") private JmsTemplate jmsTemplate; @Test public void test() { jmsTemplate.send("test_spring", new MessageCreator() { // create object public Message createMessage(Session session) throws JMSException { MapMessage mapMessage = session.createMapMessage(); mapMessage.setString("tel", "1311111111"); mapMessage.setString("code", "MSXX88sdfsdf"); return mapMessage; } }); } /*public static void main(String[] args) { ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); JmsTemplate jmsTemplate = (JmsTemplate) classPathXmlApplicationContext.getBean("jmsQueueTemplate"); //send message jmsTemplate.send("test_spring", new MessageCreator() { //create object public Message createMessage(Session session) throws JMSException { MapMessage mapMessage = session.createMapMessage(); mapMessage.setString("tel", "1311111111"); mapMessage.setString("code", "MSXX88sdfsdf"); return mapMessage; } }); }*/ }
Configuration of consumers
Step 1: Develop a class to listen for message queues
ConsumerListener@Component("consumerListener") public class ConsumerListener implements MessageListener{ //If a message listener is registered, once the message arrives, the listener's onMessage Method public void onMessage(Message message) { try { MapMessage mapMessage = (MapMessage) message; String tel = mapMessage.getString("tel"); String code = mapMessage.getString("code"); System.out.println(tel+"**********"+code); } catch (JMSException e) { e.printStackTrace(); } } }
Step 2: Configure the spring configuration file and register the listener
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:task="http://www.springframework.org/schema/task" xmlns:amq="http://activemq.apache.org/schema/core" xmlns:jms="http://www.springframework.org/schema/jms" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.8.0.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms.xsd"> <!-- Configure Connection Factory Objects: Generate Connection Mode 1: Adoption amq Namespace Connection Factory Creation Mode 2: Through bean Tag Creation Object--> <!-- <amq:connectionFactory id="connectionFactory" userName="admin" password="admin" brokerURL="tcp://localhost:61616"> </amq:connectionFactory> --> <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <constructor-arg index="0" value="admin"></constructor-arg> <constructor-arg index="1" value="admin"></constructor-arg> <constructor-arg index="2" value="tcp://localhost:61616"></constructor-arg> </bean> <!-- spring Provide optimized caching session object --> <bean id="cachingConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory"> <property name="targetConnectionFactory" ref="connectionFactory"></property> <property name="sessionCacheSize" value="10"></property> </bean> <context:component-scan base-package="cn.itcast"></context:component-scan> <!-- Register listener objects in listener containers acknowledge:Setting Response Mode auto Automatic answer destination-type: Queue type( queue,topic) connection-factory:Injection Connection Factory jms:listener: Node injection listener object --> <jms:listener-container acknowledge="auto" destination-type="queue" connection-factory="cachingConnectionFactory"> <!-- destination:Which queue to listen for --> <jms:listener destination="test_spring" ref="consumerListener"/> </jms:listener-container> </beans>
Test code
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class ConsumerTest { @Test public void test() { while(true){ } } }