ObjectInputStream and unknown object

Discussion in 'Java' started by David Levitan, Mar 16, 2006.

  1. I'm having an issue with ObjectInputStreams. I have two JVM instances
    running, communicating via ObjectInputStream/ObjectOutputStreams. Call
    them instance A and instance B.
    The basic way the program works is that A sends B some data, B does
    something to it, and sends back a result that is an object containing
    several other objects. Until today, this has worked fine, but all
    objects being sent back and forth were either part of the code base for
    A and B or a standard java package.
    Today a situation came up where B is sending back an object that is a
    subclass of one of the classes in A/B's codebase, but is not in the
    codebase itself. Based on my debugging, B sends the result (which
    contains this object) successfully, but A just hangs trying to read the
    object. The new object is in B's classpath but is not in A's classpath
    This new object seems to be the reason for the hang, since I haven't
    seen this behavior before. I'm wondering if anyone can comment on this
    and whether dynamically changing the classpath would solve the issue, or
    whether there is a more elegant way of solving the issue.
    I'll be happy to provide more details if needed.

    Thanks,
    David
     
    David Levitan, Mar 16, 2006
    #1
    1. Advertising

  2. David Levitan

    Ike Guest

    Hi David,

    Have you tried wrapping the Object(s) passed in a collections class like an
    ArrayList, then merely pass the ArrayList, looking therefore for an
    ArrayList on the other side?

    Only when you iterate through the ArrayList after receiving it, the
    unknown-classed object should then throw an exception of some sort,and you
    might be able to try-catch it there, after the OutPutStreams have completed
    their work?

    HTH -Ike
     
    Ike, Mar 16, 2006
    #2
    1. Advertising

  3. Ike wrote:
    > Hi David,
    >
    > Have you tried wrapping the Object(s) passed in a collections class like an
    > ArrayList, then merely pass the ArrayList, looking therefore for an
    > ArrayList on the other side?
    >
    > Only when you iterate through the ArrayList after receiving it, the
    > unknown-classed object should then throw an exception of some sort,and you
    > might be able to try-catch it there, after the OutPutStreams have completed
    > their work?
    >
    > HTH -Ike
    >
    >



    The actual object being transmitted is a wrapper class - the instance
    receiving the object never gets out of the readObject method. I do know
    adding the previously unknown object to the classpath does make it work
    (I confirmed this yesterday), but I need to change the path dynamically
    and have no idea how to remove directories dynamically (I know how to
    add them).
    Thanks,
    David
     
    David Levitan, Mar 16, 2006
    #3
  4. David Levitan

    Chris Uppal Guest

    David Levitan wrote:

    > Today a situation came up where B is sending back an object that is a
    > subclass of one of the classes in A/B's codebase, but is not in the
    > codebase itself.


    It's a bit tricky to manage this. If the two applications don't have a common
    set of .class files, then you'll have to find some way to do one of two things:

    a) Only transmit objects which /are/ shared. This may force you to convert
    objects from one side or the other into some sort of neutral common form before
    sending them. Not too nice...

    b) Use classloaders to share the code dynamically. Also painful, but perhaps
    not as bad.

    Before getting into that, it's worth pointing out that RMI already has features
    for doing this kind of thing for you, so if you haven't looked at RMI then it
    might be worth doing so.

    Anyway, concentrating on (b). There are two issues here.

    The first is classloaders, what they do, and how they work. If you don't know
    how to use classloaders, then now is the time to find out ;-) Once you have
    done, then you will understand how to use custom classloaders to control almost
    every aspect of where an application looks for class definitions.

    The second issue is how to tell the ObjectInputStream to use your custom
    classloader. That's a lot simpler -- it's just a matter of creating a custom
    subclass of ObjectInputStream which overrides the definition of resolveClass().
    In fact you might not even have to do that, if the class which uses the
    ObjectInputStream was itself loaded by your custom classloader.

    -- chris
     
    Chris Uppal, Mar 17, 2006
    #4
  5. David Levitan

    EJP Guest

    Ike wrote:

    > Have you tried wrapping the Object(s) passed in a collections class like an
    > ArrayList, then merely pass the ArrayList, looking therefore for an
    > ArrayList on the other side?
    >
    > Only when you iterate through the ArrayList after receiving it, the
    > unknown-classed object should then throw an exception of some sort,and you
    > might be able to try-catch it there, after the OutPutStreams have completed
    > their work?


    No. Java still has to deserialize the members of the list, so the
    problem will still arise in readObject().
     
    EJP, Mar 20, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Serial # 19781010
    Replies:
    1
    Views:
    736
    Roedy Green
    Jul 15, 2003
  2. Roedy Green
    Replies:
    2
    Views:
    343
    Roedy Green
    Aug 5, 2003
  3. Lothar Kimmeringer

    Re: creating ObjectInputStream takes forever

    Lothar Kimmeringer, Jul 31, 2003, in forum: Java
    Replies:
    3
    Views:
    629
    Blah Blah
    Aug 1, 2003
  4. R
    Replies:
    0
    Views:
    745
  5. Vincent Arnoux
    Replies:
    1
    Views:
    272
    Arnaud Bergeron
    Aug 11, 2006
Loading...

Share This Page