views:

31

answers:

2

I have a ejb3 session bean and a servlet. The bean have access to a database with some big table. The servlet should retrieve the table's content from the bean and send data through ServletOutputStream. How I can transfer big data between ejb3 bean and servlet? I can't return a list with all rows at once because it doesn't fit in memory.

PS. The data are downloaded as a file. They do not appear on a page.

A: 

You have to chunk it into managable pages.

This shouldn't be a problem, because the truth is that users don't want a large number of rows at one time. Both Google and Stackoverflow are good examples of how this works: both return responses 10-25 at a time. Google might get a billion hits for your query, but they're betting that the ones with the highest page rank will satisfy your need best. Stackoverflow returns the most recent questions and lets you page through.

Your app should do this, too.

duffymo
The data are downloaded as a file. They do not appear on a page.
Denis Kostousov
Downloaded from the servlet to a browser? Or directly written to local file system?
duffymo
A: 

You can transfer large files across the EJB boundary by using the Externalizable inteface (an extension of Serializable). This is what I suggest:

  1. Write a class eg ExternalFile that wraps/contains a File object
  2. Make that class implement Externalizable
  3. implement the writeExternal to write the contained file to the given output stream
  4. Implement the readExternal to read the given input stream into a new temp file
  5. Consider adding size and name attributes to your ExternalFile class to help the receiver decide what to do (and remember to write these out and read these in via the writeExternal and readExternal methods).

Step 3 is your serializing stage for when you are sending your object (file) from the EJB layer. Step 4 is your de-serializing stage which receives the file as a data-stream and can do whatever it wants with the stream. My 4 suggests writing it to a temp file but you could pass this stream directly via your servlet to any other destination.

Hope that helps.

jowierun