Spring Boot series (12) Spring boot integration Activeq for messaging and subscriptions

Source: Internet
Author: User
Spring Boot series (12) Spring boot integration Activeq for messaging and subscriptions

This article introduces spring boot integration Activeq for messaging and subscriptions, which requires springboot full video tutorial, click here.


The Javax.jms.ConnectionFactory interface provides a standard method for creating a javax.jms.Connection, javax.jms.Connection is used to interact with the JMS broker (broker). Although a connectionfactory is required in order to use jms,spring, it is generally not necessary to use it directly, but instead relies on the upper-layer message abstraction, and Spring boot automatically configures the facilities (infrastructure) required to send and receive messages.


If ACTIVEMQ is found to be available under Classpath, Spring boot configures a connectionfactory. If a proxy is required, an inline, automatically configured proxy is turned on (as long as the proxy URL is not specified in the configuration).


Introducing SPRING-BOOT-STARTER-ACTIVEMQ, add the following configuration in the Pom.xml configuration file (based on the Pom.xml file in the previous section, "Spring Boot Build Framework"):


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>


The ACTIVEMQ configuration is controlled by the external configuration in the spring.activemq.*. In the Application.properties configuration file, add the following:


spring.activemq.broker-url=tcp://192.168.1.100:9876
Spring.activemq.user=admin
Spring.activemq.password=secret


By default, if the target does not exist, ACTIVEMQ will create one, so the target is resolved by the name they provide.


Application Consolidation Axtiveq Support case


The message producer, with the following specific code:


Import javax.jms.Destination;

Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.jms.core.JmsMessagingTemplate;
Import Org.springframework.stereotype.Service;

@Service ("producer")
public class Producer {

@Autowired


Send message, destination is sent to the queue, message is to be sent messages
public void SendMessage (Destination Destination, final String message) {
Jmstemplate.convertandsend (destination, message);
}
}


Two message consumers, the specific code is as follows:


/***************** Consumer1 ******************/

Import Org.springframework.jms.annotation.JmsListener;
Import org.springframework.stereotype.Component;

@Component
public class Consumer {
Use Jmslistener to configure a queue for consumer listening, where text is the received message
@JmsListener (Destination = "Mytest.queue")
public void Receivequeue (String text) {
System.out.println ("Consumer received the message:" +text);
}
}

/***************** Consumer2 ******************/

Import Org.springframework.jms.annotation.JmsListener;
Import org.springframework.stereotype.Component;

@Component
public class Consumer2 {
Use Jmslistener to configure a queue for consumer listening, where text is the received message
@JmsListener (Destination = "Mytest.queue")
public void Receivequeue (String text) {
System.out.println ("Consumer2 received the message:" +text);
}
}


Note that the @component or @service annotations must be added to the message consumer's class, so that the message consumer class is delegated to the listener class, The principle is similar to using Sessionawaremessagelistener and Messagelisteneradapter to implement message-driven Pojo.


Simple test message case, the code is as follows:


Import javax.jms.Destination;

Import Org.apache.activemq.command.ActiveMQQueue;
Import Org.junit.Test;
Import Org.junit.runner.RunWith;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.boot.test.context.SpringBootTest;
Import Org.springframework.test.context.junit4.SpringRunner;

@RunWith (Springrunner.class)
@SpringBootTest
public class Springbootjmsapplicationtests {

@Autowired
Private Producer Producer;

@Test
public void Contextloads () throws Interruptedexception {
Destination Destination = new Activemqqueue ("Mytest.queue");

for (int i=0; i<100; i++) {
Producer.sendmessage (Destination, "My site is blog.yoodb.com");
}
}

}


The results of the operation are as follows:


Consumer2 received the message: My site is blog.yoodb.com
Consumer received the message: My site is blog.yoodb.com
Consumer2 received the message: My site is blog.yoodb.com
Consumer received the message: My site is blog.yoodb.com
Consumer2 received the message: My site is blog.yoodb.com
Consumer received the message: My site is blog.yoodb.com
Consumer2 received the message: My site is blog.yoodb.com
Consumer received the message: My site is blog.yoodb.com
Consumer2 received the message: My site is blog.yoodb.com
Consumer received the message: My site is blog.yoodb.com
Consumer2 received the message: My site is blog.yoodb.com
Consumer received the message: My site is blog.yoodb.com
Consumer2 received the message: My site is blog.yoodb.com

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.