在Quarkus中异步持久化数据

huangapple go评论81阅读模式
英文:

Persist data asynchronously in Quarkus

问题

我正在尝试使用Quarkus和Hibernate-Panache异步持久化一些数据:

Executors.newFixedThreadPool(1).execute(new Runnable() {
    @Override
    @Transactional // 我需要一个事务来持久化数据
    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

提前感谢!

英文:

I am trying to persist some data asynchronously with Quarkus and 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);
        }

Unfortunately, I get:

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)

A reproducer project is here: https://github.com/MarcusBiel/quarkus-reproducer

Thanks in advance!

答案1

得分: 1

在 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);
英文:

With help from Martin Kouba @martunek (et all) in the Quarkus Zulip chat, I made it work like so:

 @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&lt;List&lt;String&gt;&gt; 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;
    }
}

And in "MyService" we execute this task like so:

ExecutorService executor = Executors.newFixedThreadPool(1);
MyTask myTask = Arc.container().instance(MyTask.class).get();
myTask.setCsvData(csvData);
myTask.setMyId(myId);
executor.execute(myTask);

huangapple
  • 本文由 发表于 2020年9月15日 17:00:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63898482.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定