springboot使用activemq

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/woaiqianzhige/article/details/82389039

1.导包

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

2.配置账号密码等

 
 

3.配置类

   3.1 配置目的地,我们用topic做实验

 @Bean
    public ActiveMQTopic depthTop() {
        return new ActiveMQTopic("depthtop");
    }

    3.2配置mq连接工厂

 public ActiveMQConnectionFactory activeMQConnectionFactory() {
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(username, password, brockUrl);
        factory.setMaxThreadPoolSize(30);
        return factory;
    }

    3.3配置spring管理的cache连接工厂

  @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(activeMQConnectionFactory());
        cachingConnectionFactory.setSessionCacheSize(20);
        return cachingConnectionFactory;
    }

    3.4 配置 JmsMessagingTemplate

@Bean
    public JmsMessagingTemplate jmsMessagingTemplate() {
        JmsMessagingTemplate template = new JmsMessagingTemplate(connectionFactory());
        JmsTemplate jmsTemplate = template.getJmsTemplate();
        jmsTemplate.setDeliveryMode(DeliveryMode.NON_PERSISTENT);//非持久模式
        jmsTemplate.setPubSubDomain(true);//订阅模式
        jmsTemplate.setExplicitQosEnabled(true);//启用上面的设置
        return template;
    }

3.5使用  JmsMessagingTemplate

直接在类中 @Resource 引入JmsMessagingTemplate 即可

二,接收消息

JmsListener 注解,注解一个bean,即可接受信息

猜你喜欢

转载自blog.csdn.net/woaiqianzhige/article/details/82389039