spring boot 集成 activemq

一、安装activemq

点击进入activemq官方下载地址:http://activemq.apache.org/activemq-5157-release.html

根据你的操作系统选择不同的文件下载到本地,解压后进入bin 目录,执行./activemq  start命令启动activemq,然后在地址栏输入:http://127.0.0.1:8161 进入activemq网页

 

点击Manage ActiveMQ broker 登录,用户名密码默认都是admin,登录后进入首页如下

二、编码实现

1、在pom.xml中添加activemq的maven依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-activemq</artifactId>
</dependency>

<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-pool</artifactId>
</dependency>

2、在application.properties文件中添加activemq的配置信息

#activemq
spring.activemq.user=admin
spring.activemq.password=admin
spring.activemq.broker-url=tcp://127.0.0.1:61616
spring.activemq.pool.enabled=true
spring.activemq.pool.max-connections=10

#activemq 自定义信息
self.activemq.queueName=ldy-test-queue
self.activemq.topicName=ldy-test-topic

3、编写配置类用来装载自定义的activemq信息:ActiveMQProperties.java

package com.ldy.bootv2.demo.jms;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix="self.activemq")
public class ActiveMQProperties {
	
	//消息队列名称
	private String queueName;
	
	//通知主题名称
	private String topicName;

	public String getQueueName() {
		return queueName;
	}

	public void setQueueName(String queueName) {
		this.queueName = queueName;
	}

	public String getTopicName() {
		return topicName;
	}

	public void setTopicName(String topicName) {
		this.topicName = topicName;
	}

}

4、配置activemq消息队列和通知主题:ActiveMQConfig.java,跟启动类放在同一层级目录下

package com.ldy.bootv2.demo;

import javax.jms.ConnectionFactory;
import javax.jms.Queue;
import javax.jms.Topic;

import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;

import com.ldy.bootv2.demo.jms.ActiveMQProperties;

@Configuration
public class ActiveMQConfig {

	@Autowired
	private ActiveMQProperties activeMQProperties;

	/**
	 * 消息队列
	 */
	@Bean
	public Queue myQueue() {
		Queue queue = new ActiveMQQueue(activeMQProperties.getQueueName());
		return queue;
	}

	/**
	 * 通知主题
	 */
	@Bean
	public Topic myTopic() {
		Topic t = new ActiveMQTopic(activeMQProperties.getTopicName());
		return t;
	}

	/**
	 * 通知设置
	 */
	@Bean
	public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ConnectionFactory connectionFactory, Topic myTopic) {
		DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
		bean.setPubSubDomain(true);// 设置为发布订阅方式, 默认情况下使用的生产消费者方式
		bean.setConnectionFactory(connectionFactory);
		return bean;
	}
	

}

消息队列和通知主题的区别:

消息队列里面同一个内容只能被一个消费者消费,如有a、b两个消费者,如果消息队列里面有一个消息,则该消息只能被a和b中的一个消费者消费,另外一个消费者接收不到该消息;通知则会推送给所有的消费者,如果发送的是通知,那么所有监听该通知的消费者都会收到同样的信息。

5、生产者代码:ActiveMQProducer.java

package com.ldy.bootv2.demo.jms;

import java.util.Date;

import javax.jms.Queue;
import javax.jms.Topic;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class ActiveMQProducer {
	
	/**
	 * 消息队列
	 */
	@Autowired
	private Queue myQueue;
	
	/**
	 * 通知主题
	 */
	@Autowired
	private Topic myTopic;

	@Autowired
	private JmsMessagingTemplate jmsTemplate;
	
	/**
         * 测试消息,5秒发送一次<br>
         */
        @Scheduled(fixedRate = 1000*5)
	public void execute1() {
    		String msg = "ldy的消息 "+ new Date().getTime();
    		this.sendMessage2Queue(msg);
        }
    
        /**
         * 测试通知,10秒发送一次<br>
         */
        @Scheduled(fixedRate = 1000*10)
	public void execute2() {
    		String msg = "ldy的通知 "+ new Date().getTime();
    		this.sendMessage2Topic(msg);
        }
    

	/** 
	 * 发送消息到队列
	 */
	public void sendMessage2Queue(final String message) {
		jmsTemplate.convertAndSend(myQueue, message);
		System.out.println("Producer发送的消息为:" + message);
	}
	
	/** 
	 * 发送通知到主题
	 */
	public void sendMessage2Topic(final String message) {
		jmsTemplate.convertAndSend(myTopic, message);
		System.out.println("Producer发送的通知>>>>>>>>>>>>>>>>>>>>" + message);
	}

}

注意,测试方法上的@Scheduled注解需要开启任务调度才可以生效,你也可以通过其他方法测试。

在入口类上添加@EnableScheduling开启任务调度,也可以直接在当前类上加该注解

package com.ldy.bootv2.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling
@SpringBootApplication
public class BootV2App {

	public static void main(String[] args) {
		SpringApplication.run(BootV2App.class, args);
	}
}

6、消费者代码:

package com.ldy.bootv2.demo.jms;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

@Component
public class ActiveMQConsumer {

	/**
	 * @方法名: receiveQueue<br>
	 * @描述: 接收消息<br>
	 * @param message
	 */
	@JmsListener(destination = "${self.activemq.queueName}", concurrency="10")
	public void receiveQueue(String message) {
		System.out.println("Consumer收到的消息为:" + message);
	}
	
	/**
	 * @方法名: receiveTopic<br>
	 * @描述: 接收通知<br>
	 * @param message
	 */
	@JmsListener(destination = "${self.activemq.topicName}", containerFactory = "jmsListenerContainerTopic")
	public void receiveTopic(String message) {
		System.out.println("Consumer收到的通知>>>>>>>>>>>>>>>>>>>>" + message);
	}

}

7、启动项目查看运行效果如图

源码下载地址:https://pan.baidu.com/s/1Z771VDiuabDBJJV445xLeA#list/path=%2Fspring%20boot%202.x%20%E4%BB%A3%E7%A0%81

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值