IAmYourFaja
IAmYourFaja

Reputation: 56874

Enforce socket timeout on ActiveMQ from Camel route?

So below I have Camel (via Spring DSL) successfully integrating my beans with ActiveMQ queues:

<!-- Note: this code is just a snippet; if you need to see more, please let me know! -->
<camelContext id="my-camel-context" xmlns="http://camel.apache.org/schema/spring">
    <route>
        <from uri="activemq-myinstance:queue:myqueue" />
        <onException>
            <exception>java.lang.Exception</exception>
            <redeliveryPolicy maximumRedeliveries="2" />
            <to uri="activemq-myinstance:queue_failures" />
        </onException>
        <to uri="bean:myBean?method=doCommand" />           
    </route>
</camelContext>

<bean id="jmsConnectionFactory-myqueue" class="org.apache.activemq.ActiveMQConnectionFactory">
    <property name="brokerURL" value="${activemq.instance.url}" />
</bean>

<bean id="pooledConnectionFactory-myqueue" class="org.apache.activemq.pool.PooledConnectionFactory">
    <property name="maxConnections" value="64" />
    <property name="maximumActive" value="${max.active.consumers}" />
    <property name="connectionFactory" ref="jmsConnectionFactory-myqueue" />
</bean>

<bean id="jmsConfig-myqueue" class="org.apache.camel.component.jms.JmsConfiguration">
    <property name="connectionFactory" ref="pooledConnectionFactory-myqueue"/>
    <property name="concurrentConsumers" value="${max.active.consumers}"/>
</bean>

<bean id="activemq-myqueue" class="org.apache.activemq.camel.component.ActiveMQComponent">
    <property name="configuration" ref="jmsConfig-myqueue"/> 
</bean>

I'd like to explicitly enforce a socket timeout (on Socket.read()) - between Camel and ActiveMQ - of 25 seconds. Thus, when Camel attempts to route a message to/from ActiveMQ, if ActiveMQ takes more than 25 seconds to complete that response, I want the thread to exit gracefully. Obviously, if it's possible to also set up some kind of failover (so that requests that timeout can get replayed at a future time) that is greatly preferred over just losing the message!

How can I accomplish this? Thanks in advance!

Update: if Camel/JMS/ActiveMQ doesn't support this out of the box, I don't mind writing my own "ThreadManager" that interrupts/stops threads after 25-seconds, but I'm not sure what interface/classes to implement/extend, and to subsequently wire into my Spring beans.

Upvotes: 5

Views: 2729

Answers (2)

Ben ODay
Ben ODay

Reputation: 21005

just set the timeout property on your brokerURL

failover:(tcp\://localhost\:61616)?timeout=25000

this will propagate an error back to your producer so you can handle it instead of having it just blocking the thread forever...

Upvotes: 1

techuser soma
techuser soma

Reputation: 4877

By default Camel activemq request times out after 20 seconds.

For send timeouts there is a sendTimeout property on org.apache.activemq.ActiveMQConnectionFactory. Also check the requesttimeout option for JMSConfiguraiton.

For failover you can set the failover transport in the broker url.

Upvotes: 1

Related Questions