Simple example Application of ACTIVEMQ

Source: Internet
Author: User

ACTIVEMQ is a message middleware that implements the JMS specification, providing both point-to-point and subscription-release modes. The following describes the use of ACTIVEMQ;

First, the construction of the environment

First we need to download the ACTIVEMQ installation package, http://activemq.apache.org/activemq-510-release.html.

Unzip directly and copy to the C drive. The final directory is: C:\Program files\activemq.

The following is the start Activemq, the method is to go into the bin directory, Win32 directory, find Activemq.bat and double-click;

Then you can start the ACTIVEMQ, and start the interface as shown;

Then enter the following address in the browser http://localhost:8161/admin/(username: admin, Password: damin) can be entered into the ACTIVEMQ console.

The following is the creation of the code.

Second, the project code

1. Maven Dependency Package:

<!--Activemq's Dependence -<Dependency>    <groupId>Org.apache.activemq</groupId>    <Artifactid>Activemq-core</Artifactid>    <version>5.5.0</version></Dependency><Dependency>    <groupId>Org.apache.activemq</groupId>    <Artifactid>Activemq-client</Artifactid>    <version>5.13.1</version></Dependency><Dependency>    <groupId>Org.apache.activemq</groupId>    <Artifactid>Activemq-pool</Artifactid>    <version>5.13.1</version></Dependency><Dependency>    <groupId>Org.apache.activemq</groupId>    <Artifactid>Activemq-broker</Artifactid>    <version>5.13.1</version></Dependency><!--Activemq+spring's Dependence -<Dependency>    <groupId>Org.apache.activemq</groupId>    <Artifactid>Activemq-spring</Artifactid>    <version>5.12.2</version></Dependency><Dependency>    <groupId>Org.springframework</groupId>    <Artifactid>Spring-jms</Artifactid>    <version>4.1.9.RELEASE</version></Dependency>

2. ACTIVEMQ and Spring configuration files:

<?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:context= "Http://www.springframework.org/schema/context" xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-2.5.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/c Ontext/spring-context-2.5.xsd "default-autowire=" ByName "> <!--First step: Configure ConnectionFactory--<bean id=" ta Rgetconnectionfactory "class=" org.apache.activemq.ActiveMQConnectionFactory "> <property name=" Brokerurl " Value= "tcp://localhost:61616"/> <property name= "userName" value= "admin"/> <property name= "Password" V Alue= "admin"/> </bean> <bean id= "pooledconnectionfactory" class= " Org.apache.activemq.pool.PooledConnectionFactory "> <property name=" connectionfactory"ref=" Targetconnectionfactory "/> <property name=" MaxConnections "value=" ten "/> </bean> <bean id=" c Onnectionfactory "class=" org.springframework.jms.connection.SingleConnectionFactory "> <property name=" Targetconnectionfactory "ref=" Pooledconnectionfactory "/> </bean> <!--Step Two: Configure Jmstemplate message producer--&lt ; Bean id= "jmstemplate" class= "Org.springframework.jms.core.JmsTemplate" > <!-- This connectionfactory corresponds to the ConnectionFactory object that we have defined spring provides--<property name= "ConnectionFactory" ref= " ConnectionFactory "/> </bean> <!--queue destinations, point-to-<bean id=" queuedestination "class=" Org.apache.activemq.command.ActiveMQQueue "> <constructor-arg value=" queue "/> </bean> <!--themed destinations, set Read Release--<bean id= "topicdestination" class= "Org.apache.activemq.command.ActiveMQTopic" > <constructor-arg value= "topic"/> </bean> <!--step Three: Configure the jmstemplate message consumer--<!--message MonitorListener--<bean id= "Consumermessagelistener" class= "Com.zte.ems.springjms.listener.ConsumerMessageListener"/ > <!--Com.zte.ems.springjms.listener.ConsumerMessageListener This class is created by itself, implements MessageListener interface--<!-- Message monitoring container--<bean id= "Jmscontainer" class= "Org.springframework.jms.listener.DefaultMessageListenerContainer" &          Gt <property name= "ConnectionFactory" ref= "ConnectionFactory"/> <property name= "queue_destination" ref= "qu Euedestination "/> <property name=" topic_destination "ref=" topicdestination "/> <property name = "MessageListener" ref= "Consumermessagelistener"/> </bean> </beans>

Description

