我花了几个小时研究我的问题,但我找到了解决方案。
我用@Asynchronous 注释触发了一个线程并返回了一个 Future 对象,到目前为止一切都很好。但是,如果我试图取消未来,似乎该命令不会取消任何东西。
JSF 文件
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
<h:commandButton actionListener="#{threadHandler.start()}" value="start"/>
<h:commandButton actionListener="#{threadHandler.stop()}" value="stop"/>
</h:form>
</h:body>
</html>
Java 处理程序
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.jsf.example;
import java.util.concurrent.Future;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.inject.Named;
@Named("threadHandler")
@Stateless
public class Handler {
Future future;
@Inject
MethodPool pool;
public void start(){
System.out.println("start thread");
future = pool.run();
System.out.println("finish thread start");
}
public void stop(){
System.out.println("future.cancel " + future.cancel(true));
}
}
Java线程
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.jsf.example;
import java.util.concurrent.Future;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.AsyncResult;
import javax.ejb.Asynchronous;
import javax.ejb.Stateless;
@Stateless
public class MethodPool {
@Asynchronous
public Future run(){
System.out.println("start run");
Integer runCondition = 1;
while(runCondition > 0){
try {
// simulate heavy stuff
Thread.sleep(1000);
System.out.println(". ");
} catch (InterruptedException ex) {
Logger.getLogger(MethodPool.class.getName()).log(Level.SEVERE, null, ex);
}
}
return new AsyncResult("finish run");
}
}
*编辑:谢谢@bkail *
我将为下一个绝望的人发布工作代码示例;)
@Resource
private SessionContext sessionContext;
@Asynchronous
public Future run(){
System.out.println("start run");
while(sessionContext.wasCancelCalled() == false){
try {
// simulate heavy stuff
Thread.sleep(1000);
System.out.println(". ");
} catch (InterruptedException ex) {
Logger.getLogger(MethodPool.class.getName()).log(Level.SEVERE, null, ex);
}
}
return new AsyncResult("finish run");
}