4

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.

/**
 * 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;
    }
      }

This is what I have tried :

       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();                               
           }

How to pass the parameters using prepared statements into the query for this code am stuck up here..

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);
 }
Babu
  • 299
  • 1
  • 3
  • 12

2 Answers2

2

There could be various way to implement repetitive polling, the simplet way i can think of is to put the polling method inside a while, with thread.sleep for example:

    while(true){
        fullPoll();
        Thread.sleep(10000);
     }

do not forget to use try catch since you have an exception thrown. Alternatively you could use something like timer tasks or frameworks as quartz.

In order to check if there is new data in the DB i can think of the following ways on the fly(there can be more optimized ones):

You could keep a count of the rows in order to understand if other rows have been added so you can rerun the query.

This does not cover the case when rows are delete and reinserted, to cover also this you could try to store somewhere the max id used the last time you queried the db and check each time if the new max id is different form the last one you memorized, if yes, database has changed.

For example, if you keep the old index in variable oldSeq, and the new in variable newSeq and you want to get only the newly added messages, you could use a query as :

select * from msg_new_to_bde where ACTION = 804 and SEQ between oldSeq and newSeq order by SEQ DESC

you should check if between for your DB includes also the test values (oldSeq and newSeq)

B11
  • 223
  • 3
  • 12
  • 1
    thank you, can I know how to find new incoming messages from the code which i have added above – Babu Jan 08 '13 at 11:04
  • thanks can I know how to get this variable oldSeq and newSeq inside the query... please provide me with an approach – Babu Jan 08 '13 at 11:09
  • I meant how to pass this variable inside the query and extract it each time so that i can process the new incoming message in an another class.. – Babu Jan 08 '13 at 11:11
  • 2
    well you could use a query as select max(seq) from msg_new_to_bde, this gives you the maximum value each time you query the db, keep this in the newSeq variable, compare it to the oldSeq variable, id if newSeq>oldSeq proceed with the query, then oldSeq=newSeq, if not do not procedd with the query – B11 Jan 08 '13 at 11:13
  • to learn how to pass the parameters, check prepared statements – B11 Jan 08 '13 at 11:14
  • I also tried using prepared statements but its not working for me – Babu Jan 08 '13 at 11:35
  • 1
    check this for prepared statements http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html, in order to understand their usage, before passing the parameters as described in the link above, you have to get this parameters form the db as desribed in my answer, so the general flow would be, get newSeq from db, compare to oldSeq(te firs time oldSeq wil be 0), if they are different execute the query passing oldSeq and newSeq, if not do not execute query – B11 Jan 08 '13 at 11:53
  • also consider voting my answer or accepting it if it helped you – B11 Jan 08 '13 at 11:57
  • I could not proceed further, I tried getnewSeq but as you said if there are no incoming messages it stops polling.... It would be helpful if i get the piece of code for this.... – Babu Jan 08 '13 at 13:40
  • try showing the code u have used, than lets see if i or anyone else can help – B11 Jan 08 '13 at 14:05
1

you can call your method fullPoll() inside a while loop and use thread.sleep(<number of millis>) to wait between invocations.

another solution would be to use a full blown scheduler framework such as quartz.

hope that answers your question.

fthiella
  • 48,073
  • 15
  • 90
  • 106
frederikdebacker
  • 593
  • 5
  • 14
  • 1
    @frederikdebaker I prefer using while loop and thread.sleep so that I learn to program,say if i use while loop and use thread.sleep() for 6 seconds ,how can i suck only the new incoming data for the next poll, here I have the sequence number unique which increases for each new incoming message ... – Babu Jan 08 '13 at 10:50
  • 1
    In that case you need to keep track of the last sequence number of your previous poll. You can use this sequence number in you db query to only retrieve the new ones. – frederikdebacker Jan 08 '13 at 11:02
  • 1
    please refer to the code which i have added above, how to find new income message for this polling – Babu Jan 08 '13 at 11:03