2015-06-12 26 views
3

我正在从zookeeper读取序列化对象的UI上进行反序列化,然后将其转换为JSON。出于某种原因,我无法取消对MQTopic对象的解除关联。但是我能够对其他对象做同样的事情。readObject()之后的代码不运行

这是将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(); 

      } 

这里是序列化后的对象的字节数组。 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,如果环路正常工作,但主题循环不超过类型转换工作。

编辑2:这给其他球队存储对象到动物园管理员

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

字节[]存储在动物园管理员,这就是我在我的UI我检索。

编辑3:我做了一个调试进程,并在被调用的地方,这些是值。任何人都可以告诉我,如果对象是正确的?

is object

+0

您的代码中是否启用了记录器?根据您的评论,可能在读取对象时出现异常。 – Arvind

+4

*有*是该线上的异常。 JVM不会随机停止执行代码。您是否尝试过使用调试器来查看实际发生的事情? – Raniz

+0

你的意思是试试看?我不明白记者的意思。 – v1shnu

回答

0

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

您的实际症状不可信。必须抛出异常,否则你比提到的要早。

+0

是的,我知道,这只是一个原型,并不是最终的代码。现在,我没有任何问题进入循环。 – v1shnu

0

ObjectInputStream的readObject是一个阻塞方法。如果有东西没有被阻塞,首先使用available方法检查。

可能会在这种情况下最有可能返回0。

这可能只是您寻找的解决方案的一半,但我认为如果您有任何需要阅读或不需要的东西,这会让您知道。

+0

我已经试过可用,并且它返回两个对象即0。 FTP和主题。但我仍然没有任何FTP问题。 – v1shnu

+0

我明白了。这意味着,字节数组没有任何可读的。主题反序列化块中的字节数组长度(this.value)是多少? –

+0

长度为772. – v1shnu

相关问题