activemq整合springboot使用(个人微信小程序用)

1.引入依赖

<parent>
<groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.3.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-activemq</artifactId>
</dependency>

2.修改配置文件(其实配置也是默认值,不配置也可以)

spring.activemq.in-memory=true
spring.activemq.pool.enabled=false

3.添加ActiveMq连接池(如果不开启连接池,则每发送一条数据创建一个连接)

①.添加依赖
<dependency>
     <groupId>org.apache.activemq</groupId>
     <artifactId>activemq-pool</artifactId>
</dependency>
②.修改配置文件
#服务端口,8080被另一服务占用
server.port=9090
spring.activemq.broker-url=tcp://127.0.0.1:61616
# 在考虑结束之前等待的时间
#spring.activemq.close-timeout=15s
# 默认代理URL是否应该在内存中。如果指定了显式代理,则忽略此值。
spring.activemq.in-memory=true 
# 是否在回滚回滚消息之前停止消息传递。这意味着当启用此命令时,消息顺序不会被保留。
spring.activemq.non-blocking-redelivery=false
# 等待消息发送响应的时间。设置为0等待永远。
spring.activemq.send-timeout=0
#默认情况下activemq提供的是queue模式,若要使用topic模式需要配置下面配置
spring.jms.pub-sub-domain=true
#账号
spring.activemq.user=admin
# 密码
spring.activemq.password=admin
# 是否信任所有包
#spring.activemq.packages.trust-all=
# 要信任的特定包的逗号分隔列表(当不信任所有包时)
#spring.activemq.packages.trusted=
# 当连接请求和池满时是否阻塞。设置false会抛“JMSException异常”。
#spring.activemq.pool.block-if-full=true
# 如果池仍然满,则在抛出异常前阻塞时间。
#spring.activemq.pool.block-if-full-timeout=-1ms
# 是否在启动时创建连接。可以在启动时用于加热池。
#spring.activemq.pool.create-connection-on-startup=true
# 是否用Pooledconnectionfactory代替普通的ConnectionFactory。
#spring.activemq.pool.enabled=false
# 连接过期超时。
#spring.activemq.pool.expiry-timeout=0ms
# 连接空闲超时
#spring.activemq.pool.idle-timeout=30s
# 连接池最大连接数
#spring.activemq.pool.max-connections=1
# 每个连接的有效会话的最大数目。
#spring.activemq.pool.maximum-active-session-per-connection=500
# 当有"JMSException"时尝试重新连接
#spring.activemq.pool.reconnect-on-exception=true
# 在空闲连接清除线程之间运行的时间。当为负数时,没有空闲连接驱逐线程运行。
#spring.activemq.pool.time-between-expiration-check=-1ms
# 是否只使用一个MessageProducer
#spring.activemq.pool.use-anonymous-producers=true

4.添加JMS相关配置

①.开启JMS扫描注解:@EnableJms 相当于application.xml中的<jms:annotation-d riven/>
②.配置Queue类:
@Bean
public Queue queue() {
    return new ActiveMQQueue("queueName1");
}
③.创建生产者:
@Resource
JmsMessagingTemplate jmsMessagingTemplate;
public void sendMessage(Destination destination, String message) {
    jmsMessagingTemplate.convertAndSend(destination, message);
}
④.创建消费者:
@JmsListener(destination = "queueName1")
public void receiveQueue(String message) {
    log.info("=========接受到了消息:" + message);
    grabService.addSearchContent(message, mainConfig.getCharset());
}

ps:@JmsListener(destination = "queueName1")注解用于监听指定名称的消息
参数message代表具体的消息

猜你喜欢

转载自www.cnblogs.com/zhangsansan/p/10597569.html