2014-03-13 66 views
1

我正在创建一个具有一个Upload按钮(用于上载XLS并稍后在数据库中更新此数据)的JSP页面。只要用户点击Upload按钮,它就会读取XLS文件并准备一个将传递给方法(m1(List a))的对象列表来执行SQL查询。 现在的问题是,我有这个方法(m1(List a))大约100 SQL查询,大约需要30分钟才能完成。 所以我不希望用户等到此方法完成数据库进程。 有没有什么办法可以调用我的方法来更新数据库,而不用等待这个数据库操作的响应,我可以响应用户该文件已经上传,数据库进程已经启动,将在一段时间后完成。在向java中的服务器发送请求之后不要等待响应

+0

30 _minutes_?看起来像其他地方有一个问题开始...无论如何,这里的解决方案是使用'Executor'并提交数据库操作。 – fge

回答

1

将请求 - 响应周期之外的工作交给ExecutorService

private void doDatabaseWork(Input input) { 
    BackgroundWorkTask task = new BackgroundWorkTask(input); 
    executorService.submit(task); 
    // since the work is now handed off to a separate pool of 
    // threads, the current HTTP-handling thread will continue 
    // here and return a response to the user 
} 

public class BackgroundWorkTask implements Runnable { 
    public void run() { 
     // put all of your database querying operations in here 
    } 
} 

确保,因为这是一个Web应用程序,你有办法关机,当Web应用程序停止ExecutorService的 - 这也将会给ExecutorService的一个机会,允许之前完成任何正在进行的工作容器停止。

相关问题