我在 WLS 中实现了一个 WorkManager,并使用调度调用了一个异步任务。当我在异步任务中收到请求时,我丢失了我的应用程序上下文的所有 @Autowired 引用。
有没有办法在异步任务(类 ExecuteProcessServiceImpl)的方法“startTask”中使用我的应用程序上下文的变量?
调用异步任务的类
InitialContext ctx = new InitialContext();
String wmName = "java:comp/env/wm/wm0";
WorkManager workManager = (WorkManager) ctx.lookup(wmName);
Work work = new WorkInstance();
workManager.schedule(work, new MyWorkListener());
接收请求并启动任务的类
public class WorkInstance implements Work {
private ExecuteProcessServiceImpl task = new ExecuteProcessServiceImpl();
@Override
public void run() {
try {
new Callable<ExecuteProcessServiceImpl>() {
@Override
public ExecuteProcessServiceImpl call() {
task.startTask();
return task;
}
}.call();
} catch (Exception e) {
log.error(e.getMessage());
}
}
异步任务
public class ExecuteProcessServiceImpl implements ExecuteProcessService {
@Autowired
private MyAutowiredObj obj;
public void startTask() {
// The application context is lost here
// The value of "obj" is null
}