gpt4 book ai didi

java - AMQP(RabbitMQ) 无法反序列化对象,ClassNotFoundException

转载 作者:行者123 更新时间:2023-11-29 04:43:22 31 4
gpt4 key购买 nike

我想在 java SpringBoot 中实现 RabbitMq(AMQP) 消息传递,但是当我收到消息时它说无法反序列化消息,因为即使我没有找到我应该接收的对象的类在类路径中包含该类。

RabbitMqListener.java:

@EnableRabbit
@Component
public class RabbitMqListener {
Logger logger = Logger.getLogger(RabbitMqListener.class);

@RabbitListener(queues = "queue2")
public void processQueue1(Product message) {

logger.info("Received from queue 2: " + message);
}

}

RabbitConfiguration.java:

@Configuration
public class RabbitConfiguration {
Logger logger = Logger.getLogger(RabbitConfiguration.class);
@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory =
new CachingConnectionFactory("localhost");
return connectionFactory;
}

@Bean
public AmqpAdmin amqpAdmin() {
return new RabbitAdmin(connectionFactory());
}

@Bean
public MessageConverter jsonMessageConverter(){
return new JsonMessageConverter();
}

@Bean
public RabbitTemplate rabbitTemplate() {
RabbitTemplate template = new RabbitTemplate(connectionFactory());
template.setMessageConverter(jsonMessageConverter());
return template;
}


@Bean
public Queue myQueue1() {

return new Queue("queue1");
}

@Bean
public Queue myQueue2() {

return new Queue("queue2");
}

}

SampleController.java:

@Controller
public class SampleController {
Logger logger = Logger.getLogger(SampleController.class);

@Autowired
AmqpTemplate template;

@RequestMapping("/emit")
@ResponseBody
String queue1() {
logger.info("Emit to queue1");
template.convertAndSend("queue1","Message to queue 1");
return "Emit to queue 1";
}
}

Product.java:

public class Product implements Serializable{

private Long id;

private String name;

private int stock;

private int price;



public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getStock() {
return stock;
}

public void setStock(int stock) {
this.stock = stock;
}

public Product() {
}

@Override
public String toString() {
return "Product{" +
"id=" + id +
", name='" + name + '\'' +
", stock=" + stock +
", price=" + price +
'}';
}
}

和堆栈跟踪:

2016-07-11 09:34:02.840  WARN [order-service,,,] 4084 --- [cTaskExecutor-1] s.a.r.l.ConditionalRejectingErrorHandler : Execution of Rabbit message listener failed.

org.springframework.amqp.rabbit.listener.exception.ListenerExecutionFailedException: Listener threw exception
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.wrapToListenerExecutionFailedExceptionIfNeeded(AbstractMessageListenerContainer.java:865) ~[spring-rabbit-1.5.6.RELEASE.jar:na]
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:760) ~[spring-rabbit-1.5.6.RELEASE.jar:na]
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:680) ~[spring-rabbit-1.5.6.RELEASE.jar:na]
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$001(SimpleMessageListenerContainer.java:93) [spring-rabbit-1.5.6.RELEASE.jar:na]
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$1.invokeListener(SimpleMessageListenerContainer.java:183) ~[spring-rabbit-1.5.6.RELEASE.jar:na]
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.invokeListener(SimpleMessageListenerContainer.java:1358) [spring-rabbit-1.5.6.RELEASE.jar:na]
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:661) ~[spring-rabbit-1.5.6.RELEASE.jar:na]
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.doReceiveAndExecute(SimpleMessageListenerContainer.java:1102) [spring-rabbit-1.5.6.RELEASE.jar:na]
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.receiveAndExecute(SimpleMessageListenerContainer.java:1086) [spring-rabbit-1.5.6.RELEASE.jar:na]
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$1100(SimpleMessageListenerContainer.java:93) [spring-rabbit-1.5.6.RELEASE.jar:na]
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1203) [spring-rabbit-1.5.6.RELEASE.jar:na]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_91]
Caused by: java.lang.IllegalStateException: Could not deserialize object type
at org.springframework.amqp.utils.SerializationUtils.deserialize(SerializationUtils.java:82) ~[spring-amqp-1.5.6.RELEASE.jar:na]
at org.springframework.amqp.support.converter.SimpleMessageConverter.fromMessage(SimpleMessageConverter.java:110) ~[spring-amqp-1.5.6.RELEASE.jar:na]
at org.springframework.amqp.rabbit.listener.adapter.AbstractAdaptableMessageListener.extractMessage(AbstractAdaptableMessageListener.java:185) ~[spring-rabbit-1.5.6.RELEASE.jar:na]
at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter$MessagingMessageConverterAdapter.extractPayload(MessagingMessageListenerAdapter.java:173) ~[spring-rabbit-1.5.6.RELEASE.jar:na]
at org.springframework.amqp.support.converter.MessagingMessageConverter.fromMessage(MessagingMessageConverter.java:118) ~[spring-amqp-1.5.6.RELEASE.jar:na]
at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.toMessagingMessage(MessagingMessageListenerAdapter.java:102) ~[spring-rabbit-1.5.6.RELEASE.jar:na]
at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.onMessage(MessagingMessageListenerAdapter.java:88) ~[spring-rabbit-1.5.6.RELEASE.jar:na]
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:757) ~[spring-rabbit-1.5.6.RELEASE.jar:na]
... 10 common frames omitted
Caused by: java.lang.ClassNotFoundException: com.productservice.model.Product
at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_91]
at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_91]
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[na:1.8.0_91]
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_91]
at org.springframework.util.ClassUtils.forName(ClassUtils.java:250) ~[spring-core-4.2.7.RELEASE.jar:4.2.7.RELEASE]
at org.springframework.core.ConfigurableObjectInputStream.resolveClass(ConfigurableObjectInputStream.java:75) ~[spring-core-4.2.7.RELEASE.jar:4.2.7.RELEASE]
at org.springframework.amqp.support.converter.SimpleMessageConverter$1.resolveClass(SimpleMessageConverter.java:179) ~[spring-amqp-1.5.6.RELEASE.jar:na]
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1620) ~[na:1.8.0_91]
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1521) ~[na:1.8.0_91]
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1781) ~[na:1.8.0_91]
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1353) ~[na:1.8.0_91]
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:373) ~[na:1.8.0_91]
at org.springframework.amqp.utils.SerializationUtils.deserialize(SerializationUtils.java:76) ~[spring-amqp-1.5.6.RELEASE.jar:na]
... 17 common frames omitted


Process finished with exit code -1

最佳答案

在代码片段中,您注册了 jsonMessageConverter,同时您使 Person.java 可序列化。您应该使用序列化或 json 方法。而且问题出在Person.java的包结构上。我遇到了同样的问题,通过对生产者和消费者中传输的对象采用相同的包结构来解决它。这背后的原因是java在序列化时考虑了类名,包结构等信息,所以在消费者端我们需要有相同的包结构。我在使用jsonMessageConverter时遇到了同样的问题。因此,从我的角度来看,解决此问题的方法是将您的消息对象打包到一个 jar 中,并将该 jar 作为依赖项添加到您的生产者和消费者项目中。

关于java - AMQP(RabbitMQ) 无法反序列化对象,ClassNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38300784/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com