我正在尝试使用Quarkus和Hibernate-Panache异步保留一些数据:
Executors.newFixedThreadPool(1).execute(new Runnable() {
@Override
@Transactional // I need a transaction to persist the data
public void run() {
List<List<String>> myData = myCsvParser.parse(data);
myRepository.importData(myData, myID);
myBusinessService.doBusinessLogic(myID);
}
不幸的是,我得到了
Exception in thread "pool-5-thread-1" javax.enterprise.context.ContextNotActiveException: interface javax.enterprise.context.RequestScoped
at io.quarkus.hibernate.orm.runtime.RequestScopedEntityManagerHolder_ClientProxy.arc$delegate(RequestScopedEntityManagerHolder_ClientProxy.zig:68)
at io.quarkus.hibernate.orm.runtime.RequestScopedEntityManagerHolder_ClientProxy.getOrCreateEntityManager(RequestScopedEntityManagerHolder_ClientProxy.zig:220)
at io.quarkus.hibernate.orm.runtime.entitymanager.TransactionScopedEntityManager.getEntityManager(TransactionScopedEntityManager.java:77)
at io.quarkus.hibernate.orm.runtime.entitymanager.TransactionScopedEntityManager.contains(TransactionScopedEntityManager.java:285)
一个复制器项目在这里:https://github.com/MarcusBiel/quarkus-reproducer
谢谢!
答案 0 :(得分:2)
在Quarkus Zulip聊天室中,在Martin Kouba @martunek(全部)的帮助下,我做到了:
@Dependent
public class MyTask implements Runnable {
@Inject
private MyParser myParser;
@Inject
private MyBusinessService myBusinessService;
@Inject
private MyRepository myRepository;
private String csvData;
private String myId;
@Override
@Transactional
@ActivateRequestContext
public void run() {
List<List<String>> myData = csvParser.parse(csvData);
myRepository.importData(myData, myId);
myBusinessService.myBusinessMethod(myId);
}
public void setCsvData(String csvData) {
this.csvData = csvData;
}
public void setMyId(String myId) {
this.myId = myId;
}
}
在“ MyService”中,我们像这样执行此任务:
ExecutorService executor = Executors.newFixedThreadPool(1);
MyTask myTask = Arc.container().instance(MyTask.class).get();
myTask.setCsvData(csvData);
myTask.setMyId(myId);
executor.execute(myTask);