简体   繁体   中英

How is it to return a very long list from ejb3 session bean?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM