使用RabbitMQ的消息时,无效的流头

时间:2019-04-03 10:24:42

标签: java rabbitmq jms

我们使用RabbitMQ作为消息传递代理。为了与队列通信,我们使用RabbitMQ JMS client的JMS实现。我们将JMS客户端用于生产者和消费者。尝试从队列中读取消息时,使用者遇到了问题。它将引发以下异常。请让我知道如何解决此问题。

错误消息

    com.rabbitmq.jms.util.RMQJMSException: invalid stream header: 68656C6C
    at com.rabbitmq.jms.client.RMQMessage.fromMessage(RMQMessage.java:1128) ~[rabbitmq-jms-1.11.2.jar:1.11.2]
    at com.rabbitmq.jms.client.RMQMessage.convertJmsMessage(RMQMessage.java:901) ~[rabbitmq-jms-1.11.2.jar:1.11.2]
    at com.rabbitmq.jms.client.RMQMessage.convertMessage(RMQMessage.java:895) ~[rabbitmq-jms-1.11.2.jar:1.11.2]
    at com.rabbitmq.jms.client.MessageListenerConsumer.handleDelivery(MessageListenerConsumer.java:154) ~[rabbitmq-jms-1.11.2.jar:1.11.2]
    at com.rabbitmq.client.impl.ConsumerDispatcher$5.run(ConsumerDispatcher.java:149) [amqp-client-5.6.0.jar:5.6.0]
    at com.rabbitmq.client.impl.ConsumerWorkService$WorkPoolRunnable.run(ConsumerWorkService.java:104) [amqp-client-5.6.0.jar:5.6.0]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_191]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0_191]
    at java.lang.Thread.run(Unknown Source) [na:1.8.0_191]
Caused by: java.io.StreamCorruptedException: invalid stream header: 68656C6C
    at java.io.ObjectInputStream.readStreamHeader(Unknown Source) ~[na:1.8.0_191]
    at java.io.ObjectInputStream.<init>(Unknown Source) ~[na:1.8.0_191]
    at com.rabbitmq.jms.util.WhiteListObjectInputStream.<init>(WhiteListObjectInputStream.java:90) ~[rabbitmq-jms-1.11.2.jar:1.11.2]
    at com.rabbitmq.jms.client.RMQMessage.fromMessage(RMQMessage.java:1102) ~[rabbitmq-jms-1.11.2.jar:1.11.2]
    ... 8 common frames omitted

Producer.Java

        RMQConnectionFactory connectionFactory = new RMQConnectionFactory();
                connectionFactory.setUsername("xxx");
                connectionFactory.setPassword("xxx");
                connectionFactory.setHost(xxx.xxx.xxx);

                RMQDestination jmsDestination = new RMQDestination();
                jmsDestination.setAmqp(true);
                jmsDestination.setQueue(true);
                jmsDestination.setAmqpExchangeName("");
                jmsDestination.setAmqpRoutingKey(queueName);
                jmsDestination.setDestinationName(queueName);
                jmsDestination.setAmqpQueueName(queueName);

    try (javax.jms.Connection connection = connectionFactory.createConnection();Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE)) {

                    connection.start();
                    MessageProducer producer = session.createProducer(jmsDestination);
                    TextMessage msg = session.createTextMessage("My message");
                    msg.setJMSType("TextMessage");
                    producer.send(msg);
        }

Consumer.Java

 Connection connection = null;
    RMQConnectionFactory factory = new RMQConnectionFactory();
                factory.setHost(GTAFConfig.MESSAGE_QUEUE_HOST_IP.value());
                factory.setUsername("xxx");
                factory.setPassword("xxx");

                if (connection == null)
                    connection = factory.createConnection();

                Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                Queue queue = session
                        .createQueue(QueueRegistry.getQueueName("queue name");
                MessageConsumer consumer = session.createConsumer(queue);
                consumer.setMessageListener(listener);
                connection.start();

    @Override
        public void onMessage(Message message) {


            try {
                if (message instanceof TextMessage) {
                    TextMessage textMessage = (TextMessage) message;
                    System.out.println("Successfully receievedt message,msg id:" +message.getJMSMessageID());
        }

1 个答案:

答案 0 :(得分:0)

要能够读回消息,您需要在消费者(如生产者)中设置RMQDestination

赞:

RMQConnectionFactory connectionFactory = new RMQConnectionFactory();
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
connectionFactory.setVirtualHost("/");
connectionFactory.setHost("localhost");

RMQDestination jmsDestination = new RMQDestination();
jmsDestination.setAmqp(true);
jmsDestination.setAmqpQueueName("rabbitQueueName");

Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
RMQMessageConsumer consumer = (RMQMessageConsumer)session.createConsumer(jmsDestination);

(在此从RabbitMQ JMS客户端上的开发人员那里获取了此代码 https://groups.google.com/forum/m/#!topic/rabbitmq-users/Zbcp9WL-vUM