我想在java中为Java EE应用程序开发实现异步测试启动程序,但我的ManagedTaskListener实现不会“监听”事件。
我有这个测试实现:
package com.co.seti.sima.commons.executor;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import javax.enterprise.concurrent.ManagedExecutorService;
import javax.enterprise.concurrent.ManagedTaskListener;
public class TestLauncher implements Callable<ResultadoPruebaDTO>, ManagedTaskListener {
/* (non-Javadoc)
* @see javax.enterprise.concurrent.ManagedTaskListener#taskAborted(java.util.concurrent.Future, javax.enterprise.concurrent.ManagedExecutorService, java.lang.Object, java.lang.Throwable)
*/
public void taskAborted(Future<?> arg0, ManagedExecutorService arg1, Object arg2, Throwable arg3) {
// TODO Auto-generated method stub
System.out.println("Se obortó la ejecucion");
}
/* (non-Javadoc)
* @see javax.enterprise.concurrent.ManagedTaskListener#taskDone(java.util.concurrent.Future, javax.enterprise.concurrent.ManagedExecutorService, java.lang.Object, java.lang.Throwable)
*/
public void taskDone(Future<?> arg0, ManagedExecutorService arg1, Object arg2, Throwable arg3) {
// TODO Auto-generated method stub
System.out.println("Tark done");
}
/* (non-Javadoc)
* @see javax.enterprise.concurrent.ManagedTaskListener#taskStarting(java.util.concurrent.Future, javax.enterprise.concurrent.ManagedExecutorService, java.lang.Object)
*/
public void taskStarting(Future<?> arg0, ManagedExecutorService arg1, Object arg2) {
// TODO Auto-generated method stub
System.out.println("Starting task");
}
/* (non-Javadoc)
* @see javax.enterprise.concurrent.ManagedTaskListener#taskSubmitted(java.util.concurrent.Future, javax.enterprise.concurrent.ManagedExecutorService, java.lang.Object)
*/
public void taskSubmitted(Future<?> arg0, ManagedExecutorService arg1, Object arg2) {
// TODO Auto-generated method stub
System.out.println("Task submit");
}
/* (non-Javadoc)
* @see java.util.concurrent.Callable#call()
*/
public String call() throws Exception {
return "Task Executed";
}
}
我在EJB Singleton上使用它
@Resource
private ManagedExecutorService managedExecutorService;
...
managedExecutorService.submit(new TestLauncher());
...
执行开始并完成,但听众不记录事件。
我不明白,请帮助。
答案 0 :(得分:-1)
您必须使用ManagedTaskListener和ManagedTask实现一个类。这个clase可以是Callable或Runnable。
private static class DukeMessage implements ManagedTask, ManagedTaskListener, Callable<Long> {
private final ChatMessage cmsg;
private final Session session;
public DukeMessage(ChatMessage cmsg, Session session) {
this.cmsg = cmsg;
this.session = session;
}
public synchronized void sendAll(Session session, Object msg) {
try {
for (Session s : session.getOpenSessions()) {
if (s.isOpen()) {
s.getBasicRemote().sendObject(msg);
logger.log(Level.INFO, "Sent: {0}", msg.toString());
}
}
} catch (IOException | EncodeException e) {
logger.log(Level.INFO, e.toString());
}
}
@Override
public ManagedTaskListener getManagedTaskListener() {
return this;
}
@Override
public Map<String, String> getExecutionProperties() {
return new HashMap<>();
}
@Override
public Long call() throws Exception {
try {
String resp = "Respuesta de Duke!";
//botbean.respond(cmsg.getMessage());
sendAll(session, new ChatMessage("Duke", cmsg.getName(), resp));
} catch (Exception ex) {
System.out.println(ex);
}
return 0L;
}
//@Override
//public void run() {
//try {
//String resp = "Respuesta de Duke!";
//botbean.respond(cmsg.getMessage());
//sendAll(session, new ChatMessage("Duke", cmsg.getName(), //resp));
//} catch (Exception ex) {
//System.out.println(ex);
//}
//}
@Override
public void taskSubmitted(Future<?> future, ManagedExecutorService executor, Object task) {
System.out.println("taskSubmitted");
}
@Override
public void taskAborted(Future<?> future, ManagedExecutorService executor, Object task, Throwable exception) {
System.out.println("taskAborted");
}
@Override
public void taskDone(Future<?> future, ManagedExecutorService executor, Object task, Throwable exception) {
System.out.println("taskDone");
}
@Override
public void taskStarting(Future<?> future, ManagedExecutorService executor, Object task) {
System.out.println("taskStarting");
}
}
电话会是......
mes.submit(new DukeMessage(cmsg, session));
或
mes.execute(new DukeMessage(cmsg, session));