spring boot集成Active MQ

1. pom.xml

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

2. application.properties

spring.activemq.broker-url=tcp://127.0.0.1:61616
spring.activemq.in-memory=true
spring.activemq.pool.enabled=false
spring.jms.pub-sub-domain=true

3. JmsConfig.java

同时支持Queue和Topic

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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;

@EnableJms
@Configuration
public class JmsConfig {

	@Bean
    public JmsListenerContainerFactory<?> queueListenerFactory(ConnectionFactory connectionFactory) {
    	  DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
    	  factory.setPubSubDomain(false); // 为true 则是 topic 不写则为queue
	      factory.setConnectionFactory(connectionFactory);
    	  return factory;
       }
    
	@Bean
	public JmsListenerContainerFactory<?> topicListenerFactory(ConnectionFactory connectionFactory) {
		DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
		factory.setPubSubDomain(true); // 为true 则是 topic 不写则为queue
		factory.setConnectionFactory(connectionFactory);
		return factory;
	}
    
	@Bean(name = "newsQueue")
	public Queue newsQueue() {
		return new ActiveMQQueue("newsQueue");
	}

	@Bean(name = "newsTopic")
	public Topic newsTopic() {
		return new ActiveMQTopic("newsTopic");
	}
	
}

4. 发送消息

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

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

@Component
public class Producer {

	@Autowired
	private JmsMessagingTemplate jmsMsgTemplate;

	@Autowired
	private Queue newsQueue;

	@Autowired
	private Topic newsTopic;
	
	public void sendNewsQueue(String message) {
		System.out.println("开始发送MQ:" + message);
		jmsMsgTemplate.convertAndSend(newsQueue, message);
	}
	
	public void sendStockTopic(String message) {
		System.out.println("开始发送Topic:" + message);
		jmsMsgTemplate.convertAndSend(stockTopic, message);
	}

}

5. 消费queue

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import com.qinniu.push.web.news.service.NewsService;

@Component
public class NewsConsumer {

	@Autowired
	NewsService newsService;
	
	@JmsListener(destination = "newsQueue", containerFactory="queueListenerFactory")
	public void receiveNewsQueue(String message) {
		System.out.println("BigNewsConsumer接收了news queue:" + message);
	}
	
}

6. 消费topic

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

@Component
public class Topic1Consumer {

	@JmsListener(destination = "newsTopic", containerFactory="topicListenerFactory")
	public void receiveStockTopic(String message) {
		System.out.println("接收了topic:" + message);
	}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值