i have developed an app which executes sql jobs.
When I click on execute button the my application goes into running state and it halts untill the query is executed.
I want my app should not halt and the user should be able to enter other query and those query execution should run in background.
My question is how to run the execution of queries in background?
means when the execute button is clicked ,the remaining execution should run behind the screen.
My app is developed using struts1.3 framework.I have written the main functionality in execute() of Action Class
Code snippet of execute()
DAO dao1=new DAO();
System.out.println("Here...1");
con1=dao1.DBConnection(jndiname);
Statement st = con1.createStatement();
//status_id=1;
ResultSet rs = st.executeQuery(query);
System.out.println("Here...2");
String id = Long.toString(System.currentTimeMillis());
//int req_id = System.currentTimeMillis();
String dirTree= rsBundle.getString("CSV_DIR");
File f=new File(dirTree);
String[] directories = dirTree.split("/");
String[] lists=f.list();
for (String dir : directories )
{
if (!dir.isEmpty() )
{
if (f.exists())
{
System.out.println("directory exist");
}
if (!f.exists())
{
boolean success = (new File(dirTree).mkdirs());
if(success)
{
System.out.println("directory created");
}
}
}
}
for(String s:lists)
{
System.out.println("files.." + s);
}
String csv_file_path=dirTree+"/";
String csv_file_name=id +".csv";
//writing to csv file
CSVWriter writer = new CSVWriter(new FileWriter(csv_file_path + csv_file_name), ',',CSVWriter.NO_QUOTE_CHARACTER);
writer.writeAll(rs, true);
writer.close();
//status_id=7;
String zip_file_path=rsBundle.getString("zip_file_path");
String zip_filename=id + ".zip";
String zip_file_pwd=rsBundle.getString("zip_file_pwd");
//zip file creation
ZipUtil.zipDirWithPassword(dirTree, zip_file_path + zip_filename,zip_file_pwd);
String ftp_file_path=rsBundle.getString("ftp_file_path");
long zip_file_size= new File(zip_file_path + zip_filename).length();
System.out.println("File size..inside" + zip_file_size);
System.out.println("Here...3");
String exec_id=(String)request.getSession().getAttribute("userLoginId");
//int executor_id= Integer.parseInt(exec_id);
DateFormat dateFormat = new SimpleDateFormat("mm/dd/yyyy");
//get current date time with Date()
Date date = new Date();
System.out.println(dateFormat.format(date));
String query4 = "select executor_id,email_id from m_executor where windows_id = '" + exec_id + "'";
System.out.println("Query... " + query4);
//int i=0;
iPreparedStatement4=con.prepareStatement(query4);
iResultSet3=iPreparedStatement4.executeQuery();
while(iResultSet3.next())
{
//restriction=iResultSet2.getString(1);
exec_email=iResultSet3.getString(2);
executor_id=iResultSet3.getInt(1);
}
ValueListForExec db= new ValueListForExec();
String status_name="";
status_name=db.getStatusName(status_id);
if(zip_file_size <= 5242880){
System.out.println("send via email");
/*}
else
{*/
System.out.println("send via FTP");
upload.upload(host, usrname, pwd,zip_file_path + zip_filename, ftp_file_path,zip_filename);
}
String insertquery="{ call sp_process_job (?,?,?,?) }";
cs = con.prepareCall(insertquery.toString());
cs.setString(1,id);
cs.setString(2,host);
cs.setString(3,usrname);
cs.setString(4,pwd);
cs.execute();
con.commit();
You are about to enter the world of threading.
To run a task in the background you need to start that task on a separate thread.
If you are running in a Swing app you will need to ensure that you are not running the task on the event-dispatcher thread.
Have a look at SwingUtilities invokeLater.