Spring boot 集成 ActiveMQ

pom.xml中导入依赖

<!-- ActiveMQ的启动器 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-activemq</artifactId>
    </dependency>

在application.properties中添加mq的配置

# MQ所在的服务器的地址
spring.activemq.broker-url=tcp://127.0.0.1:61616
# 是否使用内置的MQ, true  使用; fale  不使用
spring.activemq.in-memory=false 
# 是否在回滚回滚消息之前停止消息传递。这意味着当启用此命令时,消息顺序不会被保留。
spring.activemq.non-blocking-redelivery=false
# 用户名
spring.activemq.password=admin
# 密码
spring.activemq.user=admin

在你需要编写生产者和消费者的系统中都要创建你的ActiveMQConfig

package com.czxy.config;
import javax.jms.Queue;
import javax.jms.Topic;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ActiveMQConfig {
	@Bean
	public Queue queue() {
		 return new ActiveMQQueue("czxy.queue");
	}
	@Bean
	public Topic topic() {
		return new ActiveMQTopic("czxy.topic");
	}
}

消息的生产者:

/**
  * 消息的生产者 
  * @author Administrator
  *
  */
@Componet
@EnableScheduling
public class QueueProducer {  
	/*
	 * @Autowired // 也可以注入JmsTemplate,JmsMessagingTemplate对JmsTemplate进行了封装
	 * private JmsMessagingTemplate jmsTemplate; //
	 * 发送消息,destination是发送到的队列,message是待发送的消息
	 * 
	 * @Scheduled(fixedDelay=3000)//3s执行1 
	   public void sendMessage(Destination destination, final String message){
	      jmsTemplate.convertAndSend(destination, message); 
	   }
	 */

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    private Queue queue;

    @Scheduled(fixedDelay=3000)//3s执行1
    public void send() {
       try {
		
		   MapMessage mapMessage = new ActiveMQMapMessage();
		   mapMessage.setString("info", "你还在睡觉");
		   
		   this.jmsMessagingTemplate.convertAndSend(this.queue, mapMessage);
		   
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
}  

消息的消费者:

/**
 * 消息的消费者
 * @author Administrator
 */
@Component  
public class QueueConsumer {  
    //使用JmsListener配置消费者监听的队列,其中Message是接收到的消息  
	@JmsListener(destination = "czxy.queue")  
    public void receiveQueue(Message message) {
		try {
			MapMessage mapMessage = (MapMessage) message;
			String info = mapMessage.getString("info");
			System.out.println(info);
		} catch (Exception e) {
			e.printStackTrace();
		}
    } 
}

在这里插入图片描述

启动,测试,ok
需要注意事项
根据需要创建一个ActiveMQ的生成者,在创建生产者的项目内创建一个ActiveMQconfig在config中定义好ActiveMQ的队列名
在需要接受的地方创建一个消费者,这个消费者的名字一定要根据对应生产者config的对列名,
需要注意事项:创建消费者的监听类的时候一定要记得在类名上添加@Component注释以启动监听状态,否则是找不到消费者的客要讲的就是怎么采用外置的ActiveMQ,

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值