在 JSF 中单击按钮异步调用长 运行 进程
Calling a long running process asynchronously on a button click in JSF
我想通过单击用 JSF 和 Java 开发的按钮来执行存储过程。该过程执行大约需要 30 分钟。
当用户单击此按钮时,s/he 应该会收到一条默认消息,例如 -
Data is being processed. Kindly login after 60 minutes to check the data.
现在,当我们单击按钮时,UI 页面在执行过程时挂起。
是否有解决方法来显示此默认消息和 运行 后端的过程?
您可以在单击按钮时调用可以使用 Future or FutureTask 的方法,在 java.util.concurrent 包中可用。前者是接口,后者是Future接口的实现。
通过在您的代码中使用“Future”,您的异步任务将立即执行,并保证结果在将来可供调用线程使用。
看看这个link:Is it safe to start a new thread in a JSF managed bean?
也看看这个 link:https://codereview.stackexchange.com/questions/39123/efficient-way-of-having-synchronous-and-asynchronous-behavior-in-an-application
只需触发一个 @Asynchronous
EJB 方法。它会触发并忘记一个单独的线程,bean 操作方法将立即 return.
@Named
public class Bean {
@EJB
private Service service;
public void submit() {
service.asyncDoSomething();
// Add message here.
}
}
@Stateless
public class Service {
@Asynchronous
public void asyncDoSomething() {
// ...
}
}
另请参阅:
- Is it safe to start a new thread in a JSF managed bean?
- How can server push asynchronous changes to a HTML page created by JSF?
我想通过单击用 JSF 和 Java 开发的按钮来执行存储过程。该过程执行大约需要 30 分钟。
当用户单击此按钮时,s/he 应该会收到一条默认消息,例如 -
Data is being processed. Kindly login after 60 minutes to check the data.
现在,当我们单击按钮时,UI 页面在执行过程时挂起。
是否有解决方法来显示此默认消息和 运行 后端的过程?
您可以在单击按钮时调用可以使用 Future or FutureTask 的方法,在 java.util.concurrent 包中可用。前者是接口,后者是Future接口的实现。
通过在您的代码中使用“Future”,您的异步任务将立即执行,并保证结果在将来可供调用线程使用。
看看这个link:Is it safe to start a new thread in a JSF managed bean?
也看看这个 link:https://codereview.stackexchange.com/questions/39123/efficient-way-of-having-synchronous-and-asynchronous-behavior-in-an-application
只需触发一个 @Asynchronous
EJB 方法。它会触发并忘记一个单独的线程,bean 操作方法将立即 return.
@Named
public class Bean {
@EJB
private Service service;
public void submit() {
service.asyncDoSomething();
// Add message here.
}
}
@Stateless
public class Service {
@Asynchronous
public void asyncDoSomething() {
// ...
}
}
另请参阅:
- Is it safe to start a new thread in a JSF managed bean?
- How can server push asynchronous changes to a HTML page created by JSF?