寻找springboot,activemq(主题)的示例代码

时间:2017-02-11 06:58:44

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

我找到了很多代码示例来创建一个JMS队列项目,但没有找到activemq(topic)的代码示例,它被称为PUB-SUB(发布者 - 订阅者)。

对于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的建议中实现的。是否存在任何关注点分离和可扩展方式的良好实践。

application.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");

}
}

Subscriber.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 ");
}
}

mainclass: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);
    }



}

3 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

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)

答案 2 :(得分:0)

您可以在此仓库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

如果您有很多新闻队列和许多味精,例如几百万个,则最好不要使用activemq。我在这里的生产环境中使用它,我感到很沮丧。我需要填各种坑。您可以使用RabbitMQ