| |
Struts Tutorials:
Struts2 Tag CheckBoxList , Checkbox, Iterator, IF
Struts2 Tag Library Example
Struts2 Tiles Example
Struts2 Tiles I18N Example
Struts2 Questions
Struts Tiles I18N Example
Struts Eclipse MVC
Struts2 Tags
Struts2 Example and Tutorial
Struts MVC
Struts2 Validation
Hibernate Tutorials:
Hibernate Case Study
Class Hierarchy Persist Example
Using Hibernate Interceptor
Hibernate Questions with Answer
Hibernate Many-to-Many Mapping Example
Hibernate one-to-many Mapping Example
Hibernate and ORM tools
Spring Hibernate Example
Hibernate SessionFactory Example
Hibernate Mapping Class Hierarchy
Hibernate Questions
Hibernate SessionFactory Questions
Spring Hibernate Example:
Spring Hibernate Case Study
Written By : Amit
Title :
Composite keys in Hashtable
Description :
key and value pair More...
|
Written By : Amit
Title :
Customizing JTree using Swing
Description :
AWT using Java Technology More...
|
Written By : Amit
Title :
Event handling and Java Technology
Description :
Event handling and Java Technology More...
|
Written By : admin
Title :
Using Final Keyword
Description :
Example on using Final Keyword in Java code More...
|
Written By : Amit
Title :
In memory AWT Image Creation
Description :
Java Code More...
|
Written By : Amit
Title :
Java Integral Types
Description :
Java Types with Bit size More...
|
| Tags/Keywords : Java-Output-Stream,Java, output, Stream, batch, example Author : Amit Date (Year/Month/Date): 2010-04-08
Downloading a large set of records from database
using Java output stream | |
Please be informed that NONE of the design/code/matter from this page is claiming to be some
sort of best practice and we DO NOT expect any of our visitor/reader of this page to assume
this as some sort of best practice for any context and should not be using this as it is
without appropriate evaluation.
This page intends only to provide bits and pieces of known ways for doing some sort of example
and may not be fit for any other purpose.
In spite of all precautions taken to provide accurate and avoid any typo in these pages,
there might be some issues like grammatical mistakes and typo being observed in these pages,
We extend our sincerest apologies for the same.
In this page I am going to show a very specific case study
about the way I could solve a performance bottle-neck in one
of my past academic project using Java Platform.
I had to find out the logic that is using most of the memory
allocated to the server process. This is a very simple project
with a screen to display a search screen with a date range,
start date and end date. User has to select both the start
and end date and search for all the students studied the program
within the date range. Initially number of students studied the
programme for a date range of one month was very less, but
after using this system for more than couple of years, number
of records grew exponentially and the logic used for fetching
all the records at once by a select statement resulted in
ten thousands records. And one of the functionality was to provide
user of this system to be able to download all the records in form
of a zipped file (yes a single zipped file containing all the
records). When I performed a thorough analysis on code, found
that the logic for creating a zipped file takes the complete
data in form of a List and then creates a zipped file and then
channels this complete file to the response print writer object.
So this way when complete data is passed to the web server memory,
there was potential server crash/hang was observed.
What to think? How to solve this particular issue?
These are the questions keep coming to my mind. finally after
brain storming for a day or two, I decided to do a POC on
a particular idea that came to my mind, I was not knowing
whether this will work at all or not.
This idea is not so rare, you might be thinking this while
reading this page. I decided to play around with input output
streams with batch of operation strategy.
I thought, I shall be creating a buffer output stream on top of
the ServletOutputStream object borrowed from response object,
then writing certain predefined bytes of data taken from a
batch fetch operation at data access layer. So there will be
a stream of bytes flown form the data layer to web server layer,
then this stream gets converted to a zipped output stream, and
then this zipped outputstream will be flushed to the browser with
a zipped file as the content-disposition, and content-type
appropriately defined in response header.
At first it sounds little bit confusing, isn't it!!! It did
sound bit confusing, so I started coding a POC to see
whether this is practically feasible or not within the
platform / technology stack this particular system is running
on.
I decide to write a servlet that creates a ServletOutputStream
and then sets some of those standard response setContentType
and setHeaders, then creates a BufferedOutputStream by using
the above servlet output stream as a parameter in constructor,
then there will be flushing of bytes at regular intervals.
For reducing complexities I decided to refrain from creating
the zipped output stream for some other POC :).
This following file is tested in a very limited runtime
environment, it may or may not work, and is not intended
to be some sort of advise or suggestions from my side.
it is just an example code without any intelligence, so
if use it , use it at your own risk please.
ExampleZippedDownload.java
package example;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class ExampleZippedDownload extends HttpServlet
{
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException,
ServletException {
try
{
response.setContentType("application/vnd.ns-csv");
response.setHeader("Pragma","public");
response.setHeader("Cache-Control","public");
response.setHeader("content-disposition","attachment;filename=records.csv");
ServletOutputStream sout = response.getOutputStream();
//buffer capacity is defined to be 10 as of this
//example.
BufferedOutputStream bout = new BufferedOutputStream(sout,10);
fetchRecords(bout);
System.out.println("completed downloading....");
}
catch (Exception ex)
{
ex.printStackTrace();
//Not thinking about proper exception
//handling at it is not in scope for
//this example :)
}
}
public void fetchRecords(BufferedOutputStream bout) throws Exception {
byte[] rwRecords = "Student, records".getBytes();
int i=0;
while(i<100){
//this is a place where one can pug in
//fetching records from database in
//batch operation.
bout.write(rwRecords,0,rwRecords.length);
bout.flush();
System.out.println("server processing...");
Thread.sleep(1000);
i++;
}
}
}
| |
|
|
|
|
| Home >>> Coding Tips on Java >>> Java Output Stream >>> Example Case Study POC |
|
|
Visitor/User referred related external URL:
(Visible upon review and approved by this site Administrator)
|
|
|
|
|
<- requires login | Log in or Register | |
Copyright © 2008-2010, Interview-Questions-Tips-Forum, All Rights Reserved. | CONTACT PRIVACY POLICY DISCLAIMER |
| |  |
Oracle and Java are registered trademarks of Oracle and/or its affiliates.
Other names may be trademarks of their respective owners
This web site's Terms of Use and Disclaimer :
This web site provides some of the information about various technologies, example code, tips, tutorials etc. Like any printed materials,
content of these pages may become out of date over a period of time. Therefore all visitor/users of this web site are requested/advised
to refer to the originating parties/sources for the latest changes and happenings for detailed information. This information is not
intended to be a substitute for the original reference provided by the originating parties/sources. These examples with source code or
without source code, have not been thoroughly tested under all conditions. Interview-questions-tips-forum.net therefore, cannot guarantee
or imply reliability of these example source code or programs.
By accessing and using this website in any ways, including, without limitation, browsing the website pages, using any information, using
any content and/or downloading any materials, you agree to and are bound by the terms of use described in this page and Usage Terms and Conditions.
If you do not agree to all of the terms and conditions contained in the terms of use described in this page and
Usage Terms and Conditions, do not use this website in any manner. If you are using the website on behalf of your
employer, you represent that you are authorized to accept these Terms of Use on your employer's behalf.
All Trademarks are property of their respective owner. Appropriate measure is being taken for providing accurate and up-to-date
information but like any printed materials, these blog(s)/contents may eventually be outdated one day, so if you are using any of these
information, please refer original content/documentation from respective sources. And under no circumstances shall the Author of these
contents and/or this web site be liable for any loss, damage, expense incurred or suffered which is claimed to have occurred because of
usage of the contents of this web site. If you have any questions/queries/feedback/suggestions then please write to this web site owner
at contact.
|  |
|
|
|
|
|