• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Transfering 3rd party (unserialized) objects through web server.

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have client-server application where client calls server through web server and servlets. The servlet invoke methods deployed on web server and they query SQL database to retrieve the results. Results are populated in a instance of serialized class (DAO.java) and all the instance of dao.java are added to the List and transferred back to client. No issues.

Beside that I have some additional processing to be done along with SQL query. For such processing, I am using 3rd party software and it returns the instance of 3rd part API. Now I also need to transfer the 3rd party instance, which apparently is not serialized, as well along with list.

I am getting the error that the instance I am sending over the web server is not serialized. I tried to add 3rd party instance to instance of dao.java which is serialized but it does not work either.

Is there any workaround I can do to transfer this un-serialized object.

I will really appreciate any suggestions.

Thanks a bunch.
 
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Art Thak wrote:I tried to add 3rd party instance to instance of dao.java which is serialized but it does not work either.



if you add a non-serializable member object to a serializable object, it won't work. The member object has to be serializable as well (or declared transient).

I think you need to extend that 3rd party class and implement serializable in the subclass. then, you need to write your own implementations of the readObject and writeObject methods.



You are working with someone else's object. If you use this way to serialize it and then it changes, it could cause problems.
Also, this only works if the superclass ( 3rdPartyObject ) has a no-arg constructor (as your custom code will have to call it to deserialize)
the solution to this, then, is to use Externalizable interface.
This is discussed previously on javaranch:
https://coderanch.com/t/475650/Programmer-Certification-SCJP/certification/serializing-when-subclass-implements-Serializable
 
A Thakur
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tim: Thanks for suggestion and url.

My problem is little complicated, Here's my problem:

3rd party class: (unserialized)





Subclass:



Another Class: Gateway.java




I want to capture all the properties of QueryResult class, plus additional properties for SubclassDAO set by Gateway.java and pass to the client.

I understand above readExtrenal() and writeExternal() method implementation is not correct here.

Few things I tried:

- Client is able to get projectName but not the queryResult object. out.writeObject(queryResult); in subclass implementation throws error compianing the queryResult is not serialized.
- I cannot do SubclassDAO dao = (SubClassDAO)result; in Gateway,java as this throws me type cast exception.

Please suggest. How can I make this work by proper implementation of readExternal or writeExternal


How can I do that.


 
A Thakur
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One way is to read all properties of QueryResult in Gateway.java and assign it to SubClassDao instance and then send it client.

I should mention there is a strict need transfer the QueryResult object to the client. Reading all the properties in Gateway.java (server side) is very time consuming. Reason I want to transfer it to client, so then client receives response from server it can run background thread to get all properties of QueryResult and use it where necessary. So that server side resources can be saved.

Please help.

Thanks a million.
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These restrictions make it not likely you can do this. You will never be able to send QueryResult itself to the server in a java.io stream because it isn't serializable.
You also can't, per your restrictions, read the data out of QueryResult into another class and send that.

I may have led you astray with externalizable because I didn't realize the requirement to actually send QueryResult over the stream.
 
Today's lesson is that you can't wear a jetpack AND a cape. I should have read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic