Java execution in background -
i have developed app executes sql jobs. when click on execute button application goes running state , halts untill query executed.
i want app should not halt , user should able enter other query , query execution should run in background.
my question how run execution of queries in background? means when execute button clicked ,the remaining execution should run behind screen.
my app developed using struts1.3 framework.i have written 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(); (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 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 date() date date = new date(); system.out.println(dateformat.format(date)); string query4 = "select executor_id,email_id m_executor 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 enter world of threading.
to run task in background need start task on separate thread. if running in swing app need ensure not running task on event-dispatcher thread.
have @ swingutilities invokelater.
Comments
Post a Comment