springboot集成artemis

jar包

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

点对点Queue

   //生产者 
    @Autowired
    private JmsTemplate jmsTemplate;
    @Scheduled(cron="0/5 * * * * ? ")// 使用定时任务,每两秒向mq中发送一个消息
    public void sendMsg(){
        Map<String, String> ptpMap = new HashMap<>();
        ptpMap.put("hello", "activeMQ");
        jmsTemplate.convertAndSend("666", ptpMap);
    }
//消费者
@JmsListener(destination = "666")// 通过JmsListerner实时获取mq中的消息
public void readMsg(Map map) {
        System.out.println("currentTimeMillis" + System.currentTimeMillis() + " = [" + map.toString() + "]");
    }

发布订阅Topic

spring.jms.pub-sub-domain=true #开启topic 原默认queue模式,二者不可得兼
    //生产者
    @Scheduled(cron="0/10 * * * * ?")// 使用定时任务,每两秒向mq中发送一个消息
    public void sendMsg5(){
        System.out.println("112start");
        jmsTemplate.convertAndSend("112","666");
    }
    //消费者
    @JmsListener(destination = "112")
    public void recieve112A(String message) {
        System.out.println("服务订阅112Atopic接收消息: " + message);
    }
    @JmsListener(destination = "112")
    public void recieve112B(String message) {
        System.out.println("服务订阅112Btopic接收消息: " + message);
    }

小结:

spring.jms.pub-sub-domain=false artemis 自动走queue,只能发布点对点模式,多个订阅者轮流消费不同消息,

spring.jms.pub-sub-domain=true 阿尔忒弥斯自动走topic,可以进行订阅发布模式,多个订阅者同一时间消费同一消息,实现解耦。

  • 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值