Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: How to do Polling and also check for new incoming data in a Database for a regular interval

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.


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


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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.

  3. The Following User Says Thank You to copeg For This Useful Post:

    babu788 (January 9th, 2013)

  4. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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 View Post
    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.

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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.

Similar Threads

  1. Java Weighted interval scheduling
    By ryclegman in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 23rd, 2012, 03:06 PM
  2. how to searching data from database using servlet??
    By mr.nash in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: December 21st, 2011, 03:09 PM
  3. How to Create a server socket to listen for incoming connections?
    By JavaPF in forum Java Networking Tutorials
    Replies: 3
    Last Post: October 28th, 2011, 09:02 AM
  4. Check for duplicates before inserting into database
    By igor0203 in forum JDBC & Databases
    Replies: 1
    Last Post: December 2nd, 2010, 09:04 AM
  5. How to Create a server socket to listen for incoming connections?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: May 5th, 2009, 08:02 AM

Tags for this Thread