package com.example.provider.config;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Value;
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;
import javax.jms.Queue;
import javax.jms.Topic;
/**
* @author shenzm
* @date 2019-2-27
* @description 作用
*/
@Configuration
@EnableJms
public class ActivemqConfig {
@Value("${spring.activemq.queue-name}")
private String queueName;
@Value("${spring.activemq.topic-name}")
private String topicName;
@Value("${spring.activemq.user}")
private String userName;
@Value("${spring.activemq.password}")
private String password;
@Value("${spring.activemq.broker-url}")
private String brokerUrl;
@Bean
public Queue queue(){
return new ActiveMQQueue(queueName);
}
@Bean
public Topic topic(){
return new ActiveMQTopic(topicName);
}
@Bean
public ActiveMQConnectionFactory connectionFactory() {
return new ActiveMQConnectionFactory(userName, password, brokerUrl);
}
@Bean
public JmsListenerContainerFactory<?> jmsListenerContainerQueue(ActiveMQConnectionFactory connectionFactory){
DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
bean.setConnectionFactory(connectionFactory);
return bean;
}
@Bean
public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ActiveMQConnectionFactory connectionFactory){
DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
//设置为发布订阅方式, 默认情况下使用的生产消费者方式
bean.setPubSubDomain(true);
bean.setConnectionFactory(connectionFactory);
bean.setRecoveryInterval(1000L);
return bean;
}
}
package com.example.provider.config;
/**
* @author shenzm
* @date 2019-2-27
* @description 作用
*/
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Component;
@Component
public class QueueListener {
private static final Logger logger = LoggerFactory.getLogger(QueueListener.class);
@JmsListener(destination = "${spring.activemq.queue-name}", containerFactory = "jmsListenerContainerQueue")
@SendTo("${spring.activemq.out-queue-name}")
public String receive(String text){
logger.info("QueueListener: consumer-a 收到一条信息: " + text);
return "consumer-a received : " + text;
}
}
package com.example.provider.config;
/**
* @author shenzm
* @date 2019-2-27
* @description 作用
*/
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
@Component
public class TopicListener {
private static final Logger logger = LoggerFactory.getLogger(QueueListener.class);
@JmsListener(destination = "${spring.activemq.topic-name}", containerFactory = "jmsListenerContainerTopic")
public void receive(String text){
logger.info("QueueListener: consumer-a 收到一条信息: " + text);
}
}
pom.xml , application.yml, 差不多 + ActivemqConfig (一样的)
http://localhost:9091/publish/topic 发送topic 消息, 在消费端有topic 消息收到
http://localhost:9091/publish/queue 发送的是queue消息, 并且会给生产端一个反馈的消息(相当于一个confrim, 消息确认)
https://github.com/ninuxGithub/spring-boot-dubbo-zookeeper
https://www.cnblogs.com/elvinle/p/8457596.html
https://www.cnblogs.com/Alex-zqzy/p/9558857.html