gpt4 book ai didi

java - 通过消息队列进行序列化和反序列化

转载 作者:行者123 更新时间:2023-11-30 06:52:12 26 4
gpt4 key购买 nike

我有一个 Employee 类,如下所示:

package com.mypackage.rabbitmq.model
import java.io.Serializable;

import javax.xml.bind.annotation.XmlRootElement;


@XmlRootElement
public class Employee implements Serializable{

/**
*
*/
private static final long serialVersionUID = -2736911235490297622L;
private int EmpNo;
private String FirstName;
private String LastName;
private int age;
private String gender;
private String skill;
private long phone;
private String email;
private double salary;
//getters and setters

我在rabbit MQ中发布了员工列表如下:

package com.mypackage.rabbitmq.client.publisher;

//imports

public class Publisher {

public static void main(String[] args) throws IOException {
ConnectionFactory factory = new ConnectionFactory();

Connection con = factory.newConnection("localhost");
Channel channel = con.createChannel();

Gson gson = new GsonBuilder().create();
Employee employee = null;

List<Employee> empList = new ArrayList<>();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);

String queueName = "TestQueue";
for(int i=1; i<=10; i++){
employee = newEmp(i);

String message =gson.toJson(employee);
System.out.println("queueName: "+queueName);
empList.add(employee);

}
oos.writeObject(empList);
channel.basicPublish(1, "", queueName, null, bos.toByteArray());
System.out.println("[X], sent '"+empList+"'");

channel.close(0, queueName);
con.close(0, queueName);
}

public static Employee newEmp(int i){

//logic here
}
}

我试图从一个单独的 spring boot 应用程序中获取员工列表。在消费者应用程序中,我有相同的员工类结构。但是包是不同的。

package org.springboot.consumer.model;

import java.io.Serializable;

public class Employee implements Serializable{
//fields and getters-setters here
}

简而言之,消费者代码如下:

public class SDPRabbitMQConsumer implements MessageListener {

@Override
public void onMessage(Message message) {
Gson gson = new GsonBuilder().create();
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
os.write(message.getBody(), 0, message.getBody().length);
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
ObjectInputStream objInputStream = new ObjectInputStream(is);
System.out.println("objInputStream.readObject().toString()"+objInputStream.readObject().toString());
Employee[] employeeArray = gson.fromJson(objInputStream.readObject().toString(), Employee[].class);
List<Employee> employeeList = new ArrayList<Employee>(Arrays.asList(employeeArray));
for(Employee employee: employeeList){
System.out.println(employee);
}
} catch (Exception e) {
e.printStackTrace();
}

}

}

但我得到以下异常:

java.lang.ClassNotFoundException: com.mypackage.rabbitmq.model.Employee

看来是序列化和反序列化的问题。

我的问题是:

  1. 如果我发布 Employee 而不是 List,我什至不需要序列化该类。那为什么我需要在 List 的情况下进行序列化?
  2. 为什么是这个异常(exception)?我们是否需要为两端的序列化类维护相同的包结构?
  3. 在消费者方面,我们需要有 serialVersionUID 吗?如果是,是否应该与发布方的一致?

提前致谢。

最佳答案

1) 虽然 java.util.List 不是可序列化的,但所有标准实现都是可序列化的,因此您应该能够序列化/反序列化 ArrayList 等实例。

2) 是的,你需要同一个包中的同一个类,而且..

3) 两端的类必须有相同的serialVersionUID

关于java - 通过消息队列进行序列化和反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39400010/

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