Spring Boot中引入ActiveMQ

简单示例,演示发送、消费。
ActiveMQ的Broker模式:引入一个Broker组件,解耦客户端和服务端。
ActiveMQ支持两种消息传输方式
1)Queue,队列模式,生产者生产了一个消息,只能由一个消费者进行消费
2)Topic,发布/订阅模式,生产者生产了一个消息,可以由多个消费者进行消费

1、ActiveMQ配置

pom.xml

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

application.properties

spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.user=admin
spring.activemq.password=admin

ActivemqConfig.java

package com.test.demo.config;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
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.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;

@Configuration
public class ActivemqConfig {
	@Bean
	public Destination destQueue() {
		//element="queue" description="An ActiveMQ Queue Destination"
		return new ActiveMQQueue("DestinationName-Jms-String");
	}
	@Bean
	public Destination destTopic() {
		//element="topic" description="An ActiveMQ Topic Destination"
		return new ActiveMQTopic("DestinationName-Jms-Map");
	}

	/**
	 * JmsListener注解默认只接收queue消息,如果要接收topic消息,需要设置containerFactory
	 */
	@Bean
	public JmsListenerContainerFactory<?> topicListenerContainer(
			ConnectionFactory activeMQConnectionFactory) {
		DefaultJmsListenerContainerFactory topicListenerContainer = new DefaultJmsListenerContainerFactory();
		topicListenerContainer.setPubSubDomain(true);
		topicListenerContainer.setConnectionFactory(activeMQConnectionFactory);
		return topicListenerContainer;
	}
}

2、业务模拟

ActiveSender.java消息发送

@Component
public class ActiveSender {
	@Autowired
	private JmsTemplate jmsTemplate;
	
	@Resource(name = "destQueue")
	private Destination destQueue;
	@Resource(name = "destTopic")
	private Destination destTopic;

	public void sendString(String message) {
		//send this message to destinationName 
		jmsTemplate.convertAndSend("DestinationName-Jms-String", message);
		System.out.println("String发送成功["+message+"]");
	}
	
	public void sendMap() {
		Map<String, String> map = new HashMap<String, String>();
		map.put("name", "Tom");
		map.put("age", "35");
		//Destination destTopic = new ActiveMQTopic("DestinationName-Jms-Map");
		jmsTemplate.convertAndSend(destTopic, map);
		System.out.println("HashMap发送成功");
	}

}

ActiveReceiver.java消息接收

@Component
public class ActiveReceiver {
	@JmsListener(destination = "DestinationName-Jms-String")
	public void readMsg(String text) {
		System.out.println("接收到Queue消息:" + text);
	}
	@JmsListener(destination = "DestinationName-Jms-Map", containerFactory = "topicListenerContainer")
	public void readMap(Map<String, String> map) {
		System.out.println("接收到Topic消息:" + map);
	}
}

3、测试

ActiveMQTest.java测试类

@RunWith(SpringRunner.class)
@SpringBootTest
public class ActiveMQTest {
	@Autowired
    private ActiveSender mySender;
	
	@Test
    public void test() throws Exception {
		mySender.sendMap();
		mySender.sendString("fromActiveMQTest");
    }
}

queue

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值