1) First step: Configure ConnectionFactory

ConnectionFactory is used to generate a link to a JMS server, and spring provides us with multiple connectionfactory, There are singleconnectionfactory and cachingconnectionfactory. Singleconnectionfactory the request to establish a JMS server link will always return the same link, and the Close method call of connection will be ignored. Cachingconnectionfactory inherits Singleconnectionfactory, so it has all the features of Singleconnectionfactory, and it also has a new cache feature that caches session, MessageProducer and Messageconsumer. Here we use Singleconnectionfactory as an example. Spring provides connectionfactory only spring is used to manage connectionfactory, and the connectionfactory that actually generate a link to the JMS server are provided by the JMS service vendor. And it needs to be injected into the connectionfactory provided by spring. We are using JMS implemented by ACTIVEMQ, so the real connection that can be produced here is the connectionfactory provided by ACTIVEMQ. ACTIVEMQ provides us with a pooledconnectionfactory that can be used to inject a activemqconnectionfactory into the connection, Session and MessageProducer pooling, this can greatly reduce our resource consumption.

2) Step Two: Configure the message generator

After configuring the ConnectionFactory, we need to configure the producer. The producer is responsible for generating the message and sending it to the JMS server, which usually corresponds to one of our business logic service implementation classes. But how does our service implementation class send messages? This is usually done with the Jmstemplate class that spring provides for us, so the most important thing to configure the producer is to configure the jmstemplate to send the message. For a message sender, it needs to know where to send the message, so we need to inject a spring-provided connectionfactory object into it when defining jmstemplate. When we really use jmstemplate for message sending, we need to know the destination where the message is sent, that is, destination. There is a destination interface in JMS that represents the destination, there is no method definition in it, just to make an identity. The default destination is used when we do not specify destination when using Jmstemplate for message sending. The default destination can be injected by using the property defaultdestination or Defaultdestinationname when defining the Jmstemplate bean object. Defaultdestinationname corresponds to a normal string. Two types of destination are implemented in ACTIVEMQ, one is point-to-point Activemqqueue and the other is activemqtopic that supports subscription/release mode. When defining both types of destination, we can construct them by using a Name property.

3) Configure Message consumers

After the producer sends a message to the specified destination destination, the consumer then consumes the message for the specified destination. So how does the consumer know that there are producers sending messages to the specified destination destination? This is achieved through spring for our encapsulated Message listener container Messagelistenercontainer, which is responsible for receiving information and distributing the received information to the real messagelistener for processing. Each customer must have a corresponding Messagelistenercontainer for each destination. For a message listener, in addition to knowing which destination to listen to, you need to know where to listen, which means it needs to know which JMS server to listen to. This is achieved by injecting a connectionfactory into the configuration messageconnectionfactory. So when we configure a messagelistenercontainer, there are three attributes that must be specified, one is the connectionfactory that indicates where to listen, and the other is destination One is the messagelistener of message processing after receiving the message. Spring provides us with two types of Messagelistenercontainer,simplemessagelistenercontainer and Defaultmessagelistenercontainer.

Simplemessagelistenercontainer will create a conversation session and consumer consumer at the very beginning, and will use the standard JMS Messageconsumer.setmessagelistener () method registers the listener to let the JMS provider invoke the listener's callback function. It does not dynamically adapt to runtime needs and participates in external transaction management. In terms of compatibility, it is very close to the standalone JMS specification, but is generally incompatible with the JMS restrictions of Java EE. In most cases we still use defaultmessagelistenercontainer, compared with Simplemessagelistenercontainer, Defaultmessagelistenercontainer will dynamically adapt to runtime needs and be able to participate in external transaction management. It balances well with low requirements for JMS providers, advanced features such as transactional participation, and compatibility with Java EE environments.

3.1) define Messagelisenter for handling messages

 Package Com.zte.ems.springjim.listener; Import Javax.jms.Message; Import Javax.jms.MessageListener;  Public class Implements MessageListener {    publicvoid  onMessage (Message arg0) {        //  Here We listen to the message sent by the producer and proceed with the process     }} .

3.2) Message listener container:

The Message Listener container (MSG listener container) is a special bean that monitors the JMS destination and waits for the message to arrive. Once a message arrives, it takes out the message and passes the message to any message listener that is interested in the message.

Simple example Application of ACTIVEMQ

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.