3

我正在尝试从 Java 连接到本地 RabbitMQ 服务器。我目前ConnectionFactory在 Spring 中配置了我的实例,如下所示:

<bean class="org.rabbitmq.client.ConnectionFactory">
    <property name="host" value="localhost"/>
    <property name="port" value="5672"/>
    <property name="username" value="user"/>
    <property name="password" value="password"/>
</bean>

当我尝试获取新连接时:

connectionFactory.newConnection();

我收到以下错误:

java.lang.IllegalArgumentException: invalid value in table
    at com.rabbitmq.client.impl.Frame.fieldValueSize(Frame.java:306)
    at com.rabbitmq.client.impl.Frame.tableSize(Frame.java:246)
    at com.rabbitmq.client.impl.ValueWriter.writeTable(ValueWriter.java:120)
    at com.rabbitmq.client.impl.MethodArgumentWriter.writeTable(MethodArgumentWriter.java:139)
    at com.rabbitmq.client.impl.AMQImpl$Connection$StartOk.writeArgumentsTo(AMQImpl.java:161)
    at com.rabbitmq.client.impl.Method.toFrame(Method.java:83)
    at com.rabbitmq.client.impl.AMQCommand.transmit(AMQCommand.java:102)
    at com.rabbitmq.client.impl.AMQChannel.quiescingTransmit(AMQChannel.java:316)
    at com.rabbitmq.client.impl.AMQChannel.quiescingTransmit(AMQChannel.java:298)
    at com.rabbitmq.client.impl.AMQChannel.quiescingRpc(AMQChannel.java:233)
    at com.rabbitmq.client.impl.AMQChannel.rpc(AMQChannel.java:224)
    at com.rabbitmq.client.impl.AMQChannel.privateRpc(AMQChannel.java:209)
    at com.rabbitmq.client.impl.AMQChannel.rpc(AMQChannel.java:202)
    at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:340)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:516)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:533)
    at com.tkassembled.dispatcher.service.RabbitMQQueueService.init(RabbitMQQueueService.java:25)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1581)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1522)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:876)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:818)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:735)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1184)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1091)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:609)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:469)
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133)
    at javax.servlet.GenericServlet.init(GenericServlet.java:160)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1266)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1185)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1080)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5015)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5302)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1568)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1558)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:679)

在 RabbitMQ 日志中:

=ERROR REPORT==== 13-Jul-2012::15:24:30 ===
exception on TCP connection <0.1159.0> from 127.0.0.1:55154
{handshake_timeout,frame_header}

更新

我现在也注意到一些非常奇怪的事情。如果我ConnectionFactory在 Spring 中创建实例,那么所有调用都newConnection()将失败,即使我没有在其上设置任何属性:

<bean class="org.rabbitmq.client.ConnectionFactory">
    <!-- nothing -->
</bean>

但是,如果我ConnectionFactory在 Spring 容器之外创建实例然后调用newConnection(),则不会引发错误:

ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("localhost");

Connection connection = connectionFactory.newConnection();

我究竟做错了什么?得到这样一个仅ConnectionFactory在 Spring 容器中创建时才会发生的随机错误似乎有点奇怪。Spring 容器与在原始 Java 中创建它有什么不同呢?

4

2 回答 2

2

我有同样的问题,它在我的标题中:

Map<String, Object> headers = new HashMap<>();
headers.put("user-id", event.uid);
[...]
channel.basicPublish(exchangeName, "???", new AMQP.BasicProperties.Builder().headers(headers).build(), payload.getBytes(UTF_8));

(BasicProperties.Builder() 已被缩短以提高可见性)

问题已通过标头中的字符串值而不是 UUID 解决:

Map<String, Object> headers = new HashMap<>();
headers.put("user-id", event.uid.toString());
于 2017-09-11T08:06:17.927 回答
0

我实现了 Rabbitmq ConnectionFactory 的一些包装器。它是从带有主机/端口/等的 Spring 配置中配置的。并且有简单的方法 getConnection()

ConnectionFactory factory = new ConnectionFactory(); 
factory.setHost(getHostName());
factory.setUsername(getUserName());
factory.setPassword(getPassword());
factory.setAutomaticRecoveryEnabled(true);
Connection connection = factory.newConnection();

如果您只需要一个连接(真的吗?),您可以在构造函数中使用此方法...

于 2014-06-30T07:57:09.977 回答