Spring Boot2学习笔记--activemq

摘要

看完本文你将掌握如下知识点:

  • Spring Boot(2.1.1.RELEASE)中使用activemq(5.15.8)的方法

引入依赖




     org.springframework.boot
     spring-boot-starter-activemq
 


 
     org.apache.activemq
     activemq-pool
 

 
 
     org.messaginghub
     pooled-jms
     1.0.3
 

配置文件

spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.user=admin
spring.activemq.password=admin
#启用连接池
spring.activemq.pool.enabled=true
#最大连接数
spring.activemq.pool.max-connections=100

配置类

@Configuration
@EnableJms //启用jms功能
public class ActiveMqConfig {

    //如果要使用topic类型的消息,则需要配置该bean
    @Bean("jmsTopicListenerContainerFactory")
    public JmsListenerContainerFactory jmsTopicListenerContainerFactory(
            ConnectionFactory connectionFactory
    ){
        DefaultJmsListenerContainerFactory factory
                = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setPubSubDomain(true); //这里必须设置为true,false则表示是queue类型
        return factory;
    }


    @Bean("springboot.queue")
    public Queue queue() {
        return new ActiveMQQueue("springboot.queue") ;
    }

    @Bean("springboot.topic")
    public Topic topic() {
        return new ActiveMQTopic("springboot.topic") ;
    }

    @Bean("springboot.queuereply")
    public Queue queuereply() {
        return new ActiveMQQueue("springboot.queuereply") ;
    }


}

消费者

@Component
public class Consumer {
    //监听队列,queue类型
    @JmsListener(destination="springboot.queue")
    public void receiveQueue(String text){
        System.out.println(this.getClass().getName()+ "收到的报文为:"+text);
    }

    //监听队列,topic类型,这里containerFactory要配置为jmsTopicListenerContainerFactory
    @JmsListener(destination = "springboot.topic",
            containerFactory = "jmsTopicListenerContainerFactory"
    )
    public void receiveTopic(String text) {
        System.out.println(this.getClass().getName()+" 收到的报文为:"+text);
    }


    @JmsListener(destination="springboot.queuereply")
    @SendTo("out.replyTo.queue") //消费者应答后通知生产者
    public String receiveQueueReply(String text){
        System.out.println(this.getClass().getName()+ "收到的报文为:"+text);
        return "out.replyTo.queue receiveQueueReply";
    }

}

生产者

@Component
public class Producer {

    @Resource("springboot.queue")
    private Queue queue;

    @Resource("springboot.topic")
    private Topic topic;

    @Resource("springboot.queuereply")
    private Queue queuereply;

    @Autowired
    private JmsMessagingTemplate jmsTemplate;

    // 发送消息,destination是发送到的队列,message是待发送的消息
    public void sendMessage(Destination destination, final String message){
        jmsTemplate.convertAndSend(destination, message);
    }

    public void sendQueueMessage(final String message){
        sendMessage(queue, message);
    }

    public void sendTopicMessage(final String message){
        sendMessage(topic, message);
    }

    public void sendQueueMessageReply(final String message){
        sendMessage(queuereply, message);
    }

    //生产者监听消费者的应答
    @JmsListener(destination = "out.replyTo.queue")
    public void consumerMessage(String text){
        System.out.println("从out.replyTo.queue收到报文"+text);
    }
}

你可能感兴趣的:(Spring Boot2学习笔记--activemq)