テーブルからデータを読み取り、各行で何らかの作業を行い、行を更新済みとしてマークする必要があります。更新された行を再度読み取りたくありません。これが、ExecutorService で行うことを計画していることです。これは正しいです ?
ありがとう。
public class ScheduledDBPoll
{
public static void main(String args[])
{
ExecutorService service = Executors.newFixedThreadPool(10);
/* Connect to database. */
while (true)
{
/* Issue a select statment for un-updated rows. */
/* Get the primary key. */
service.submit(new Task(primaryKey)); /* Pass the primary key*/
try
{
Thread.sleep(3000); /* Sleep for 3 seconds. */
}
catch (InterruptedException ex)
{
Logger.getLogger(ScheduledDBPoll.class.getName()).log(Level.SEVERE, null, ex);
}
}
/* Close the connection here. */
}
}
final class Task implements Runnable
{
private int taskId;
public Task(int primayKey)
{
this.taskId = primayKey;
}
@Override
public void run()
{
try
{
/* Connect to database. */
/* Select the passed primary key row. */
/* Do some work, mark the row as updated. */
/* Close the connection here. */
}
catch (InterruptedException ex)
{
Logger.getLogger(Task.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Firebird データベースを使用しています。Firebird は接続レベルでのみスレッドセーフです。そのため、同じ接続に対して異なるスレッドで 2 つのクエリが実行されるのを避けようとしています。また、上記のコードは Windows サービスとして実行されるため、新しく挿入されたレコードを常に探していることを忘れていました。