2017-09-07 104 views
0

這是我的配置,用於將消息發送給嵌入式中間件。另外,第一個bean方法用於創建和啓動代理。它不返回任何實例。 此代碼是否啓動代理?我已經使用JMS模板的發送方法將消息發送到下面提到的主題。我已經爲經紀人創建和運行寫了createbrokerervice。嵌入式中間件ActiveMQ的配置

@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; 
    } 
+0

代碼評論有https://codereview.stackexchange.com/。 – ventiseis

+0

好吧,我的問題有點不同。 createBrokerservice()中的語句是否被執行?以便我可以發佈和訂閱該經紀人。 – africandrogba

回答

0
@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應該返回經紀人,那麼你的地方使用你的應用程序代理實例得到它開始。

+0

明白了。我把代碼放在main方法中。 – africandrogba

+0

@africandrogba如果它是正確的,請接受答案。 ;) – sarahTheButterFly