activemq的配置文件及依赖
下面展示一些 内联代码片
。
// An highlighted block
@Configuration
public class ActiveMQConfig {
@Value("${spring.activemq.broker-url}")
private String brokerUrl;
@Value("${spring.activemq.user}")
private String username;
@Value("${spring.activemq.password}")
private String password;
@Bean
public ConnectionFactory connectionFactory(){
return new ActiveMQConnectionFactory(username, password, brokerUrl);
}
@Bean
public JmsMessagingTemplate jmsMessageTemplate(){
return new JmsMessagingTemplate(connectionFactory());
}
// 在Queue模式中,对消息的监听需要对containerFactory进行配置
@Bean("queueListener")
public JmsListenerContainerFactory<?> queueJmsListenerContainerFactory(ConnectionFactory connectionFactory){
SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setPubSubDomain(false);
return factory;
}
//在Topic模式中,对消息的监听需要对containerFactory进行配置
@Bean("topicListener")
public JmsListenerContainerFactory<?> topicJmsListenerContainerFactory(ConnectionFactory connectionFactory){
SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setPubSubDomain(true);
return factory;
}
}
//使用
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
//调用队列
//将参数放入队列
Destination destination = new ActiveMQQueue("test-crm-hhcd-topic-video");
jmsMessagingTemplate.convertAndSend(destination,JSONObject.toJSONString(contact));
//消费者配置,方法上添加注解
@JmsListener(destination="test-crm-hhcd-topic-video", containerFactory="queueListener")
<!--ActiveMq-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
<version>1.5.0.RELEASE</version>
</dependency>
<!--消息队列连接池-->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>5.15.0</version>
</dependency>
spring配置文件
spring:
activemq:
broker-url: tcp://31.91.151.71:2861
user: hl1r
password: gb13
#broker-url: tcp://11:2816
#broker-url: tcp://31.78116
#user: hluser
#password: gbuser.123
# broker-url: tcp://10.1191:61616
# user: admin
# password: admin
close-timeout: 15s # 在考虑结束之前等待的时间
in-memory: true # 默认代理URL是否应该在内存中。如果指定了显式代理,则忽略此值。
non-blocking-redelivery: false # 是否在回滚回滚消息之前停止消息传递。这意味着当启用此命令时,消息顺序不会被保留。
send-timeout: 0 # 等待消息发送响应的时间。设置为0等待永远。
# packages:
# trust-all: true #不配置此项,会报错
jms:
Listener:
# 默认开启多少个消费者
concurrency: 5
#在这里消费者是可以随动的。最大配置消费者并行最大数量
max-concurrency: 5
pool:
enabled: true
max-connections: 10 #连接池最大连接数
idle-timeout: 30000 #空闲的连接过期时间,默认为30秒