クラスが最初の run メソッドを実行した後、オーバーライドされた 2 番目の run メソッドに進まないという問題があります。
プログラムの実行は、メイン メソッドとスレッド プールを持つコントローラー クラスで行われます。
public class RunnableController {
// Main method
public static void main(String[] args) throws InterruptedException {
try {
RunnableController controller = new RunnableController();
controller.initializeDb();
controller.initialiseThreads();
System.out.println("Polling");
} catch (Exception e) {
e.printStackTrace();
}
}
private void initialiseThreads() {
try {
threadExecutorRead = Executors.newFixedThreadPool(10);
PollingSynchronizer read = new PollingSynchronizer(incomingQueue, dbConncetion);
threadExecutorRead.submit(read);
} catch (Exception e){
e.printStackTrace();
}
}
}
新しいデータをフェッチし、シミュレートして更新を行う必要がある私のポーラー クラス:
public class PollingSynchronizer implements Runnable {
public PollingSynchronizer(Collection<KamMessage> incomingQueue,
Connection dbConnection) {
super();
this.incomingQueue = incomingQueue;
this.dbConnection = dbConnection;
}
private int seqId;
public int getSeqId() {
return seqId;
}
public void setSeqId(int seqId) {
this.seqId = seqId;
}
// The method which runs Polling action and record the time at which it is done
public void run() {
int seqId = 0;
while (true) {
List<KamMessage> list = null;
try {
list = fullPoll(seqId);
if (!list.isEmpty()) {
seqId = list.get(0).getSequence();
incomingQueue.addAll(list);
this.outgoingQueue = incomingQueue;
System.out.println("waiting 3 seconds");
System.out.println("new incoming message");
Thread.sleep(3000);//at this wait I should execute run()
//when I debug my execution stops here and throws " Class not found Exception "
// its does not enters the message processor class
MessageProcessor processor = new MessageProcessor() {
//the run method which should fetch the message processor class.
final public void run() {
MessageProcessor(outgoingQueue).generate(outgoingQueue);
}
};
new Thread(processor).start();
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
}
私のメッセージプロセッサクラス:
public abstract class MessageProcessor implements Runnable {
private Connection dbConnection;
Statement st = null;
ResultSet rs = null;
PreparedStatement pstmt = null;
private Collection<KamMessage> outgoingQueue;
public KamMsg804 MessageProcessor(Collection<KamMessage> outgoingQueue,
Connection dbConnection) {
this.outgoingQueue = outgoingQueue;
this.dbConnection = dbConnection;
return (KpiMsg804) fetchedMessages;
}
public Collection<KamMessage> generate(Collection<KamMessage> outgoingQueue) {
while (true) {
try {
while (rs.next()) {
KamMessage filedClass = convertRecordsetToPojo(rs);
outgoingQueue.add(filedClass);
}
for (KamMessage pojoClass : outgoingQueue) {
KamMsg804 updatedValue = createKamMsg804(pojoClass);
System.out.print(" " + pojoClass.getSequence());
System.out.print(" " + pojoClass.getTableName());
System.out.print(" " + pojoClass.getAction());
System.out.print(" " + updatedValue.getKeyInfo1());
System.out.print(" " + updatedValue.getKeyInfo2());
System.out.println(" " + pojoClass.getEntryTime());
}
return outgoingQueue;
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
私の問題は、MessageProcessor クラスで例外が発生し、Polling にループバックする 2 番目の run(9 メソッドにあります。
- ここでマルチスレッドを実装するにはどうすればよいですか。スレッドがポーリングで 3 秒間スリープすると、同時にデータベースが更新されるはずです。
- その後、データをどのようにフィードしてデータベースに更新することができますか。
私のプログラム フロー - 3 つのクラスがあります: 1.Controller 2.PollerSynchro 3.Msgprocessor
POJO 形式に変換され、コレクションに格納されるデータベース レコードがあります。これらの POJO を使用して、私のクラスはマルチプロセッシングと更新を一度に実行しようとします。
コントローラ - スレッド プールを持ち、poll メソッドでポーラー クラスを開始 - 完了
ポーラー - 新しい受信メッセージをポーリングし、受信キューに格納する必要があります - 完了
MsgProcessor - 新しい受信メッセージを探して、送信キューから受信キューに渡す必要があります - これも完了
問題:
今私の問題は
ポーリング スレッドが 3 秒間スリープしている間に、この更新を実装する必要があります。
Poller クラスの 2 番目の void run() メソッドのコードでは、送信キューが渡されず、更新のためにメッセージ プロセッサ クラスに供給されます。私の実行フローは、最初の実行メソッドにループバックするだけで、クラス例外が発生しています。
これらの問題を解決するのを手伝ってください。