简体   繁体   English

使用Spring Jms问题异步发送到队列

[英]Asynchronous send to a queue using Spring Jms issue

I have a requirement when I need to write asynchronously to a queue in activemq. 当我需要异步写入activemq中的队列时,我有一个要求。 I am using Spring Jms to do it. 我正在使用Spring Jms来做到这一点。 This is the wiring in my spring context file 这是我的春季上下文文件中的接线

<bean id="amqProducerConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="${activemq.broker}"/>        
    </bean>

    <bean id="pooledProducerConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop" lazy-init="true">
        <property name="connectionFactory" ref="amqProducerConnectionFactory" />
    </bean>

    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="pooledProducerConnectionFactory" />
    </bean>

And in my code...i do.. 用我的代码...

void sendMessage(JmsTemplate jmsTemplate, String message, String requestQueue) {
        def messageCreator = { session ->
            session.createTextMessage(message)
        } as MessageCreator

        jmsTemplate.send(requestQueue, messageCreator)
    }

But the above seems to be working synchronously, not asynchrously. 但是以上内容似乎是同步进行的,而不是异步进行的。 Is there anything that I need to add here that makes the process asynchronous(I mean, App 'A' writes to a queue. It should write to the queue and forget, not wait until App 'B' picks it up from the queue and processes it.) 我是否需要添加任何使流程异步的内容(我的意思是,App'A'写入队列。它应该写入队列,然后忘记,不要等到App'B'从队列中将其取走,然后处理它。)

The JmsTemplate send is never synchronous in terms of waiting for a consumer to take a Message of the Queue in the normal case. 在正常情况下,就等待用户接受队列消息而言,JmsTemplate发送永远不会同步。 The send can however be synchronous in that it waits for a response from the Broker indicating that it has received and stored the Message. 但是,发送可以是同步的,因为它等待来自Broker的响应,表明它已接收并存储了消息。 This is so that you have an indication of success as Queues must ensure that they are reliable. 这样一来,您就可以指示成功,因为队列必须确保它们是可靠的。 You can configure many things in the ActiveMQConnectionFactory to control this. 您可以在ActiveMQConnectionFactory中配置很多东西来控制它。 Setting the options useAsyncSend will enforce that your send don't wait for a Broker ACK if that is what you want. 设置选项useAsyncSend将强制您的发送不要等待Broker ACK(如果需要的话)。 These options are all documented . 这些选项均已记录在案

You configure these on the connection URI like so: tcp://localhost:61616?jms.useAsyncSend=true 您可以像这样在连接URI上配置它们: tcp://localhost:61616?jms.useAsyncSend=true

A send can however block if the ActiveMQ Broker's producer flow control kicks in to prevent a Producer from flooding the Broker with messages. 但是,如果启动了ActiveMQ Broker的生产者流控制以防止生产者向代理充斥消息,则发送可能会阻塞。 This is configurable as well both in terms of disabling it altogether and also in that you can increase the memory limits on the Broker for when this might kick in. 这是可以配置的,既可以完全禁用它,也可以增加代理的内存限制 ,以确保在可能的时候启动代理。

Finally a Producer send could block if the Connection to the broker is lost and you are using the Failover Transport to have your client automatically reconnect. 最后,如果与代理的连接丢失并且您正在使用故障转移传输来让客户端自动重新连接,则生产者发送可能会阻塞。

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

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