我在localhost上有两个不同的 RabbitMQ实例:5672和localhost:5676。 我也在我的应用程序上下文中配置了它:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation=
"http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
xmlns:cache="http://www.springframework.org/schema/cache">
<bean id="rabbitConnectionFactory" class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
<property name="username" value="${rabbit.username}"/>
<property name="password" value="${rabbit.password}"/>
<property name="host" value="${rabbit.host}"/>
<property name="port" value="${rabbit.port}"/>
</bean>
<bean id="amqpTemplate" class="org.springframework.amqp.rabbit.core.RabbitTemplate">
<property name="connectionFactory" ref="rabbitConnectionFactory"/>
<property name="exchange" value="${rabbit.exchange}"/>
<property name="routingKey" value="${rabbit.routing-key}"/>
<property name="queue" value="${rabbit.queue}"/>
</bean>
<bean id="admin" class="org.springframework.amqp.rabbit.core.RabbitAdmin">
<constructor-arg ref="rabbitConnectionFactory" />
</bean>
<bean id="rabbitTxManager"
class="org.springframework.amqp.rabbit.transaction.RabbitTransactionManager">
<property name="connectionFactory" ref="rabbitConnectionFactory"/>
</bean>
<bean id="messageListenerContainer" class="org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer">
<property name="channelTransacted" value="true"/>
<property name="transactionManager" ref="rabbitTxManager"/>
<property name="prefetchCount" value="${rabbit.prefetchCount}"/>
<property name="txSize" value="${rabbit.txSize}"/>
<property name="connectionFactory" ref="rabbitConnectionFactory"/>
<property name="messageListener" ref="handler"/>
<property name="queueNames" value="${rabbit.queue}"/>
<property name="concurrentConsumers" value="${rabbit.concurrentConsumers}"/>
<property name="errorHandler" ref="errorHandler"/>
</bean>
<bean id="handler" class="com.pb.pav.timingbus.amqp.MessageHandler" >
<constructor-arg index="0" ref="gson"/>
<constructor-arg index="1" ref="sender"/>
</bean>
<bean id="errorHandler" class="com.pb.pav.timingbus.amqp.ExceptionHandler"/>
<bean id="gson" factory-bean="formattedBuilder" factory-method="create" />
<bean id="gsonBuilder" class="com.google.gson.GsonBuilder" />
<bean id="formattedBuilder" factory-bean="gsonBuilder" factory-method="setDateFormat">
<constructor-arg index="0" value="yyyy-MM-dd HH:mm:ss.SSS"/>
</bean>
<bean id="sender" class="com.pb.timing.client.TimingWSClient">
<constructor-arg index="0" value="${timing.url}"/>
</bean>
<bean id="busService" class="com.pb.pav.timingbus.BusService" />
<bean id="answerConnFactory" class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
<property name="username" value="guest"/>
<property name="password" value="guest"/>
<property name="host" value="localhost"/>
<property name="port" value="5676"/>
<property name="channelCacheSize" value="2"/>
</bean>
<bean id="rabbitAnswer" class="org.springframework.amqp.rabbit.core.RabbitTemplate">
<property name="connectionFactory" ref="answerConnFactory"/>
<property name="exchange" value="timinganswers"/>
<property name="routingKey" value="temp"/>
<property name="queue" value="temp"/>
</bean>
<bean id="answerAdmin" class="org.springframework.amqp.rabbit.core.RabbitAdmin">
<constructor-arg ref="answerConnFactory" />
</bean>
<bean id="mesSender" class="com.pb.pav.timingbus.amqp.MessageSender" >
<constructor-arg index="0" ref="gson"/>
<constructor-arg index="1" ref="rabbitAnswer"/>
</bean>
我的Spring Web应用程序向 rabbit(localhost:5672,id =“amqpTemplate”)生成消息,然后从队列中消耗并保存到DB。
保存到数据库应用程序后产生答案(确定或错误),我需要发布第二个 rabbitMQ(localhost:5676,id =“rabbitAnswer”)的答案。
当我尝试发布第二个兔子的答案(localhost:5676,id =“rabbitAnswer”)时,他们发布了第一只兔子,我不明白为什么???我在发送消息之前记录连接端口,它正确 - 5676,但是应用程序向5672发送消息。
我怀疑问题出在频道,因为在第二只兔子我没有看到任何打开的频道。那么,我如何向第二只兔子申报不同的频道并以正确的方式发送信息呢?