发布服务器无法接收rabbitmq上的第一条消息
我试图在RabbitMQ上实现一个集成测试,因此我有一个已发布的基本使用者的设置,用于跨队列发送和接收消息发布服务器无法接收rabbitmq上的第一条消息,rabbitmq,spring-amqp,Rabbitmq,Spring Amqp,我试图在RabbitMQ上实现一个集成测试,因此我有一个已发布的基本使用者的设置,用于跨队列发送和接收消息 try { ConnectionFactory factory = new ConnectionFactory(); factory.setUsername("guest"); factory.setPassword("guest"); factory.setHost("localhost"); factory.setPort(56
try
{
ConnectionFactory factory = new ConnectionFactory();
factory.setUsername("guest");
factory.setPassword("guest");
factory.setHost("localhost");
factory.setPort(5672);
connection = factory.newConnection();
channel = connection.createChannel();
channel.queueDeclare(RPC_QUEUE_NAME_KEY, DURABLE, EXCLUSIVE, AUTO_DELETE, null);
channel.exchangeDeclare(RPC_EXCHANGE_NAME, "direct");
String queueName = channel.queueDeclare().getQueue();
channel.queueBind(queueName, RPC_EXCHANGE_NAME, RPC_QUEUE_NAME_KEY);
channel.basicQos(1);
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(queueName, false, consumer);
System.out.println(" [x] Awaiting RPC requests");
while(true)
{
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
BasicProperties props = delivery.getProperties();
BasicProperties replyProps = new BasicProperties.Builder().correlationId(props.getCorrelationId()).build();
try
{
RetrieveUnencryptedCardsResponse response = null;
JAXBContext jaxbContext = JAXBContext.newInstance(RetrieveUnencryptedCardsRequest.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader(new String(delivery.getBody()));
JAXBElement<RetrieveUnencryptedCardsRequest> message = jaxbUnmarshaller.unmarshal(new StreamSource(reader), RetrieveUnencryptedCardsRequest.class);
response = retrieveUnencryptedCardsResponse(message);
JAXBContext jaxbMarshallingContext = JAXBContext.newInstance(RetrieveUnencryptedCardsResponse.class);
Marshaller jaxbMarshaller = jaxbMarshallingContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
ByteArrayOutputStream xmlStream = new ByteArrayOutputStream();
jaxbMarshaller.marshal(new JAXBElement<RetrieveUnencryptedCardsResponse>(_RetrieveUnencryptedCardsResponse_QNAME, RetrieveUnencryptedCardsResponse.class, null,
response), xmlStream);
data = xmlStream.toByteArray();
xmlStream.close();
}
catch(Exception e)
{
logger.error(e);
}
finally
{
channel.basicPublish("", props.getReplyTo(), replyProps, data);
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
if(connection != null)
{
try
{
connection.close();
}
catch(Exception ignore)
{
}
}
}
试试看
{
ConnectionFactory工厂=新的ConnectionFactory();
factory.setUsername(“来宾”);
工厂设置密码(“来宾”);
setHost(“localhost”);
工厂设置端口(5672);
connection=factory.newConnection();
channel=connection.createChannel();
queueDeclare(RPC\u QUEUE\u NAME\u KEY,持久,独占,自动删除,null);
通道交换声明(RPC_交换名称,“直接”);
字符串queueName=channel.queueDeclare().getQueue();
queueBind(queueName、RPC\u交换名称、RPC\u队列名称\u密钥);
基本频道(1);
QueueingConsumer=新的QueueingConsumer(通道);
channel.basicConsume(queueName,false,consumer);
System.out.println(“[x]等待RPC请求”);
while(true)
{
QueueingConsumer.Delivery=consumer.nextDelivery();
BasicProperties props=delivery.getProperties();
BasicProperties replyProps=新建BasicProperties.Builder().correlationId(props.getCorrelationId()).build();
尝试
{
RetrieveUnencryptedCardsResponse=null;
JAXBContext JAXBContext=JAXBContext.newInstance(RetrieveUnencryptedCardsRequest.class);
解组器jaxbUnmarshaller=jaxbContext.createUnmarshaller();
StringReader=newStringReader(新字符串(delivery.getBody());
JAXBElement message=jaxbUnmarshaller.unmarshal(新StreamSource(reader),RetrieveUnencryptedCardsRequest.class);
响应=检索未加密的卡片响应(消息);
JAXBContext jaxbMarshallingContext=JAXBContext.newInstance(RetrieveUnencryptedCardsResponse.class);
Marshaller jaxbMarshaller=jaxbMarshallingContext.createMarshaller();
setProperty(Marshaller.JAXB_片段,true);
ByteArrayOutputStream xmlStream=新建ByteArrayOutputStream();
jaxbMarshaller.marshall(新的JAXBElement(_RetrieveUnencryptedCardsResponse_QNAME,RetrieveUnencryptedCardsResponse.class,null,
响应),xmlStream);
data=xmlStream.toByteArray();
xmlStream.close();
}
捕获(例外e)
{
错误(e);
}
最后
{
channel.basicPublish(“,props.getReplyTo(),replyProps,data);
channel.basicAck(delivery.getEnvelope().getDeliveryTag(),false);
}
}
}
捕获(例外e)
{
e、 printStackTrace();
}
最后
{
if(连接!=null)
{
尝试
{
connection.close();
}
捕获(异常忽略)
{
}
}
}
}
当我试图从队列接收消息时,问题就出现了。我不能收到第一条信息,但我可以收到第二条信息
我不知道这是否有帮助:
<rabbit:queue id="account.amqp.default.reply.queue"
name="${vendor.account.amqp.default.reply.queue}" durable="${connector.fixed.queue.durable}"
auto-delete="false" exclusive="false">
<rabbit:queue-arguments>
<entry key="x-message-ttl">
<value type="java.lang.Long">${vendor.account.amqp.default.reply.ttl}</value>
</entry>
<entry key="x-ha-policy" value="${vendor.account.default.queue.ha.policy}" />
<entry key="x-ha-policy-params"
value="#{ T(org.springframework.util.CollectionUtils).arrayToList((T(org.springframework.util.StringUtils).commaDelimitedListToSet('${vendor.account.default.queue.ha.nodes}')).toArray())}" />
</rabbit:queue-arguments>
</rabbit:queue>
<rabbit:template id="pamRabbitTemplate"
connection-factory="connectionFactory" message-converter="accountMarshallingMessageConverter"
reply-timeout="${gateway.reply.timeout}" reply-queue="${vendor.account.amqp.default.reply.queue}">
<rabbit:reply-listener
concurrency="${vendor.account.amqp.default.reply.queue.consumers}" />
</rabbit:template>
<bean id="accountMarshallingMessageConverter"
class="org.springframework.amqp.support.converter.MarshallingMessageConverter">
<property name="marshaller" ref="fuelNotificationWsMarshaller" />
<property name="unmarshaller" ref="fuelNotificationWsMarshaller" />
</bean>
${vendor.account.amqp.default.reply.ttl}
在pamRabbitTemplate中:
<int:channel id="channel.accounts.request" />
<int:channel id="channel.accounts.reply" />
<int:channel id="channel.accounts.outbound.request" />
<int:channel id="channel.accounts.outbound.reply" />
<int:channel id="channel.accounts.error" />
<int-amqp:outbound-gateway request-channel="channel.accounts.outbound.request"
reply-channel="channel.accounts.outbound.reply" amqp-template="pamRabbitTemplate"
exchange-name="${pam.exchange.service.account}" routing-key="${pam.routingkey.service.account}"
requires-reply="false" />
<int:gateway id="gateway.account"
service-interface="notification.fuelnotification.infrastructure.integration.RetrieveAccountsGateway"
error-channel="channel.error" default-reply-timeout="${gateway.reply.timeout}">
<int:method name="retrieveUnencryptedCards"
request-channel="channel.accounts.request" reply-channel="channel.accounts.reply" />
</int:gateway>
<int:transformer id="accountsReqTf" method="transform"
input-channel="channel.accounts.request" output-channel="channel.accounts.outbound.request">
<bean
class="notification.fuelnotification.infrastructure.transformer.common.AccountsRequestTransformer" />
</int:transformer>
<int:transformer id="accountResTf" method="transform"
input-channel="channel.accounts.outbound.reply" output-channel="channel.accounts.reply">
<bean
class="notification.fuelnotification.infrastructure.transformer.common.UnencryptedCardsResponseTransformer" />
</int:transformer>
<int:transformer id="errorLoggerTransformer"
method="logDetails" input-channel="channel.error">
<bean
class="notification.fuelnotification.infrastructure.config.amqp.ErrorChannelDetailLogger" />
</int:transformer>
听起来很奇怪。请分享一下症状,看起来怎么样?关于此事的一些日志。您检查了RabbitMQ代理上的队列状态了吗?嗨,Artem,我的队列在RabbitMQ代理上是空闲的,您的StackTrace真的没有帮助。从另一个角度看,您的
空闲
是什么意思还不清楚。队列不能处于空闲或活动状态。它只是一个信息容器。你的第一条信息有什么问题。从另一个角度看,这对我来说没有意义,因为在出现第一条消息之前,您无法检索下一条消息。这就是为什么它是队列…我刚刚更新了我的问题,添加了XML配置。听起来很奇怪。请分享一下症状,看起来怎么样?关于此事的一些日志。您检查了RabbitMQ代理上的队列状态了吗?嗨,Artem,我的队列在RabbitMQ代理上是空闲的,您的StackTrace真的没有帮助。从另一个角度看,您的空闲
是什么意思还不清楚。队列不能处于空闲或活动状态。它只是一个信息容器。你的第一条信息有什么问题。从另一个角度看,这对我来说没有意义,因为在出现第一条消息之前,您无法检索下一条消息。这就是为什么它是队列…我刚刚更新了我的问题,我添加了XML配置。