gpt4 book ai didi

java - readObject() 之后的代码不运行

转载 作者:行者123 更新时间:2023-12-02 05:56:27 26 4
gpt4 key购买 nike

我正在开发一个 UI,它从 Zookeeper 读取序列化对象,反序列化它,然后将其转换为 JSON。由于某种原因,我无法反序列化 MQTopic 对象。但我可以对其他对象执行相同的操作。

这是将 byte[] 转换为 MQTopic 对象的部分。

if (tester != null && tester.contains("com.ibm.mq.jms.MQTopic")) {
System.out.println(getValue());
ByteArrayInputStream in = new ByteArrayInputStream(this.value);
ObjectInputStream is = new ObjectInputStream(in);
System.out.println("after deserializing..");
topic = (MQTopic) is.readObject();
System.out.println("after typecasting..");
System.out.println(topic.getTopicName());
System.out.println(topic.toString());

is.close();
in.close();

}

这里value是序列化后对象的字节数组。topic = (MQTopic) is.readObject(); 之后没有任何内容运行。甚至连打印语句都没有。程序既不会终止,也不会引发或捕获异常。

编辑:整个方法

public String getStrValue() {
FtpConnectionInfo ftp = null;
MQTopic topic = null;
try {
String tester = new String(this.value, "UTF-8");
if (tester != null && tester.contains("FtpConnectionInfo")) {
ByteArrayInputStream in = new ByteArrayInputStream(this.value);
ObjectInputStream is = new ObjectInputStream(in);
ftp = (FtpConnectionInfo) is.readObject();
in.close();
is.close();
Gson gson = new Gson();
return gson.toJson(ftp);

} else if (tester != null
&& tester.contains("com.ibm.mq.jms.MQTopic")) {
ByteArrayInputStream in = new ByteArrayInputStream(this.value);
ObjectInputStream is = new ObjectInputStream(in);
System.out.println("after deserializing..");
topic = (MQTopic) is.readObject();
System.out.println("after typecasting..");
System.out.println(topic.getTopicName());
System.out.println(topic.toString());
is.close();
in.close();

} else {
return new String(this.value, "UTF-8");
}
} catch (UnsupportedEncodingException ex) {
System.out.println("unsupported error ");
ex.printStackTrace();
//logger.error(Arrays.toString(ex.getStackTrace()));
} catch (Exception e) {
System.out.println("Exception in new logic.");
e.printStackTrace();
}
System.out.println("im out of try");

return null;
}

FTP if 循环工作正常,但 Topic 循环除了类型转换之外不起作用。

编辑2:这是其他团队将对象存储到Zookeeper中的方式

public static byte[] serialize(Object obj) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(out);
os.writeObject(obj);
return out.toByteArray();
}

byte[] 存储在 Zookeeper 中,这就是我在 UI 中检索的内容。

编辑3:我对进程进行了调试,在调用它的地方,这些是值。谁能告诉我这个对象是否正确?

is object

最佳答案

你这样做是错误的。您应该首先反序列化该对象,然后使用 instanceof 查看它是什么类型。即使在最好的情况下,将二进制数据转换为 String 也是不好的做法。

你的实际症状不可信。必须抛出异常,否则您将比规定的时间更早阻塞。

关于java - readObject() 之后的代码不运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30799439/

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