我正在尝试使用带有jackson2JsonMessageConverter
的Spring AMQP简单发送和接收消息。
此外,_TypeId_
在这里有什么重要意义
为什么它显示带有类名的发件人包?
我在接收消息方面遇到了问题。
以下是我的配置
org.springframework.amqp.support.converter.MessageConversionException: 无法解析班级名称。找不到课程 [org.springframework.amqp.helloworld.User] 在org.springframework.amqp.support.converter.DefaultJackson2JavaTypeMapper.getClassIdType(DefaultJackson2JavaTypeMapper.java:121) 在org.springframework.amqp.support.converter.DefaultJackson2JavaTypeMapper.toJavaType(DefaultJackson2JavaTypeMapper.java:90) 在org.springframework.amqp.support.converter.Jackson2JsonMessageConverter.fromMessage(Jackson2JsonMessageConverter.java:145) 在org.springframework.amqp.rabbit.listener.adapter.AbstractAdaptableMessageListener.extractMessage(AbstractAdaptableMessageListener.java:236) 在org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter.onMessage(MessageListenerAdapter.java:288) 在org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:777) ...省略了10个常见帧引起的:java.lang.ClassNotFoundException: org.springframework.amqp.helloworld.User 在org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1305) 在org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1139) 在org.springframework.util.ClassUtils.forName(ClassUtils.java:250) 在org.springframework.amqp.support.converter.DefaultJackson2JavaTypeMapper.getClassIdType(DefaultJackson2JavaTypeMapper.java:118) ...省略了15个常见帧
XML配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<rabbit:connection-factory id="connectionFactory"
channel-cache-size="25" host="10.165.18.29" username="BipUser"
password="bip" />
<rabbit:queue name="Job Queue"></rabbit:queue>
<rabbit:queue name="Input Queue"></rabbit:queue>
<rabbit:queue name="More Info Queue"></rabbit:queue>
<rabbit:queue name="Adaptor O/P Queue"></rabbit:queue>
<rabbit:queue name="Command Queue"></rabbit:queue>
<rabbit:queue name="Error Queue"></rabbit:queue>
<bean id="simpleMessageConverter"
class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter">
</bean>
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory"
message-converter="jsonConverterWithDefaultType" />
<rabbit:listener-container
connection-factory="connectionFactory" auto-declare="true"
message-converter="simpleMessageConverter" auto-startup="true"
acknowledge="auto">
<rabbit:listener ref="rabbitMQJobListener"
queue-names="Job Queue" priority="10" />
</rabbit:listener-container>
<rabbit:admin connection-factory="connectionFactory" id="amqpAdmin" />
<bean id="rabbitMQJobListener" class="com.bosch.bip.rabbitmq.consumer.RabbitMQJobListener">
</bean>
<rabbit:annotation-driven container-factory="rabbitListenerContainerFactory" />
<bean id="rabbitListenerContainerFactory"
class="org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory">
<property name="connectionFactory" ref="connectionFactory"></property>
<property name="messageConverter" ref="jsonConverterWithDefaultType"></property>
</bean>
<bean id="jsonConverterWithDefaultType"
class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter">
<property name="classMapper">
<bean class="org.springframework.amqp.support.converter.DefaultClassMapper">
</bean>
</property>
</bean>
</beans>
发件人
package org.springframework.amqp.helloworld;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.DefaultClassMapper;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Component;
@Component
public class Sender {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(HelloWorldConfiguration.class);
User user=new User();
user.setPassword("welcome");
user.setUserName("welcome");
user.setXml("myxml");
RabbitTemplate rabbitTemplate = context.getBean(RabbitTemplate.class);
Jackson2JsonMessageConverter converter = context.getBean(Jackson2JsonMessageConverter.class);
MessageProperties properties = new MessageProperties();
properties.setHeader("user", "user");
properties.setContentType(MessageProperties.CONTENT_TYPE_JSON);
Message message = converter.toMessage(user, properties);
System.out.println(message);
rabbitTemplate.send(message);
}
/* @RabbitListener(queues = HelloWorldConfiguration.helloWorldQueueName)
public void handleMessage(User user) {
System.out.println("User Values::::::::"+user.getPassword());
}*/
}
消费
package com.bip.rabbitmq.consumer;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import com.bip.entity.User;
@EnableRabbit
@Component
public class RabbitMQJobListener {
@RabbitListener(queues="Job Queue")
public void onMessage(User message) {
System.out.println(new String(message.getPassword()));
}
}
的RabbitMQ
Exchange (AMQP default)
Routing Key Job Queue
Redelivered ○
Properties
priority: 0
delivery_mode: 2
headers:
user: user
__TypeId__: org.springframework.amqp.helloworld.User
content_encoding: UTF-8
content_type: application/json
Payload
57 bytes
Encoding: string
{"userName":"welcome","password":"welcome","xml":"myxml"}
答案 0 :(得分:3)
在出站时设置_TypeID_
标头,以告知入站要将JSON转换为哪个类。如果要转换为与JSON类型兼容的其他类,则必须配置转换器。
如果它始终是同一个类,请使用自定义ClassMapper
(不是默认值)。
或者,请参阅this test,其listener和its configuration,了解如何配置不同的typeid映射。