ここでメソッドは、増加し続けるシーケンス番号を持つ一意の ID を持つデータベースを読み取ります。Java の初心者なので、この反復ポーリングを実装し、毎回新しい着信メッセージをチェックする方法を知ることができますか?
/**
* Method which defines polling of the database and also count the number of Queries
* @return pojo collection
* @throws Exception
*/
public List<KAMessage> fullPoll() throws Exception {
Statement st = dbConnection.createStatement();
ResultSet rs = st.executeQuery("select * from msg_new_to_bde where ACTION = 804 order by SEQ DESC");
List<KAMessage> pojoCol = new ArrayList<KAMessage>();
while (rs.next()) {
KAMessage filedClass = convertRecordsetToPojo(rs);
pojoCol.add(filedClass);
}
return pojoCol;
}
/**
* Converts a provided record-set to a {@link KAMessage}.
*
* The following attributes are copied from record-set to pojo:
*
* <ul>
* <li>SEQ</li>
* <li>TABLENAME</li>
* <li>ENTRYTIME</li>
* <li>STATUS</li>
* </ul>
*
* @param rs
* @return the converted pojo class object
* @throws SQLException
*
*/
private KAMessage convertRecordsetToPojo(ResultSet rs) throws SQLException {
KAMessage msg = new KAMessage();
int sequence = rs.getInt("SEQ");
msg.setSequence(sequence);
int action = rs.getInt("ACTION");
msg.setAction(action);
String tablename = rs.getString("TABLENAME");
msg.setTableName(tablename);
Timestamp entrytime = rs.getTimestamp("ENTRYTIME");
Date entryTime = new Date(entrytime.getTime());
msg.setEntryTime(entryTime);
Timestamp processingtime = rs.getTimestamp("PROCESSINGTIME");
if (processingtime != null) {
Date processingTime = new Date(processingtime.getTime());
msg.setProcessingTime(processingTime);
}
String keyInfo1 = rs.getString("KEYINFO1");
msg.setKeyInfo1(keyInfo1);
String keyInfo2 = rs.getString("KEYINFO2");
msg.setKeyInfo2(keyInfo2);
return msg;
}
}
これは私が試したことです:
while(true){
try {
incomingMessages.addAll(fullPoll());
System.out.println("waiting 6 seconds");
//perform this operation in a loop
Thread.sleep(6000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
準備されたステートメントを使用してパラメーターをこのコードのクエリに渡す方法は、ここで立ち往生しています..
public List<KAMessage> fullPoll() throws Exception {
PreparedStatement oldSeq = null;
PreparedStatement newSeq = null;
Statement st = dbConnection.createStatement();
System.out.println("Polling");
String query = "select * from msg_new_to_bde where ACTION = 804 and SEQ between oldSeq and newSeq order by SEQ DESC";// insert in table
pstmt = conn.prepareStatement(query);
}