2012-06-07 34 views
0

我開發了一個執行sql作業的應用程序。 當我點擊執行按鈕時,我的應用程序進入運行狀態,並暫停執行查詢。在後臺執行Java程序

我希望我的應用程序不應該停止,用戶應該能夠輸入其他查詢,並且這些查詢執行應該在後臺運行。

我的問題是如何在後臺運行查詢的執行? 意味着當單擊執行按鈕時,剩餘的執行應該在屏幕後面運行。

我的應用程序是使用struts1.3 framework.I寫的主要功能,在操作類的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(); 
+0

你需要使用線程。我建議使用ExecutorService,因爲它可以更輕鬆地管理任務。 –

+0

http://docs.oracle.com/javase/tutorial/uiswing/concurrency/ –

+0

嘗試研究一下[Thread](http://www.roseindia.net/java/thread/) – Crazenezz

回答

0

您即將進入線程世界。

要在後臺運行任務,您需要在單獨的線程上啓動該任務。 如果您在Swing應用程序中運行,則需要確保您未在​​事件分派器線程上運行任務。

看看SwingUtilities invokeLater。

0

您可以使用ExecutorService或Java開發線程來完成這項工作。您可以在Runnable/Callable對象中編寫sql作業,並且一旦用戶單擊按鈕作業,則應將其傳遞給將在後臺執行的其他線程。即使您可以使用線程池將作業傳輸到池線程。