1.依赖引入
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- springboot2.1.7 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-artemis</artifactId>
</dependency>
<!-- springboot2.1.7对应的 artemis-jms-server 版本是2.6.4-->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-jms-server</artifactId>
<version>2.6.4</version>
</dependency>
</dependencies>
2.配置文件的配置:mlx配置前缀是我自己额外添加的一个配置,该配置的目的是当 mlx.artemis.enable 值为 true的时候才启动消息队列的服务,否则不启动消息队列的服务
spring:
artemis:
host: 127.0.0.1
port: 6161
embedded:
enable: true
jms:
pub-sub-domain: true
mlx:
artemis:
enable: true
3.artemis配置类 : mlx.artemis.enable 值为 true时才将artemis配置类引入IOC容器;
package mlx.boot.artemis.service;
import org.apache.activemq.artemis.api.core.SimpleString;
import org.apache.activemq.artemis.core.config.impl.ConfigurationImpl;
import org.apache.activemq.artemis.core.server.ActiveMQServer;
import org.apache.activemq.artemis.core.server.impl.ActiveMQServerImpl;
import org.apache.activemq.artemis.core.settings.impl.AddressSettings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jms.artemis.ArtemisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConditionalOnProperty(prefix = "mlx.artemis",name = "enable", havingValue = "true")
public class ArtemisConfig {
@Autowired
ArtemisProperties artemisProperties;
@Bean
public ActiveMQServer createActiveMQServer() throws Exception {
ConfigurationImpl configuration = new ConfigurationImpl();
// 关闭安全功能,否则客户端无法创建session会话对象
configuration.setSecurityEnabled(false);
// 关闭持久化功能
configuration.setPersistenceEnabled(true);
AddressSettings addressSettings = new AddressSettings();
// 设置死信地址与延迟地址
addressSettings.setExpiryAddress(SimpleString.toSimpleString("expiry.address"));
addressSettings.setDeadLetterAddress(SimpleString.toSimpleString("dead.letter.address"));
// 如果添加了一个queue, 为这个queue绑定一个死信地址与延迟地址的配置
configuration.addAddressesSetting("top1",addressSettings);
configuration.addAddressesSetting("com.mlx.cn",addressSettings);
configuration.addAddressesSetting("mlx.mlx.cn",addressSettings);
configuration.addAddressesSetting("*.mlx.cn",addressSettings);
configuration.addAddressesSetting("com.mlx.com",addressSettings);
configuration.addAcceptorConfiguration("tcp", "tcp://"+artemisProperties.getHost()+":" + artemisProperties.getPort());
ActiveMQServer server = new ActiveMQServerImpl(configuration);
return server;
}
}
4.artemis的消息消费者:消息消费者在初始化时启动artemis服务,紧接着才创建消费者(@JmsListener 注解的方法)
package mlx.boot.artemis.service;
import org.apache.activemq.artemis.core.server.ActiveMQServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@ConditionalOnBean(ArtemisConfig.class)
@Component
public class ArtemisConsumer {
@Autowired
ActiveMQServer activeMQServer;
@JmsListener(destination = "com.mlx.cn")
public void myQueue1(String msg) {
System.out.println("myQueue1:" + msg);
}
@JmsListener(destination = "mlx.mlx.cn")
public void myQueue2(String msg) {
System.out.println("myQueue2:" + msg);
}
@JmsListener(destination = "*.mlx.cn")
public void myQueue3(String msg) {
System.out.println("myQueue3:" + msg);
}
@JmsListener(destination = "com.mlx.com")
public void myQueue4(String msg) {
System.out.println("myQueue4:" + msg);
}
@JmsListener(destination = "top1")
public void myQueue5(String msg) {
System.out.println("myQueue5:" + msg);
}
@JmsListener(destination = "top1")
public void myQueue6(String msg) {
System.out.println("myQueue6:" + msg);
}
@PostConstruct
public void init() throws Exception {
activeMQServer.start();
}
}
5.测试类:为了方便将测试方法写在启动类中了
@EnableJms
@RestController
@SpringBootApplication
public class ArtemisApplication {
public static void main(String[] args){
SpringApplication.run(ArtemisApplication.class, args);
}
@Autowired
JmsTemplate jmsTemplate;
// http://localhost:9876/test?adr=com.mlx.cn&msg=111
@RequestMapping(value = "/test")
public String test(String adr, String msg){
jmsTemplate.convertAndSend(adr, msg);
return "向【+"+ adr +"+】发送消息:【"+ msg +"】";
}
}