activemq artemis安装运行及其在springboot中的使用

安装

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

创建broker

将artemis解压完成后,在重新建一个文件夹artmisbroker

运行
artemis.cmd create C:\artmisbroker --user mq --password 123
即可在artmisbroker目录下生成所需的文件

运行artemis 
"C:\artmisbroker\bin\artemis" run

使用Windows service方式运行artemis
"C:\artmisbroker\bin\artemis-service.exe" install
"C:\artmisbroker\bin\artemis-service.exe" start

停止 windows service:
"C:\artmisbroker\bin\artemis-service.exe" stop

卸载windows service
"C:\artmisbroker\bin\artemis-service.exe" uninstall

访问 http://localhost:8161/console 进入监视界面

在springboot中的使用

依赖

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

配置

spring.artemis.mode=native
spring.artemis.host=localhost
spring.artemis.port=61616
spring.artemis.user=mq
spring.artemis.password=123

Producer

@Component
public class Producer {
 
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;
 
    public void send(Destination destination, final String message) {
        jmsMessagingTemplate.convertAndSend(destination, message + "from queue");
    }

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

Consumer

@Component
public class Consumer {
    @JmsListener(destination = "mytest.queue")
    @SendTo("out.queue")
    public String receiveQueue(String text) {
        System.out.println("Consumer收到的信息为:"+text);
        return "return message "+text;
    }
}

Rest使用

@ResponseBody
@RequestMapping(value = "/mqtest", method = RequestMethod.GET)
public Object mqtest() {
    Destination destination = new ActiveMQQueue("mytest.queue");
    producer.send(destination, "I am YeJiaWei");
    return "OK";
}

猜你喜欢

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