从 Camel 路由强制 ActiveMQ 上的套接字超时?

2022-01-19 00:00:00 sockets timeout java apache-camel activemq

所以下面我有 Camel(通过 Spring DSL)成功地将我的 bean 与 ActiveMQ 队列集成:

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>

我想在 Camel 和 ActiveMQ 之间明确强制执行 socket timeout(在 Socket.read() 上)为 25 秒.因此,当 Camel 尝试将消息路由到 ActiveMQ 或从 ActiveMQ 路由消息时,如果 ActiveMQ 需要超过 25 秒才能完成该响应,我希望线程优雅地退出.显然,如果还可以设置某种故障转移(以便超时的请求可以在未来重播),那比仅仅丢失消息更可取!

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!

更新:如果 Camel/JMS/ActiveMQ 不支持开箱即用,我不介意编写自己的ThreadManager"来中断/停止25 秒后线程,但我不确定要实现/扩展哪些接口/类,以及随后连接到我的 Spring bean.

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.

推荐答案

只需在你的 brokerURL 上设置 timeout 属性

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

相关文章