springboot与常用消息中间件的简单使用

springboot与常用消息中间件的简单使用

activemq

activemq的安装很容易,直接去官网下载zip包解压即可http://activemq.apache.org/。windows下进入bin目录的win64目录,以管理员运行InstallService.bat就可以通过服务的方式运行mq。
pom需要引入的依赖如下

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

        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-pool</artifactId>
            <!-- <version>5.7.0</version> -->
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

配置文件如下,比较简单。

#broker的url
spring.activemq.broker-url=tcp://localhost:61616
#是否是内存模式
spring.activemq.in-memory=true
#是否使用连接池
spring.activemq.pool.enabled=false
#设置为true为发布订阅模式,默认为点对点模式
spring.jms.pub-sub-domain=false

配置类如下,主要创建队列和主题的bean

package org.mq.activemq;

import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.jms.Queue;
import javax.jms.Topic;

@Configuration
public class MqConfig {
   

    //创建队列bean
    @Bean
    public Queue queue(){
   
        return new ActiveMQQueue("testqueue");
    }

    //创建主题bean
    @Bean
    public Topic topic(){
   
        return new ActiveMQTopic("testtopic1");
    }
}

生产者如下

package org.mq.activemq;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;

import javax.jms.Queue;
import javax.jms.Topic;

@Service
public class Producter {
   

    //注入队列和主题对象
    @Autowired
    private Queue queue;

    @Autowired
    private Topic topic;
    
    //通过JmsMessagingTemplate发送消息
    @Autowired
    private JmsMessagingTemplate jmsTemplate;
    // 发送消息到队列
    public void sendQueueMessage(final String message){
   
        jmsTemplate.convertAndSend(queue, message);
    }

    // 发送消息到主题
    public void sendTopicMessage(final String message){
   
        jmsTemplate.convertAndSend(
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值