SpringBoot整合Artemis

依赖引入

        <dependency>
            <groupId>org.messaginghub</groupId>
            <artifactId>pooled-jms</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-artemis</artifactId>
        </dependency>

application.properties:

spring.artemis.mode=native
spring.artemis.host=你的Artemis服务器IP
spring.artemis.port=61616
spring.artemis.user=root
spring.artemis.password=root
spring.artemis.pool.enabled=true
spring.artemis.pool.max-connections=50
spring.artemis.embedded.enabled=false

 发送消息:面向JmsTemplate编程

package cc.mq;

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

/**
 * @author 张家琛
 * @version 1.0
 * @date 2023/1/22 20:21
 */
@Service
public class MessageService {
    private final JmsTemplate jmsTemplate;

    @Autowired
    public MessageService(JmsTemplate jmsTemplate) {
        this.jmsTemplate = jmsTemplate;
    }

    //p2p模型
    public void produce(String message) {
        this.jmsTemplate.setPubSubDomain(false);
        this.jmsTemplate.convertAndSend("myQueue", message);
    }

    //pub-sub模型
    public void publish(String message) {
        this.jmsTemplate.setPubSubDomain(true);
        this.jmsTemplate.convertAndSend("myTopic", message);
    }
}

接收消息

Spring Boot会自动将@JmsListener注解修饰的方法注册为消息监听器,如果没有显示配置监听器容器工厂,此时Spring Boot会自动配置一个监听器容器工厂。

监听消息队列的监听器

package cc.mq;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

/**
 * @author 张家琛
 * @version 1.0
 * @date 2023/1/22 20:53
 */
@Component
public class QueueListener {
    //表示监听myQueue消息队列
    @JmsListener(destination = "myQueue")
    public void processMessage(String content){
        System.out.println("接收到消息:"+content);
    }
}

Spring Boot的消息监听器总是监听消息队列,监听消息主题得为@JmsListener注解指定containerFactory属性,设置使用自定义的监听器容器工厂。

自定义监听器容器工厂

package cc.mq.config;

import org.springframework.boot.autoconfigure.jms.DefaultJmsListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;

import javax.annotation.Resource;
import javax.jms.ConnectionFactory;

/**
 * @author 张家琛
 * @version 1.0
 * @date 2023/1/22 20:57
 */
@Configuration
public class JmsConfig {
    @Bean
    @Resource
    public DefaultJmsListenerContainerFactory myFactory(DefaultJmsListenerContainerFactoryConfigurer configurer
            , ConnectionFactory connectionFactory) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        //设置成Pub-Sub消息模型!!!
        factory.setPubSubDomain(true);
        return factory;
    }
}

然后再进行配置监听消息主题监听器

package cc.mq;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

/**
 * @author 张家琛
 * @version 1.0
 * @date 2023/1/22 20:56
 */
@Component
public class TopicListener {
    @JmsListener(destination = "myTopic", containerFactory = "myFactory")
    public void processMessage(String content) {
        System.out.println("收到主题消息:" + content);
    }
}

然后启动服务器,对服务器发送请求,我这用的浏览器直接访问

 消息发送出去了

监听器也接收到了

 

 再试试消息主题监听

发送消息:

没问题也成功发送成功了并接收到了 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在Spring Boot整合Artemis,你需要进行以下步骤: 1. 添加依赖:在你的Spring Boot项目的pom.xml文件中,添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-artemis</artifactId> </dependency> ``` 2. 配置Artemis连接:在application.properties(或application.yml)文件中,添加以下配置: ```properties spring.artemis.mode=native spring.artemis.host=<Artemis服务器主机名> spring.artemis.port=<Artemis服务器端口号> spring.artemis.user=<Artemis用户名> spring.artemis.password=<Artemis密码> ``` 确保替换`<Artemis服务器主机名>`,`<Artemis服务器端口号>`,`<Artemis用户名>`和`<Artemis密码>`为实际的Artemis服务器连接信息。 3. 创建Artemis连接工厂:在你的Spring Boot应用程序中,创建一个`ConnectionFactory` bean,用于创建JMS连接。你可以使用`ActiveMQConnectionFactory`类来创建连接工厂实例。 ```java @Bean public ConnectionFactory connectionFactory() { return new ActiveMQConnectionFactory(); } ``` 4. 创建JmsTemplate:创建一个`JmsTemplate` bean,用于发送和接收消息。 ```java @Bean public JmsTemplate jmsTemplate(ConnectionFactory connectionFactory) { JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory); // 配置其他属性,如目的地等 return jmsTemplate; } ``` 5. 发送和接收消息:你可以在你的应用程序中使用`JmsTemplate`来发送和接收消息。例如,发送消息的示例代码如下: ```java @Autowired private JmsTemplate jmsTemplate; public void sendMessage(String destination, String message) { jmsTemplate.convertAndSend(destination, message); } ``` 注意替换`destination`为实际的Artemis队列或主题名称。 这样,你就可以在Spring Boot应用程序中成功整合Artemis并发送/接收消息了。希望对你有所帮助!如果你还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

服务端相声演员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值