問題の調査に何時間も費やしましたが、解決策を見つけました。
@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");
}
}
*編集: THX @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");
}