1

Could someone suggest what’s the best approach to go about this:

What I like to do is draw a continuous poll (or query) of a database table. And anytime a new update has occurred, I want to push this to the client in a Java application. The idea is users can suggest what data they want to subscribe to and define an event-handler to handle any incoming data.

I therefore need to have the polling being done in the background. But I seem to having trouble with doing this correctly. I’m looking at using a simple event bus for local JVM pub/sub, but I can’t seem to get the asynchronous continuous polling going.

7
  • 1
    What user interface? Swing or web? Commented Jul 24, 2011 at 15:44
  • Are you using a Timer? Commented Jul 24, 2011 at 15:47
  • @Jonas At the moment it is just a Java library. Where I want users to define an event-handler to handle events they want to subscribe to. On the publishing side, new entries in the table act as events coming in. Commented Jul 24, 2011 at 15:50
  • @Larry see for example this. Commented Jul 24, 2011 at 15:54
  • @toto What’s the differences between this and what jiggy suggested below (i.e using a ScheduledThreadPollExecutor)? Commented Jul 24, 2011 at 16:00

2 Answers 2

2

Your solution has a lot of disadvantages.
A better solution is to create business logic that is responsible for saving data in the database and for notifications. You can for example create an application that is subscribed to JMS topic. Another component receives calls to process and save data. It stores data and then sends notification of this action. All components subscribed to the topic receive this notification and react accordingly.

Sign up to request clarification or add additional context in comments.

3 Comments

That'a assuming his app is also responsible for populating the data.
Thanks, but JMS seems too complicated to implement. Is there a more simple solution? That is why I was thinking of a simple ESB like code.google.com/p/simpleeventbus, but I’m stuck trying to get a continuos poll/read of the DB?
@jiggy That is right, populating the Data happens via a different app. I am aiming for this to be a client-side application connected to the DB to get events, via new entries in the DB table.
1

Have a look at java.util.concurrent. You can create a scheduler to run at a given interval utilizing a thread pool like with a ScheduledThreadPoolExecutor.

2 Comments

thanks, do you happen to know of any good examples/sample code on the web for this?
@Larry, just look a this javadoc for a good example of a scheduled job. Instead of beeping, you can read your DB. Note that the Runnable class is defined inline for terseness, but it can be defined in it's own .java file and just instantiated here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.