Code after readObject() does not run

901 views Asked by At

I am working on a UI which reads serialized object from zookeeper, deserializes it and then converts it into JSON. For some reason I am not able to deseerialize the MQTopic object. But I am able to do the same with other objects.

This is the part which converts the byte[] into the 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 value is a byte array of the object after serialization. Nothing runs after topic = (MQTopic) is.readObject(); . Not even the print statements. The program neither terminates nor an exception is thrown or caught.

EDIT : 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 loop works fine, but the Topic loop does not work beyond typecasting.

EDIT 2: This how the other team stores the object into 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 that is what I am retrieving in my UI.

EDIT 3: I did a debug of the process and at the point where is is called, these are the values. Can anyone tell me if the object is right ?

is object

2

There are 2 answers

1
user207421 On

You're doing this wrong. You should deserialize the object first, and then use instanceof to see what type it is. Converting binary data to String is poor practice at the best of times.

Your actual symptom is not credible. An exception must be thrown, or else you are blocking earlier than stated.

5
rudedude On

ObjectInputStream's readObject is a blocking method. First check using the available method if there is something to read without blocking.

available would most probably return 0 in this case.

This might be only half the solution you are looking for, but I think this would let you know if you have anything to read or not.