网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

Springboot中怎么对ActiveMQ进行整合

Springboot中怎么对ActiveMQ进行整合,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

蔡甸网站制作公司哪家好,找创新互联建站!从网页设计、网站建设、微信开发、APP开发、响应式网站建设等网站项目制作,到程序开发,运营维护。创新互联建站2013年至今到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联建站

1、首先新建一个springboot工程(新建过程略),本文springboot版本 是2.1.1.RELEASE

2、在pom.xml文件添加activemq的相关依赖



              

                  org.springframework.boot

                  spring-boot-starter-activemq

              

              

              

                  org.apache.activemq

                  activemq-pool

                 

              

              

              

                  org.messaginghub

                  pooled-jms

              

其中连接池相关的依赖可以不用配置

3、配置application.properties 或者application.yml 本文以application.properties为例

spring.activemq.broker-url=tcp://localhost:61616

spring.activemq.user=admin

spring.activemq.password=admin

#默认情况下activemq提供的是queue模式,若要使用topic模式需要配置下面配置

#spring.jms.pub-sub-domain=true

#true 表示使用内置的MQ,false则连接服务器

spring.activemq.in-memory=false

#true表示使用连接池;false时,每发送一条数据创建一个连接

spring.activemq.pool.enabled=true

#连接池最大连接数

spring.activemq.pool.max-connections=10

#空闲的连接过期时间,默认为30秒

spring.activemq.pool.idle-timeout=15000

spring.activemq.pool.enabled=true时要在pom文件中添加连接池pool相关的依赖,为false时不用添加连接池pool相关的依赖;

若使用连接池pool配置时,注意两种依赖的配置否则启动失败。

工程结构如下图

Springboot中怎么对ActiveMQ进行整合

Demo代码如下

package com.example.acmpp.config;

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;

@Configuration
public class BeanConfig {
	
	//定义存放消息的队列
    @Bean
    public Queue queue() {
        return new ActiveMQQueue("ActiveMQQueue");
    }
    //定义存放消息的队列
    @Bean
    public Topic topic() {
        return new ActiveMQTopic("ActiveMQTopic");
    }
}

消息生产者代码

import javax.jms.Queue;

import javax.jms.Topic;



import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.jms.core.JmsMessagingTemplate;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/*

 *

 * 消息生产者

 */

@RestController

public class ProviderController {

      

       @Autowired

       private JmsMessagingTemplate jmsMessagingTemplate;



       @Autowired

       private Queue queue;



       @Autowired

       private Topic topic;



       /**

         * 消息生产者 Queue模式

        *

        */

       @RequestMapping("/sendQ")

       public void sendQ(String msg) {

              //方法一:添加消息到消息队列

        jmsMessagingTemplate.convertAndSend(queue, msg);

        //方法二:这种方式不需要手动创建queue,系统会自行创建名为test的队列

        //jmsMessagingTemplate.convertAndSend("testQ", msg);

       }

       /**

        * 消息生产者 Topic模式

        * @param msg

        */

       @RequestMapping("/sendT")

       public void sendT(String msg) {

              // 指定消息发送的目的地及内容

              System.out.println("@@@@@@@@@@@@@@" + msg);

              this.jmsMessagingTemplate.convertAndSend(this.topic, msg);

       }
}

消息消费者代码

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.jms.annotation.JmsListener;

import org.springframework.jms.core.JmsMessagingTemplate;

import org.springframework.messaging.handler.annotation.SendTo;

import org.springframework.stereotype.Component;



/**

 * 消息消费者

 * @author FFF

 *

 */

@Component

public class ConsumerService {

       @Autowired

    private JmsMessagingTemplate jmsMessagingTemplate;

      

       /**

        * 消费ActiveMQQueue

        */

    // 使用JmsListener配置消费者监听的队列,其中name是接收到的消息

    @JmsListener(destination = "ActiveMQQueue")

    // SendTo 会将此方法返回的数据, 写入到 OutQueue 中去.

    @SendTo("SQueue")

    public String handleMessage(String name) {

        System.out.println("ActiveMQQueue成功接受Name" + name);

        return "ActiveMQQueue成功接受Name" + name;

    }

    /**

        * 消费ActiveMQ.DLQ

        */

    // 使用JmsListener配置消费者监听的队列,其中name是接收到的消息

    @JmsListener(destination = "ActiveMQ.DLQ")

    public void DLQ(String name) {

        System.out.println("ActiveMQ.DLQ成功接受Name==" + name);

    }

    /**

        * 消费SQueue

        */

    // 使用JmsListener配置消费者监听的队列,其中name是接收到的消息

    @JmsListener(destination = "SQueue")

    public void SQueue(String name) {

        System.out.println("SQueue成功接受Name==" + name);

    }

    /**

        * 消费testQ

        */

    // 使用JmsListener配置消费者监听的队列,其中name是接收到的消息

    @JmsListener(destination = "testQ")

    public void testQMessage(String name) {

        System.out.println("testQ成功接受Name" + name);

    }

    /**

        * 消费topic

        *

        */

    // 使用JmsListener配置消费者监听的队列,其中name是接收到的消息

    @JmsListener(destination = "ActiveMQTopic")

    public void topicMessage(String name) {

        System.out.println("topicMessage成功接受Name" + name);

    }
}

Springboot中怎么对ActiveMQ进行整合

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。


网页题目:Springboot中怎么对ActiveMQ进行整合
文章源于:http://bjjierui.cn/article/jhhghd.html

其他资讯