【发布时间】:2015-08-28 05:59:07
【问题描述】:
我正在开发一个 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 时,这些是值。谁能告诉我这个对象是否正确?
【问题讨论】:
-
您的代码中是否启用了记录器?根据您的评论,对象读取可能存在异常。
-
那里必须在那条线上抛出异常。 JVM 不只是随机停止执行代码。您是否尝试过使用调试器单步执行代码以查看实际发生的情况?
-
你的意思是 try catch 吗?我不明白你所说的记录器是什么意思。
-
@Raniz 整个代码都被 try catch 包围,我正在捕捉 Exception 。仍然没有任何反应。
-
请告诉我们你的捕获块,@ViChU