0

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.

<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>

2 Answers2

0
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.

Also, it's better to wrap the ActiveMQConnectionFactory in a CachingConnectionFactory to avoid opening a connection for each send.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • thanks Gray, anyway i am using ProducerTemplate to send messages , since i am using the spring with activemq as below. ......................................................................................................................... ProducerTemplate tepm = camelContext.createProducerTemplate(); tepm.sendBody("direct:a", string); So i don't call connection in java code. I used all the connection in xml file. – Aruna Sameera Liyanage Aug 21 '17 at 18:08
  • You didn't mention camel in your original question - I can't help you with that. – Gary Russell Aug 21 '17 at 18:51
  • I am using ActiveMQ using camel, so i need to handle broker unavailable situation. How can i do that ? – Aruna Sameera Liyanage Aug 22 '17 at 01:21
0

"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 (e.g. 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.

Torsten Mielke
  • 239
  • 1
  • 3