BIn*_*hav 3 spring rabbitmq spring-amqp spring-boot
我正在尝试使用带有jackson2JsonMessageConverter. 另外,_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.to DefaultJackson2JavaTypeMapper.java:90) 在 org.springframework.amqp.support.converter.Jackson2JsonMessageConverter.fromMessage(Jackson2JsonMessageConverter.java:145) 在 org.springframework.amqp.rabbit.listener.adapter.AbstractAdaptableMessageAbstractMessageAbstractMessage(3)在 org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter。
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>
Run Code Online (Sandbox Code Playgroud)
发件人
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());
}*/
}
Run Code Online (Sandbox Code Playgroud)
消费者
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()));
}
}
Run Code Online (Sandbox Code Playgroud)
兔MQ
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"}
Run Code Online (Sandbox Code Playgroud)
当序列化实例的包名称与消费者的模型(由标头 TypeId 表示)不同时,可能会发生这种情况。
我相信下面的例子会让事情变得更清楚。
架构:扇出类型的交换 x.invoice 绑定到队列 q.invoice。
生产者:我们正在发送类型 Id com.example. Produceronequeuemultipletypes.model.InvoiceCreatedMessage 的 JSON 消息。ParseConfig类是帮助我们避免手动将实例序列化为String。
public void sendInvoiceMessages() {
invoiceCreatedMessage.setId(0);
invoiceCreatedMessage.setType("Invoice Created");
rabbitTemplate.convertAndSend("x.invoice", "", invoiceCreatedMessage);
}
class InvoiceCreatedMessage {
private String type;
private int id;
}
@Configuration
class ParseConfig {
@Bean
public ObjectMapper getObjectMapper() {
return new ObjectMapper();
}
@Bean
public Jackson2JsonMessageConverter getConverter(
@Autowired ObjectMapper objectMapper) {
return new Jackson2JsonMessageConverter(objectMapper);
}
}
Run Code Online (Sandbox Code Playgroud)
消费者:创建一个类映射器 bean,映射从“com.example. Produceronequeuemultipletypes.model.InvoiceCreated”到 InvoiceCreated.class。
@Slf4j
@Service
public class InvoiceConsumer {
@RabbitListener(queues = "q.invoice")
public void handleInvoiceCreated(
InvoiceCreatedMessage invoiceCreatedMessage) {
log.info("[Created] Invoice " + invoiceCreatedMessage);
}
}
@Configuration
class ParseConfig {
@Bean
public ObjectMapper getObjectMapper() {
return new ObjectMapper();
}
@Bean
public Jackson2JsonMessageConverter getConverter(
@Autowired ObjectMapper objectMapper) {
Jackson2JsonMessageConverter messageConverter =
new Jackson2JsonMessageConverter(objectMapper);
messageConverter.setClassMapper(getClassMapper());
return messageConverter;
}
@Bean
public DefaultClassMapper getClassMapper() {
DefaultClassMapper classMapper = new DefaultClassMapper();
Map<String, Class<?>> map = new HashMap<>();
map.put(
"com.example.produceronequeuemultipletypes.model." +
"InvoiceCreatedMessage",
InvoiceCreatedMessage.class)
classMapper.setIdClassMapping(idClassMapping);
return classMapper;
}
}
class InvoiceCreatedMessage {
private String type;
private int id;
}
Run Code Online (Sandbox Code Playgroud)
参考:
| 归档时间: |
|
| 查看次数: |
4686 次 |
| 最近记录: |