1.JMS(ActiveMQ)

Spring Boot支持的jms有:ActiveMQ、Artemis、HornetQ

三个加入相应 jar包即可以使用

一、添加依赖

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

二、配置文件

# ACTIVEMQ (ActiveMQProperties)
spring.activemq.in-memory=true
#spring.activemq.broker-url=
#spring.activemq.password=
#spring.activemq.user=
#spring.activemq.packages.trust-all=false
#spring.activemq.packages.trusted=
#spring.activemq.pool.configuration.*=
#spring.activemq.pool.enabled=false
#spring.activemq.pool.expiry-timeout=0
#spring.activemq.pool.idle-timeout=30000
#spring.activemq.pool.max-connections=1

三、代码实现

启动注解:

@EnableJms 添加在main方法上面

所涉及文件

配置队列

util/configuration/JmsConfiguration.java

package com.shuju.test2.util.configuration;

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.jms.Queue;

@Configuration
public class JmsConfiguration {

    @Bean
    public Queue queue() {
        return new ActiveMQQueue("demo.queue");
    }
}

component/DemoJmsComponent.java

package com.shuju.test2.component;

import javax.jms.Queue;

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 DemoJmsComponent {

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    private Queue queue;

    public void send(String msg) {
        this.jmsMessagingTemplate.convertAndSend(this.queue, msg);
    }

    @JmsListener(destination = "demo.queue")
    public void receiveQueue(String text) {
        System.out.println("接受到:" + text);
    }

}

测试

package com.shuju.test2;

import com.shuju.test2.component.DemoJmsComponent;
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;

import java.util.Date;

@RunWith(SpringRunner.class)
@SpringBootTest
public class Test2ApplicationTests {

    @Autowired
    private DemoJmsComponent demoJmsComponent;

    @Test
    public void send() {
        demoJmsComponent.send("hello world");
    }
}

资料

spring boot整合JMS(ActiveMQ实现)

spring boot集成ActiveMQ

实力干货!SpringBoot JMS(ActiveMQ) 使用实践

Spring Boot学习笔记——Spring Boot与ActiveMQ的集成

Last updated