Embedded Broker ActiveMQ的配置

时间:2017-09-07 19:43:22

标签: java spring-boot jms messagebroker

这是我向嵌入式代理发送消息的配置。此外,第一个bean方法用于创建和启动代理。它不会返回任何实例。 这段代码是否启动了经纪人?我使用了JMS模板的send方法将消息发送到下面提到的主题。我已经为要创建和运行的代理编写了createbrokerservice。

@Configuration
@EnableJms
public class JMSConfig {

    public static final String DEFAULT_BROKER_URL = "tcp://localhost:61616";
    public static final String COMMENT_QUEUE = "comment-queue";

    @Bean
    public void createBrokerService() throws Exception {
        BrokerService broker = new BrokerService();
        TransportConnector connector = new TransportConnector();
        connector.setUri(new URI("tcp://localhost:61616"));
        broker.addConnector(connector);
        broker.start();
    }

    @Bean
    public ActiveMQConnectionFactory connectionFactory(){
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
        connectionFactory.setBrokerURL(DEFAULT_BROKER_URL);
        return connectionFactory;
    }   

    @Bean
    public JmsTemplate jmsTemplate(){
        JmsTemplate template = new JmsTemplate();
        template.setConnectionFactory(connectionFactory());
        template.setDefaultDestinationName(COMMENT_QUEUE);
        template.setPubSubDomain(true);
        template.setSessionTransacted(true);
        return template;
    }

1 个答案:

答案 0 :(得分:1)

   @Bean
public void createBrokerService() throws Exception {
    BrokerService broker = new BrokerService();
    TransportConnector connector = new TransportConnector();
    connector.setUri(new URI("tcp://localhost:61616"));
    broker.addConnector(connector);
    broker.start();
}

这是不正确的,@ Bean应该返回代理,然后在应用程序的某个地方使用代理实例来启动代理。