2015-06-12 25 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

相關問題