如何在SpringBoot中整合ActiveMQ
这篇文章将为大家详细讲解有关如何在SpringBoot中整合ActiveMQ,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于成都网站制作、成都网站建设、迎江网络推广、小程序定制开发、迎江网络营销、迎江企业策划、迎江品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联公司为所有大学生创业者提供迎江建站搭建服务,24小时服务热线:18982081108,官方网址:www.cdcxhl.com
目录结构

引入 maven依赖
org.springframework.boot spring-boot-starter-parent 1.5.4.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-activemq org.springframework.boot spring-boot-maven-plugin
引入 application.yml配置
spring: activemq: broker-url: tcp://127.0.0.1:61616 user: admin password: admin queue: springboot-queue server: port: 8080
创建QueueConfig
@Configuration
public class QueueConfig {
@Value("${queue}")
private String queue;
@Bean
public Queue logQueue() {
return new ActiveMQQueue(queue);
}
@Bean
public JmsTemplate jmsTemplate(ActiveMQConnectionFactory activeMQConnectionFactory, Queue queue) {
JmsTemplate jmsTemplate = new JmsTemplate();
jmsTemplate.setDeliveryMode(2);// 进行持久化配置 1表示非持久化,2表示持久化
jmsTemplate.setConnectionFactory(activeMQConnectionFactory);
jmsTemplate.setDefaultDestination(queue); // 此处可不设置默认,在发送消息时也可设置队列
jmsTemplate.setSessionAcknowledgeMode(4);// 客户端签收模式
return jmsTemplate;
}
// 定义一个消息监听器连接工厂,这里定义的是点对点模式的监听器连接工厂
@Bean(name = "jmsQueueListener")
public DefaultJmsListenerContainerFactory jmsQueueListenerContainerFactory(
ActiveMQConnectionFactory activeMQConnectionFactory) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setConnectionFactory(activeMQConnectionFactory);
// 设置连接数
factory.setConcurrency("1-10");
// 重连间隔时间
factory.setRecoveryInterval(1000L);
factory.setSessionAcknowledgeMode(4);
return factory;
}
}创建生产者:
@SpringBootApplication
@Component
@EnableScheduling
public class Producer {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private Queue queue;
@Scheduled(fixedDelay=3000)
public void send() {
String result = System.currentTimeMillis()+"---测试";
System.out.println("result"+result);
jmsMessagingTemplate.convertAndSend(queue,result);
}
public static void main(String[] args) {
SpringApplication.run(Producer.class, args);
}
}创建消费者的application.yml
spring: activemq: broker-url: tcp://127.0.0.1:61616 user: admin password: admin queue: springboot-queue server: port: 8081
创建消费者:
@Component
@SpringBootApplication
public class consumer {
private int count =0;
@JmsListener(destination = "${queue}")
public void receive(TextMessage textMessage,Session session) throws JMSException {
String text = textMessage.getText();
System.out.println("消费:"+text+"第几次获取消息count:"+(++count));
System.out.println();
String jmsMessageID = textMessage.getJMSMessageID();
}
public static void main(String[] args) {
SpringApplication.run(consumer.class,args);
}
}结果显示:

关于如何在SpringBoot中整合ActiveMQ就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
当前文章:如何在SpringBoot中整合ActiveMQ
标题来源:http://www.jxjierui.cn/article/ghgchg.html


咨询
建站咨询
