springboot integration ActiveMQ1 (basic use)

Description : acitveMQ version: 5.9.1, springboot version 2.0.3

A. Download and install (windows)

  Official Download: point I jump , choose windows installation package download and unpack, run the bin directory after decompression activemq.bat start the service, you can start successfully without error. The default management address is: localhost: 8161 / ADMIN , the default administrator account password is ADMIN / ADMIN .

Two. Springboot integration

1. Create a project springboot

  Creating springboot web project, adding springboot-starter-activemq dependence.

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

  Then edit the file with, plus a configuration: 61616 is the default port activeMQ temporarily do other configuration, use the default value.

spring:
  activemq:
    broker-url: tcp://localhost:61616

2. Create the Producer Consumer

  The default configuration springboot in activeMQ for Production - consumer model , there is a model for a subscription model - released revisit later. Project directory as follows:

Project Directory

  First, write configuration class Config.java, the following code

@Configuration
public class Config {
    @Bean(name = "queue2")
    public Queue queue2(){
        return new ActiveMQQueue("active.queue2");
    }

    @Bean(name = "queue1")
    public Queue queue1(){
        return new ActiveMQQueue("active.queue1");
    }
}

The above code creates two message queues queue1, queue2, were injected into the vessel by Spring and queue1 queue2 two Bean. Will activeMQ management page after running -> queue to see the following:

queue

  Manufacturer Producer.java code is as follows:

@RestController
public class Producer {
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;
    @Autowired()
    @Qualifier("queue2")
    private Queue queue2;
    @Autowired()
    @Qualifier("queue1")
    private Queue queue1;

    @GetMapping("/queue2")
    public void sendMessage1(String message){
            jmsMessagingTemplate.convertAndSend(queue2,"I'm from queue2:"+message);
    }

    @GetMapping("/queue1")
    public void sendMessage2(String message){
        jmsMessagingTemplate.convertAndSend(queue1,"I'm from queue1:"+message);
    }
}

The above creates two classes GET interface to access both interfaces are sending a message to queue1 and queue2.

Consumers Comsumer.java code is as follows:

@Component //将该类注解到Spring 容器中
public class Comsumer {
    //接受消息队列1消息
    @JmsListener(destination = "active.queue1") //监听active.queue1消息队列
    public void readActiveQueue11(String message){
        System.out.println(1+message);
    }

    //接受消息队列1消息
    @JmsListener(destination = "active.queue1")
    public void readActiveQueue12(String message){
        System.out.println(2+message);
    }

    //接受消息队列2消息
    @JmsListener(destination = "active.queue2")
    public void readActiveQueue21(String message){
        System.out.println(1+message);
    }

    //接受消息队列2消息
    @JmsListener(destination = "active.queue2")
    public void readActiveQueue22(String message){
        System.out.println(2+message);
    }
}

The above code defines four consumers, each consumer a two message queues.

3. Run

  ?? After the projects are to start / queue1 message = niihao, / queue2 message = nihaoa send http request, then we can see the following output in the console:

2I'm from queue2:nihaoa
1I'm from queue2:nihaoa
2I'm from queue1:nihao
1I'm from queue1:nihao

Messages are successfully consumer spending, can also be seen from the results of a print characteristics of producers and consumers: A message will be consumed by a consumer. At the same time can be seen in the management page:

operation result

Each message queue has two customers, the queue to enter the three messages, three out of the news, indicating that messages are consumed, and if consumers commented code, run again, and then send the message you will find MessagesEnqueued quantities greater than MessagesDequeued, and then let the consumer line immediately consumed messages in the queue.

Original article published on: https://www.tapme.top/blog/detail/2018-09-05-10-38

Source: https://github.com/FleyX/demo-project/tree/master/jms_demo

Guess you like

Origin www.cnblogs.com/wuyoucao/p/10947940.html