SpringBoot整合JMS
JMS介绍
Java消息服务(Java Message Service,JMS)应用程序接口是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送消息,进行异步通信。JMS是一个与具体平台无关的API。
maven配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-broker</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
</dependency>
springboot启动类
启动类增加**@EnableJms**即可
@EnableJms
public class CetiServiceApplication {
public static void main(String[] args) {
SpringApplication.run(CetiServiceApplication.class, args);
}
@Bean
public JmsListenerContainerFactory<?> myFactory(ConnectionFactory connectionFactory,
DefaultJmsListenerContainerFactoryConfigurer configurer) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
configurer.configure(factory, connectionFactory);
return factory;
}
}
消费者
public class MeterProvisioningRequestConsumer {
private static Logger logger = LoggerFactory.getLogger(MeterProvisioningRequestConsumer.class);
@JmsListener(destination = "TEST_QUEUE", containerFactory = "myFactory")
public void receiveFluviusMessage(String message) {
log.info("message= {}", message);
}
}
生产者
public class Producer {
public void sendMessage(Destination destination, String message) {
jmsTemplate.convertAndSend(destination, message);
}
@Autowired
private JmsTemplate jmsTemplate;
}
测试
public class TestController {
@GetMapping("/send")
public void sender() {
producer.sendMessage(new ActiveMQQueue("TEST_QUEUE"), "this is test message");
}
@Autowired
private Producer producer;
}
properties文件
spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.user=admin
spring.activemq.password=admin
spring.activemq.pool.enabled=true
spring.activemq.pool.max-connections=100
#集群配置(后续需要在配上)
#spring.activemq.broker-url=failover:(tcp://localhost:61616,tcp://localhost:61617)
#消息队列默认是点对点的,如果需要发布/订阅模式那么需要加上下面注解(如果同时需要点对点发布订阅这里也需注释掉)
# spring.jms.pub-sub-domain=true
Active MQ的安全机制
JAAS authentication plug-in:实现了JAAS API,提供了一个更强大的和可定制的权限方案
进入activemq的配置目录:
1、修改login.config
activemq {
org.apache.activemq.jaas.PropertiesLoginModule required
org.apache.activemq.jaas.properties.user="users.properties"
org.apache.activemq.jaas.properties.group="groups.properties"
reload=true;
};
2、修改users.properties,增加对应的用户和密码
3、修改groups.properties,增加对应的组信息
4、修改activemq.xml,配置***目的地级别***授权
JMS目的地的三种操作级别:
read:读取目的地消息权限 write:发送消息到目的地权限 admin:管理目的地的权限
<plugins>
<!-- 值是在login.conf中定义的 -->
<jaasAuthenticationPlugin configuration="activemq" />
<authorizationPlugin>
<map>
<authorizationMap>
<authorizationEntries>
<!-- ">" 为通配符,指定了具体的Topic/Queue与用户组的授权关系,一个topic对应一个queue,不能少 -->
<authorizationEntry queue=">" read="admin" write="admin" admin="admin" />
<authorizationEntry topic=">" read="admin" write="admin" admin="admin" />
<!-- 这个是必须的配置,不能少 -->
<authorizationEntry queue="ActiveMQ.Advisory.>" read="admin" write="admin" admin="admin"/>
<authorizationEntry topic="ActiveMQ.Advisory.>" read="admin" write="admin" admin="admin"/>
<!-- 下面定义具体的权限 -->
</authorizationEntries>
</authorizationMap>
</map>
</authorizationPlugin>
</plugins>
通配符:
遗留问题:
项目所使用的QUEUE名字是用“_”作为分隔符,activemq默认使用“.”作为分隔符。
activemq官网提供了修改别名的配置;
<plugins>
.....
<!-- A default path separator this plugin will use is /.
You can customize it further using pathSeparator property. -->
<destinationPathSeparatorPlugin/>
</plugins>
想采用“ _”作为分隔符,采用如下配置,“ _”分隔符没有生效
<plugins>
.....
<destinationPathSeparatorPlugin pathSeparator = "_" />
</plugins>