ここでメソッドは、増加し続けるシーケンス番号を持つ一意の ID を持つデータベースを読み取ります。Java の初心者なので、この反復ポーリングを実装し、毎回新しい着信メッセージをチェックする方法を知ることができますか?
public void run() {
int seqId = 0;
while(true) {
List<KpiMessage> list = null;
try {
list = fullPoll(seqId);
if (!list.isEmpty()) {
seqId = list.get(0).getSequence();
incomingMessages.addAll(list);
System.out.println("waiting 3 seconds");
System.out.println("new incoming message");
Thread.sleep(3000);
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
//Method which defines polling of the database and also count the number of Queries
public List<KpiMessage> fullPoll(int lastSeq) throws Exception {
Statement st = dbConnection.createStatement();
ResultSet rs = st.executeQuery("select * from msg_new_to_bde where ACTION = 804 and SEQ >" + lastSeq + "order by SEQ DESC");
List<KpiMessage> pojoCol = new ArrayList<KpiMessage>();
while (rs.next()) {
KpiMessage filedClass = convertRecordsetToPojo(rs);
pojoCol.add(filedClass);
}
for (KpiMessage pojoClass : pojoCol) {
System.out.print(" " + pojoClass.getSequence());
System.out.print(" " + pojoClass.getTableName());
System.out.print(" " + pojoClass.getAction());
System.out.print(" " + pojoClass.getKeyInfo1());
System.out.print(" " + pojoClass.getKeyInfo2());
System.out.println(" " + pojoClass.getEntryTime());
}
// return seqId;
return pojoCol;
}
私の目標は、データベースからテーブルをポーリングし、新しい受信メッセージをチェックすることです。これは、一意であり、新しいエントリに対して増加し続けるテーブルのヘッダー フィールド SequenceID から見つけることができます。今私の問題は
1.初めてポーリングした後、すべてのエントリを読み取り、スレッドを 6 秒間スリープ状態にするとします。その間に、新しい着信データを取得して再度ポーリングするにはどうすればよいですか?
2.また、2回目のポーリングを行い、新しいデータを別のクラスに渡すときに、新しいデータを追加する方法。