夸克斯读取 `ResultSet` 并使用多线程进行处理

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

Quarkus read `ResultSet` and process with multi-threads

问题

我正在尝试并行读取 JDBC ResultSet,并在 Quarkus 服务中对每条记录进行一些处理。ResultSet 中可能有大量记录,因此我不想预先读取所有数据。我更愿意将结果流式传输到处理器。

我已经封装了 ResultSet 对象,以便我可以以线程安全的方式从中读取。然而,我在尝试以多个线程读取结果时遇到了困难。我尝试过使用 ExecutorService,但是数据库上下文丢失了,并且ResultSet 在线程中被关闭了。

我考虑使用 SmallRye 的 Multi 对象来进行处理,但是我很难找到一种将记录添加到项目列表的方法,而且我不确定这样是否会创建多个线程。

在 Quarkus 中,是否有首选的多线程库呢?

英文:

I'm trying to read a JDBC ResultSet in parallel and do some processing on each record in a Quarkus service. There could be lots of records in the ResultSet so I don't want to read all the data upfront. I'd rather stream the results to a processor.

I have wrapped the ResultSet object so I can read from it in a thread safe manner. However I am struggling to find a way to read the results in multiple threads. I've tried an ExecutorService but the DB context gets lost and the ResultSet is closed in the thread.

I considered using a SmallRye Multi object to do the processing, but I struggled to find a way to add the records to the item list and I'm not sure that will create multiple threads anyway.

Is there a preferred multi-threading library for Quarkus?

答案1

得分: 2

你可以使用生产者消费者模式:一个线程打开数据库连接,从结果集中读取数据,并将其放入具有一定大小的阻塞队列中,然后其他几个线程从该阻塞队列获取数据并进行处理。

英文:

You can use producer-consumer pattern: one thread opens database connection, reads data from ResultSet and places it into Blocking Queue with some limited size and several other threads get data from that blocking queue and process it

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

发表评论

匿名网友

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

确定