【问题标题】:looking for a sample code for springboot, activemq(topic)寻找springboot的示例代码,activemq(topic)
【发布时间】:2023-03-13 00:24:01
【问题描述】:

我找到了许多代码示例来创建 JMS 队列项目,但没有找到名为 PUB-SUB(Publisher-Subscriber) 的 activemq(topic) 的代码示例。

对于 Spring,我在下面找到了代码,但正在寻找 Spring Boot 完整代码。

       Topic topic = topicConsumerSession.createTopic("customerTopic");

        // Consumer1 subscribes to customerTopic
        MessageConsumer consumer1 = topicConsumerSession.createSubscriber(topic);
        consumer1.setMessageListener(new ConsumerMessageListener(
                "Consumer1"));

        // Consumer2 subscribes to customerTopic
        MessageConsumer consumer2 = topicConsumerSession.createSubscriber(topic);
        consumer2.setMessageListener(new ConsumerMessageListener(
                "Consumer2"));

首先非常感谢@Gary Russell。

这是我从@Gary Russell 建议中实现的。有什么好的做法可以分离关注点和更具可扩展性的方式。

应用程序.properties

spring.jms.pub-sub-domain=true
#spring.jms.template.default-destination=testTopic

Publisher.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;
@Component
public class Publisher implements CommandLineRunner{
@Autowired
private JmsTemplate jmsTemplate ;
@Autowired
private Topic topic1;
@Autowired
private Topic topic2;

@Override
public void run(String... arg0) throws Exception {
    // TODO Auto-generated method stub
    Thread.sleep(5000); // wait for subscriptions, unless they are durable
    this.jmsTemplate.convertAndSend(this.topic1,"-----> 1st message from publisher -- topic 1");
    Thread.sleep(5000);
    this.jmsTemplate.convertAndSend(this.topic1,"-----> 2nd message from publisher -- topic 1");
    /**
     * for topic2 
     */

 // TODO Auto-generated method stub
        Thread.sleep(5000); // wait for subscriptions, unless they are durable
         this.jmsTemplate.convertAndSend(this.topic2,"-----> 1st message from publisher -- topic 2");
         Thread.sleep(5000);
         this.jmsTemplate.convertAndSend(this.topic2,"-----> 2nd message from publisher -- topic 2");

}
}

订阅者.java

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


@Component
public class Subscriber {
@JmsListener(destination = "Topic1")
public void listener1(String in) {
    System.out.println("Listener1: " + in);
}

@JmsListener(destination = "Topic1,Topic2")
public void listener2(String in) {
    System.out.println("Listener2: " + in);
}

@JmsListener(destination = "Topic2")
public void listener3(String in) {
    System.out.println("Listener3: " + in+"\n listener 3 is just ");
}
}

主类:springBootApplication

PubSubJmsBootApplication.java

import javax.jms.Topic;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.annotation.EnableJms;

@SpringBootApplication
@EnableJms
public class PubSubJmsBootApplication {


    @Bean
    public Topic topic1() {
        return new ActiveMQTopic("Topic1");
    }
    @Bean
    public Topic topic2() {
        return new ActiveMQTopic("Topic2");
    }

    public static void main(String[] args) {
        SpringApplication.run(PubSubJmsBootApplication.class, args);
    }



}

【问题讨论】:

    标签: spring-boot jms activemq spring-jms jms-topic


    【解决方案1】:

    你可以在这个 repo spring-boot-quick找到一些简单的代码

    https://github.com/vector4wang/spring-boot-quick/tree/master/quick-activemq https://github.com/vector4wang/spring-boot-quick/tree/master/quick-activemq2

    如果你的新闻队列很多,msg也很多,比如几百万,最好不要使用activemq。我这里生产环境使用,被坑了。我需要填补各种坑。你可以使用 RabbitMQ

    【讨论】:

      【解决方案2】:

      application.properties

      spring.jms.pub-sub-domain=true
      spring.jms.template.default-destination=testTopic
      

      那么……

      @SpringBootApplication
      public class So42173236Application implements CommandLineRunner {
      
          public static void main(String[] args) {
              SpringApplication.run(So42173236Application.class, args)
                  .close();
          }
      
          @Autowired
          private JmsTemplate jmsTemplate;
      
          @Override
          public void run(String... arg0) throws Exception {
              Thread.sleep(5000); // wait for subscriptions, unless they are durable
              this.jmsTemplate.convertAndSend("foo");
              Thread.sleep(5000);
          }
      
          @JmsListener(destination = "testTopic")
          public void listener1(String in) {
              System.out.println("Listener1: " + in);
          }
      
          @JmsListener(destination = "testTopic")
          public void listener2(String in) {
              System.out.println("Listener2: " + in);
          }
      
      }
      

      如果您自己构建 JmsTemplate 或侦听器容器(而不是使用 Boot 的自动配置),只需 setPubSubDomain(true)

      【讨论】:

      • 如果我想创建一个新主题怎么办。这是正确的方法吗 ==> ' spring.jms.template.default-destination=youtube-channel
      • 是的;如果主题不存在,将创建该主题。您还可以在convertAndSend 命令中指定目标(主题名称)。关键是模板是为pub/sub配置的。
      • 当你用 '@JmsListener(destination = "testTopic")' 覆盖这个属性时,为什么会有 'spring.jms.template.default-destination=testTopic' 属性?
      • ?模板用于发送,监听器用于接收。 this.jmsTemplate.convertAndSend("foo");foo 发送到默认目的地。
      • 它说“在没有目标参数的发送和接收操作中使用的默认目标。” docs.spring.io/spring-boot/docs/current/reference/html/…
      【解决方案3】:

      【讨论】:

      • 已经看到了,它没有被称为PUB-SUB(Publisher-Subscriber)的activemq(topic)。
      猜你喜欢
      • 1970-01-01
      • 2018-06-30
      • 1970-01-01
      • 2021-05-03
      • 2010-09-16
      • 1970-01-01
      • 1970-01-01
      • 2016-07-08
      • 2010-12-04
      相关资源
      最近更新 更多