How to do Polling and also check for new incoming data in a Database for a regular interval
Here the method reads the database which has an unique ID with the sequence number which keeps on increasing, since am a beginner in java,can I know how to implement this repetitive polling and check for new incoming message each time.
Code :
/**
* 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;
}
}
Re: How to do Polling and also check for new incoming data in a Database for a regular interval
Thread moved from 'What's wrong with my code'
I am not clear what you are asking. If you wish to check for new messages each time, then there are several way to go about it. One could be to record the time of each 'message', for instance each message would have a column in the database for insert time, when you poll - say every minute - you check for messages that have an insert time greater than the current time minus a minute.
Re: How to do Polling and also check for new incoming data in a Database for a regular interval
Hi thanks for de reply..am new here and beginner in Java..
but there might be many entries at the same time
My goal is to Poll the table from the database and also check for new incoming message, which I can find from the Header field SequenceID in table which is unique and keeps on increasing for newentries.
Now my problem is
1.Lets say after I poll the first time, it reads all the entries and makes the thread to sleep for 6 seconds, by the mean time how can I get the new incoming data and Poll it again ?
2.Also how to add the new data ,when it does Polling for the second time and pass the new data to another class.
--- Update ---
Quote:
Originally Posted by
copeg
Thread moved from 'What's wrong with my code'
I am not clear what you are asking. If you wish to check for new messages each time, then there are several way to go about it. One could be to record the time of each 'message', for instance each message would have a column in the database for insert time, when you poll - say every minute - you check for messages that have an insert time greater than the current time minus a minute.
Hi thanks for the reply..am new here and beginner in Java..
but there might be many entries at the same time
My goal is to Poll the table from the database and also check for new incoming message, which I can find from the Header field SequenceID in table which is unique and keeps on increasing for new entries.
Now my problem is
1.Lets say after I poll the first time, it reads all the entries and makes the thread to sleep for 6 seconds, by the mean time how can I get the new incoming data and Poll it again ?
2.Also how to add the new data ,when it does Polling for the second time and pass the new data to another class.
Re: How to do Polling and also check for new incoming data in a Database for a regular interval
Like I said, there are many different ways to do this. One is posted above. If you wish to rely on the auto-increment key, then you could create a method which retrieves all messages with values greater than a given value (this given value being a parameter to the method). This allows you to first do a query to retrieve all, then when you poll for any new messages you pass the highest key value to the poll method and it returns any new messages, which you can deal with accordingly.