AMQP与RPC模型中的事务管理器

时间:2014-06-19 17:21:24

标签: spring-transactions spring-amqp

我使用spring-amqp / RabbitMQ作为我的RPC项目。我可以通过消息代理发送和接收消息,没有任何问题。

这是我的工作代码

<bean id="stbListener"
    class="org.springframework.amqp.remoting.service.A mqpInvokerServiceExporter">
    <property name="serviceInterface" value="com.java.stb.service.api.STBService" />
    <property name="service" ref="stbService" />
    <property name="amqpTemplate" ref="template" />
</bean>

<bean id="stbService" class="com.java.stb.service.impl.STBServiceImpl" />

<rabbit:connection-factory id="connectionFactory"
    channel-cache-size="10" />
<rabbit:admin connection-factory="connectionFactory"
    auto-startup="false" />
<rabbit:template id="template" connection-factory="connectionFactory"
    channel-transacted="true" />

<rabbit:queue name="${queue}" />

<rabbit:listener-container connection-factory="connectionFactory">
    <rabbit:listener ref="stbListener" queue-names="${queue}" />
</rabbit:listener-container>

最近我在Postgres中为get / insert / update数据添加了@transactional注释。现在我有问题从制作人那里得到消息。我使用添加的事务管理器

复制了我的生产者代码
<bean id="stbListener"
class="org.springframework.amqp.remoting.service.A mqpInvokerServiceExporter">
<property name="serviceInterface" value="com.java.stb.service.api.STBService" />
<property name="service" ref="stbService" />
<property name="amqpTemplate" ref="template" />
</bean>


<bean id="rabbitTxManager"
class="org.springframework.amqp.rabbit.transaction .RabbitTransactionManager">
<property name="connectionFactory" ref="connectionFactory" />
</bean>
<bean id="stbService" class="com.java.stb.service.impl.STBServiceImpl" />

<rabbit:connection-factory id="connectionFactory"
channel-cache-size="10" />
<rabbit:admin connection-factory="connectionFactory"
auto-startup="false" />
<rabbit:template id="template" connection-factory="connectionFactory" 
channel-transacted="true" />

<rabbit:queue name="${queue}" />

<rabbit:listener-container concurrency="20" prefetch="20" transaction-size="10" auto-startup="false" 
connection-factory="connectionFactory" transaction-manager="rabbitTxManager">
<rabbit:listener ref="stbListener" queue-names="${queue}" />
</rabbit:listener-container> 

我收到超时错误

Caused by: org.springframework.remoting.RemoteProxyFailureExc eption: No reply received - perhaps a timeout in the template?
at org.springframework.amqp.remoting.client.AmqpClien tInterceptor.invoke(AmqpClientInterceptor.java:60)
at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :172)
at org.springframework.aop.framework.JdkDynamicAopPro xy.invoke(JdkDynamicAopProxy.java:204)
at com.sun.proxy.$Proxy7.isIdentityAvailableForSTB(Un known Source)
at tv.moonweb.stb.service.api.STBServiceClient.isIden tityAvailableForSTB(STBServiceClient.java:101)
... 17 more

我在消费者方面设置了reply-timeout="10000"

请帮我解决这个问题

提前感谢

Gopy

1 个答案:

答案 0 :(得分:0)

我建议你打开双方的调试日志记录;如果你无法弄清问题是什么,请将日志发布到某个地方,我们可以看看。

当你说你在生产者方面添加了@Transactional时,为什么你在消费者方面添加了一个Rabbit Transaction manager就不清楚了。

如果您的STBServiceImpl与DB通信,则事务管理器应该是JDBC事务管理器。

如果您希望下游RabbitTemplate参与入站会话,则消息侦听器容器也需要channel-transacted - 否则侦听器将在非事务性通道中运行。