Multithreading Advice

Hi,

I want to develop a notification application which docks in the system tray, it should poll my database for changes to various tables and popup a display.

A well known exapleis the GMail Notifier.

I have been reading up on Multithreading and this seems to be how to achieve my goal.

Is there a better/more efficient way to poll a database for changes?

Any good souces of information/examples of multithreading to a database?

Cheers Charl

[489 byte] By [charla] at [2007-11-26 22:02:35]
# 1

If possible, it's more efficient for the database operations to notify "listeners" of any changes instead of polling the db at regular intervals in a background thread. In that case you wouldn't need a new thread at all.

If that's not possible and you have to run a background thread to constantly poll the db for changes, then I think you may find SwingWorker helpful.

http://java.sun.com/docs/books/tutorial/uiswing/concurrency/worker.html

It's designed to help you calculate a value in a background thread (db changed?) and then update Swing components in the EDT based on that value.

BinaryDigita at 2007-7-10 10:43:10 > top of Java-index,Java Essentials,Java Programming...
# 2

thanks, luckily i haven't spent to much time on the multi-threading today.

why is swingworker better to use than multi-threading though?

my app will run in the backgroud, and poll every 15 - 20 minutes for changes to the users recordset based on userid.

unfortunately i can't use the DB to update the app, it's not mine to play with.

i'm reading up some more about swingworker. going to give it a try.

charla at 2007-7-10 10:43:10 > top of Java-index,Java Essentials,Java Programming...
# 3

> why is swingworker better to use than multi-threading

> though?

Sorry, that's not what I meant. SwingWorker is good if you do need the background thread, not instead of it. It makes it easier to properly update Swing components in the EDT based on what a non-EDT thread did. So you'll override SwingWorker's doInBackground() method to query the database and find the changes, and override its done() method to pop up the display.

> my app will run in the backgroud, and poll every 15 - 20 minutes for changes to the users recordset based on userid.

That's not so bad. I was assuming it would poll every few seconds or something.

BinaryDigita at 2007-7-10 10:43:10 > top of Java-index,Java Essentials,Java Programming...
# 4

Hi

I need some guidance as I am not sure how to approach this next part of my application. I want my tray app to query a db and display a message in the system tray if there's new records.

I have been reading on swingworker but I can't figure how to use it.

The more I read the more I think that swingworker is more suited for GUI tasks which are driven by a user event.

Please put me in the right direction as to what I should try.

I also can't find any info on how to create a swingworker object which sets up a db connection and runs on a timer. any example code here whould be great

My application doesn't do anything except check for updates either user driven or on a set timer 20 mins.

How can I get my tray app to run a search every 20mins and then ... do something..

thanks

charl

charla at 2007-7-10 10:43:10 > top of Java-index,Java Essentials,Java Programming...
# 5
> If possible, it's more efficient for the database> operations to notify "listeners" of any changes> instead of polling the db at regular intervals in a> background thread. And that statement is based on what?
jschella at 2007-7-10 10:43:10 > top of Java-index,Java Essentials,Java Programming...
# 6

>

> My application doesn't do anything except check for

> updates either user driven or on a set timer 20

> mins.

>

> How can I get my tray app to run a search every

> 20mins and then ... do something..

There are three parts.

1. Get a system tray app and have it display a message (based on input.)

2. Determine that a change has occurred.

3. Create a timer.

For 3 Java has a timer class.

As for 2 that depends on your data.

For 1 that is entirely a GUI issue for which there are several specific forums.

jschella at 2007-7-10 10:43:10 > top of Java-index,Java Essentials,Java Programming...
# 7

> > If possible, it's more efficient for the database

> > operations to notify "listeners" of any changes

> > instead of polling the db at regular intervals in

> a

> > background thread.

>

> And that statement is based on what?

Based on my assumption that the database will be polled more often than it will be updated. It depends on the environment, and I shouldn't have made that assumption as I already stated.

> I have been reading on swingworker but I can't figure

> how to use it.

> The more I read the more I think that swingworker is

> more suited for GUI tasks which are driven by a user

> event.

> Please put me in the right direction as to what I

> should try.

> I also can't find any info on how to create a

> swingworker object which sets up a db connection and

> runs on a timer. any example code here whould be

> great

You're right, SwingWorker is suited for GUI tasks. Your popup display will be something like a JWindow right? Which is a Swing component. And SwingWorker isn't driven only by a user event, you can execute one at any time.

Here's the simplest example of using SwingWorker that I could come up with. Create a JButton called btnTest and call doBtnTest() in its action:

private void doBtnTest() {

btnTest.setText("SwingWorker is running...");

new SwingWorker() {

public Object doInBackground() throws Exception {

// This is called in a dedicated thread, not the EDT

try {

// Connect to the the DB and query it here

Thread.sleep(2000);

// Return the result of the query

return "It's finished!";

} catch (Exception e) {

e.printStackTrace();

return null;

}

}

public void done() {

// This is called in the EDT to safely update Swing components

try {

// This gets the value that you returned from doInBackground

Object value = get();

if (value != null) {

// Show your popup here

btnTest.setText(value.toString());

}

} catch (Exception ex) {

ex.printStackTrace();

}

}

}.execute();

}

The idea is, whatever code you put in the doInBackground method will run in a dedicated thread (connect to the DB, find out if it changed). When that thread is finished, whatever code you put in the done method will run in the EDT (show the popup etc.) So it helps you perform a lengthy background task and then safely update your components based on what the thread did. So for each 20 minute (or whatever) poll interval you'll create and execute a new SwingWorker.

BinaryDigita at 2007-7-10 10:43:10 > top of Java-index,Java Essentials,Java Programming...
# 8
Hey thats much easier to understand than all the reading i've done on swingworker combined.i'll try make that work with my database. looks like it will work. thanks
charla at 2007-7-10 10:43:10 > top of Java-index,Java Essentials,Java Programming...
# 9

> > > If possible, it's more efficient for the database

> > > operations to notify "listeners" of any changes

> > > instead of polling the db at regular interval in a

> > > background thread.

> >

> > And that statement is based on what?

>

> Based on my assumption that the database will be

> polled more often than it will be updated. It

> depends on the environment, and I shouldn't have made

> that assumption as I already stated.

>

So far as I know a push notification would require triggers.

So what happens if the database in polled 1000 times a day versus a single update.

But that single update adds a million rows?

jschella at 2007-7-10 10:43:10 > top of Java-index,Java Essentials,Java Programming...