Activemq Getting Started +spring boot integration activemq

Source: Internet
Author: User
Tags message queue

Recently want to learn MOM (message middleware: Messages oriented middleware), from the comparative basis of ACTIVEMQ learning, RabbitMQ, ZeroMQ, ROCKETMQ, Kafka and other follow-up to learn.

It says that ACTIVEMQ is a kind of message middleware, but why use ACTIVEMQ?

In the absence of JMS, many applications will have synchronous communication (after the client initiates the request to wait for the service side to return the results to continue execution), the client side of the server coupling, the single point-to-point (peer to edge) communication problem, JMS can be oriented to the message middleware approach to solve the above problem.

JMS Specification Terminology:

Provider/messageprovider: Producer

Consumer/messageconsumer: Consumer

Message form:
1, point-to-point (queue)
2, one-to-many (topic)

ConnectionFactory: Connection Factory, JMS uses it to create a connection

CONNNECTION:JMS client-to-JMS provider connection

Destination: Message destination, created by session

Session: Sessions, created by connection, are essentially a thread that sends and receives messages, so producers and consumers are created by the session

I installed the Windows version here, after the installation of the directory is such

To the bin directory, start Activemq.bat

This will start the success.

Visit http://localhost:8161/admin/index.jsp to see the console, such as:

Spring Boot Integration ACTIVEMQ:

Referenced in Pom.xml

<dependency>            <groupId>org.springframework.boot</groupId>            <artifactId> spring-boot-starter-activemq</artifactid>        </dependency>        <dependency>            <groupid >org.apache.activemq</groupId>            <artifactId>activemq-pool</artifactId>            <!--< Version>5.7.0</version>-        </dependency>

To configure the ACTIVEMQ connection in application.properties:

SPRING.ACTIVEMQ.BROKER-URL=TCP://localhost:61616spring.activemq.in-memory=true  Spring.activemq.pool.enabled=false

Create message Producer:

/** * @authorHuangzhang * @description * @date Created in 2018/7/16 18:57*/@Service ("Provider") Public classProvider {@AutowiredPrivatejmsmessagingtemplate jmsmessagingtemplate;  Public voidSendMessage (Destination Destination,FinalString message)    {jmsmessagingtemplate.convertandsend (destination,message); }
The message @JmsListener in the queue "Return.queue" returned by consumer consumers (destination= "Return.queue") Public voidconsumermessage (String string) {System.out.println ("The reply message received from the Return.queue queue is:" +string); }}

Create a first consumer:

/**@author*/@Componentpublicclass  Consumer {    = "Mytest.queue")    publicvoid  receivequeue (String Text) {        System.out.println ("Consumer received the message:" +text);}    }

Create a second consumer (this not only consumes the message from the producer into the queue, but also inserts the return value into the "Return.queue" queue):

/**@author*/@Componentpublicclass  Consumer1 {    = "Mytest.queue")    @SendTo ("Return.queue")    public string Receivequeue (String message) {        System.out.println ("Consumer1 received the message:" +message);         return "========return message" +message;    }}

Test method:

@Service  Public class springbootjmsapplicationtests {    @Autowired    private  Provider Provider;          Public void throws interruptedexception {        new activemqqueue ("Mytest.queue");          for (int i=0; i<10; i++) {            "Huangzhang" +i);     }}}

Here I call the test method in the controller:

/**@author*/@Controllerpublicclass  Test {    @Autowired    springbootjmsapplicationtests springbootjmsapplicationtests;    @RequestMapping ("/")    @ResponseBody    public String test01 ()throws  exception{                springbootjmsapplicationtests.contextloads ();                 return "Success!" ;    }}

Visit http://localhost:8080/to test this demo

Here is the result of the execution, it can be seen that two consumers respectively consumed the message of the producer into the message queue, and Consumer1 consumers will return the results into the queue for the producers to consume.

View queues

Here we can see that our producers are circulating to the Mytest.queue queue 10 times, and two consumers consume 10 messages

Consumer1 consumes 5 messages and writes 5 times to the return queue Return.queue, 5 times by the original producer

Here a simple spring boot integration ACTIVEMQ Demo is complete (please correct me).

Activemq Getting Started +spring boot integration 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.