Code after readObject () does not run

I am working on a UI that reads a serialized object from a zookeeper, deserializes it, and then converts it to JSON. For some reason I am unable to deserialize the MQTopic object. But I can do the same with other objects.

This is the part that converts the [] byte to an MQTopic object.

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();

            }

      

Here, the value is the byte array of the object after serialization. Nothing starts after topic = (MQTopic) is.readObject();

. Even print statements. The program is neither terminated, nor is an exception thrown or thrown.

EDIT : The whole method

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;
}

      

The FTP if protocol works great, but the Subject loop doesn't work outside of typing.

EDIT 2: How does another command store the object in Zookeeper

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

      

The [] byte is stored in Zookeeper and this is what I retrieve in my interface.

EDIT 3: I have debugged the process and at the point where it is called these are the values. Can anyone tell me if the object is correct?

is object

+3


source to share


2 answers


You are doing it wrong. You must deserialize the object first and then use instanceof

to find out what type it is. Converting binary data to String

is bad practice at the best of times.



Your actual symptom is untrustworthy. The exception must be thrown, otherwise you block earlier than specified.

0


source


ObjectInputStream readObject is a blocking method. First check with the available method if there is anything to read without blocking.

will most likely return 0 in this case.



This may only be half of the solution you are looking for, but I think it will tell you if you have anything to read or not.

0


source







All Articles