activemq安装运行及其在springboot中的queue和topic使用

安装activemq

http://activemq.apache.org/download.html

运行

运行
bin\win64\activemq.bat

访问 http://127.0.0.1:8161/admin/ 
用户名/密码:admin/admin

springboot使用

依赖

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

配置

spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.close-timeout=5000
spring.activemq.in-memory=false
spring.activemq.pool.max-connections=100
spring.activemq.send-timeout=3000

# spring.jms.pub-sub-domain=true 开启topic订阅,不开启就是queue

Producer

生产者

package jky.springboot.alliinone.activemq;

import javax.jms.Destination;
import javax.jms.Topic;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;

@Component
public class Producer {
 
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    // 使用queue
    public void send(Destination destination, final String message) {
        jmsMessagingTemplate.convertAndSend(destination, message + "from queue");
    }
    
    //使用topic使用
    //public void send(Topic topi, final String message) {
    //    jmsMessagingTemplate.convertAndSend(topic, message + " from topic");
    //}

    // 监听out队列
    @JmsListener(destination="out.queue")
    public void consumerMessage(String text){
        System.out.println("从out.queue队列收到的回复信息为:"+text);
    }
}

Consumer

queue消费者

package jky.springboot.alliinone.activemq;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Component;

@Component
public class Consumer {
    // 监听mytest队列,并且发送消息到out队列
    @JmsListener(destination = "mytest.queue")
    @SendTo("out.queue")
    public String receiveQueue(String text) {
        System.out.println("Consumer收到的信息为:"+text);
        return "return message "+text;
    }
}

ComsumerTopic

topic消费者

package jky.springboot.alliinone.activemq;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

@Component
public class ComsumerTopic {
    // 监听sample topic
    @JmsListener(destination = "sample.topic")
    public void receiveTopic(String text) {
        System.out.println("Consumer收到的topic信息为:"+text);
    }
}

使用

队列生产者生产消息
Destination destination = new ActiveMQQueue("mytest.queue");
producer.send(destination, "I am YeJiaWei");

topic生产者生产消息
Topic topic = new ActiveMQTopic("sample.topic");
producer.send(topic, "I am YeJiaWei");

猜你喜欢

转载自www.cnblogs.com/ye-hcj/p/9782934.html