简体   繁体   English

我需要在发送消息之前检查activemq代理的状态

[英]I need to check the status of the activemq broker before the messages sent

actually my usecase is i need to handle the broker not available situation. 实际上我的用例是我需要处理经纪人不可用的情况。 So i need to know the status of the broker before send the messsages? 所以我需要在发送消息之前了解经纪人的状态? I tried with below sendtimeout property, but still not success. 我尝试使用下面的sendtimeout属性,但是仍然没有成功。

<bean primary="true" id="jmsConnectionFactory"
    class="org.apache.activemq.ActiveMQConnectionFactory">
    <property name="brokerURL" value="failover:(tcp://localhost:61616)" />
    <property name="useAsyncSend" value="true" />
    <property name="watchTopicAdvisories" value="false" />
    <property name="sendTimeout" value="2000" />

</bean>
Connection conn = null;
try {
    conn = connectionFactory.createConnection();
    jmsTemplate.send(...);
} 
catch (Exception e) {
    ...
}
finally {
    if (conn != null) {
        conn.close();
    }
}

Or you can simply do the send within a try/catch. 或者,您可以简单地在try / catch中进行发送。

Also, it's better to wrap the ActiveMQConnectionFactory in a CachingConnectionFactory to avoid opening a connection for each send. 另外,最好将ActiveMQConnectionFactory包装在CachingConnectionFactory以避免为每个发送打开连接。

"So i need to know the status of the broker before send the messsages" “因此,在发送消息之前,我需要了解经纪人的身份”

There are surely different options to check the status of the broker but I wonder if this is really needed? 当然,可以使用不同的选项来检查代理的状态,但是我想知道是否真的需要这样做? If you try to send a message when the broker is not available, that send should fail immediately. 如果您在代理不可用时尝试发送消息,则该发送应立即失败。 The only reason I can think of for the producer getting blocked is if the producer gets flow controlled. 我能想到的让生产者受阻的唯一原因是生产者是否受到流量控制。 But that also implies the broker is available and flow control behaviour can be configured (eg throw an exception after a configured time out). 但这也意味着可以使用代理,并且可以配置流控制行为(例如,在配置的超时后引发异常)。 So it should be okay to simply send your message and catch any exceptions that may arise. 因此,只需发送您的消息并捕获可能出现的任何异常,就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM