SpringBoot整合ActiveMQ和开启持久化(2)

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.annotation.JmsListeners;

import javax.jms.JMSException;
import javax.jms.TextMessage;

@SpringBootApplication
public class StartP2PConsumer {
    public static void main(String[] args) {
        SpringApplication.run(StartP2PConsumer.class,args);
    }


    //消费者消费
    @JmsListener(destination = "SpringBoot_Queue")
    public void getMessage(TextMessage message) throws JMSException {
        System.out.println("消费者获取到消息:"+message.getText());
    }
}

11、启动提供者并访问

  

SpringBoot整合ActiveMQ和开启持久化

  

SpringBoot整合ActiveMQ和开启持久化

12、启动消费者

  

SpringBoot整合ActiveMQ和开启持久化

  

SpringBoot整合ActiveMQ和开启持久化

二、发布订阅 1、消费者目录展示

  

SpringBoot整合ActiveMQ和开启持久化

2、导入依赖

  

SpringBoot整合ActiveMQ和开启持久化

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <!-- spring boot web支持:mvc,aop... -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-activemq</artifactId>
    </dependency>

3、消费者application.yml配置文件

  

SpringBoot整合ActiveMQ和开启持久化

4、consumer启动类StartTopicConsumer

package com.zn;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;

import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.TextMessage;

@SpringBootApplication
public class StartTopicConsumer {
    public static void main(String[] args) {
        SpringApplication.run(StartTopicConsumer.class,args);
    }


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


    //消费者消费  destination队列或者主题的名字
    @JmsListener(destination = "SpringBoot_Topic",containerFactory = "jmsTopicListenerContainerFactory")
    public void getMessage(TextMessage message) throws JMSException {

System.out.println("消费者获取到消息:"+message.getText());
    }
}

5、提供者目录展示

  

SpringBoot整合ActiveMQ和开启持久化

6、导入依赖 

  

SpringBoot整合ActiveMQ和开启持久化

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/bcbd9f9695596f61d6709b6e8b546c09.html