在我的Web应用程序中,我有一个后台服务.此服务使用Generator类,该类包含Engine类,并ExecutorService配置为使用多个线程并接受GeneratorTasks.
@Component
public class Generator {
@Autowired
private Engine heavyEngine;
private ExecutorService exec = Executors.newFixedThreadPool(3);
//I actually pass the singleton instance Generator class into the task.
public void submitTask(TaskModel model, TaskCallback callback) {
this.exec.submit(new GeneratorTask(model, this, callback));
}
}
@Component
public class Engine {
public Engine() {
//time-consuming initialization code here
}
}
public class GeneratorTask implements Callable<String> {
public GeneratorTask(TaskModel m, Generator g, ReceiptCallback c) {
this.m = m;
this.generator = g;
this.c = c;
}
public …Run Code Online (Sandbox Code Playgroud) 也许,我做错了什么,但我找不到以下情况的好方法。
我想对一个在下面使用Spring Batch来执行作业的服务进行单元测试。作业通过AsyncTaskExecutor在单独的线程中预先配置来执行。在我的单元测试中,我想:
显然,以上所有内容都应该在一个事务中执行,但不幸的是,事务不会传播到新线程(我理解这背后的基本原理)。
我想到的想法:
Isolation.READ_UNCOMMITTED在作业配置中使用。但这需要两种不同的配置用于测试和生产。