2

2 台の Linux マシンがあります。あるマシンでは、実行可能ファイルを起動するスレッドを使用しており、別の内部スレッドが実行可能ファイルからデータを読み取り、実行可能ファイルの値をデータベースに入力しています。データを永続化するために myBatis を使用しています。その後、プロセスと内部スレッドが稼働しているかどうかを継続的にチェックします。他のマシンでは、データベースがリモートで接続されており、毎晩継続的に展開されています。これにより、データベースが削除され、再作成されます。したがって、このビルド中にデータベース テーブルを使用できないため、例外が発生します。

org.apache.ibatis.exceptions.PersistenceException 
### Error updating database.  Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:
Table 'updates_table' doesn't exist

投げられます。次に、プロセスと内部スレッドを継続的にチェックしているスレッドが強制終了され、チェックが停止します。

スレッドが強制終了されないように処理する方法を教えてください。データベースが利用可能になり、実行されたら、テーブルの再作成を試みる必要があります。データベースが利用できない場合は、データベースが利用可能になるまで常に試行し続ける必要があります。

ありがとうございました。

4

3 に答える 3

1

Consider switching to a system where you submit jobs to an Executor from the thread pulling stuff off of the process:

public class MyThread extends Thread {
    private final InputStream processStream;
    private final Executor executor = Executors.newSingleThreadExecutor();

    public MyThread(InputStream processStream) {
        this.processStream = processStream;
    }

    @Override
    public void run() {
        while ([processStream has stuff]) {
            final Object obj = // Get your object from the stream
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    // Do database stuff with obj
                }
            });
        }
    }

    private static Object getSomethingFromStream(InputStream stream) {
        // return something off the stream
    }
}

If an exception is thrown by your Runnable, it will be logged, but it won't be stopped, and it will just continue to the next job in the queue. Also note that this is using a single-threaded executor, so everything submitted will be executed one at a time, and in the order they're submitted. If you want concurrent execution, use Executors.newFixedThreadPool(int) or Executors.newCachedThreadPool(). Note that this answers how to keep your thread alive. If you want to resubmit a runnable for re-execution if the job fails, change its run method to:

@Override
public void run() {
    try {
        // Do database stuff with obj
    } catch (PeristenceException ex) {
        // Try again
        executor.execute(this);
    }
}

You can add logic to this to tailor when it will try again on an exception.

于 2012-09-07T20:27:48.600 に答える
0

高レベルでは、Observableパターン(JDKに組み込まれている)を使用して、メンテナンス中にコードが通知されるようにすることができます。新しいスレッドを生成することで、接続を再確立できます。

于 2012-09-07T20:24:46.933 に答える
0

この構成を使用します:

try{ // code to update db
} catch(MySQLSyntaxErrorException exception){ // handle db exception }

dbで動作するように実行されるスレッド内。

于 2012-09-07T20:38:18.217 に答える