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);
}
}