• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Java Web Refresh Data

Shayughul

n00b
Joined
Jun 28, 2004
Messages
60
So I have the program built already. It basically is just a simple class that grabs the source from a page. Scrapes out the data it needs and displays that data in a JFrame.

The part that has stumped me is that this webpage updates constantly...maybe as many times as 5 or 6 times every minute.

Now if connections to the server aren't a worry. Is there anyway for me to simply just repeat my code indefinitely every 30 seconds or whatever interval until I close program. Preferably without any user interaction.

This is the first attempt of Java programming that I have ever done so am pretty happy with out far I have gotten so far. TIA for any help.
 
You'll need to familiarize yourself with threaded programming. For swing its important anyway.

Then you could have something like this running in a background thread:

Code:
 while(notToldToStop){
     scrapeData
     if dataChanged
         SwingUtilities.invokeLater(new Runnable() {
public void run() {
   updateGuiItems() // from the swing thread, now that we're on it
}
}
     Thread.sleep(30)       
}


Now updating the gui may be complex, depending on how the user interacts with your system. You don't want to be changing things out from underneath them if they are in the middle of editing, or controlling something.

You also need to be polite to the server and consider how often you really need to poll it.
 
The timer class does the this sort of thing for you as well.

It'll invoke an ActionEvent on an interval for you. The events are run on the event-dispatching thread, similar to invokeLater.
 
Lunknumb, one wouldn't want to use javax.swing.Timer in this case since the callback is run on the EDT. if the web page hangs for some reason then the GUI will be locked up. either what ambientZ suggests or preferably java.util.Timer would probably be the better option. just be sure to do the SwingUtilities.invokeLater(...), that's kind of important.
 
Even the java.utilTimer class docs say:
Corresponding to each Timer object is a single background thread that is used to execute all of the timer's tasks, sequentially. Timer tasks should complete quickly. If a timer task takes excessive time to complete, it "hogs" the timer's task execution thread. This can, in turn, delay the execution of subsequent tasks, which may "bunch up" and execute in rapid succession when (and if) the offending task finally completes.

A web server timeout is likely to take quite some time, so I don't know how well Timer would handle it.

I've never used java.util.Timer though, too newfangled for me ;)
 
Even the java.utilTimer class docs say:


A web server timeout is likely to take quite some time, so I don't know how well Timer would handle it.

I've never used java.util.Timer though, too newfangled for me ;)

Since:
1.3

In the API docs means since JDK 1.3 which was eons ago. :p

If your worried about tasks bunching up and executing rapid fire the timer supports both fixed-delay execution and fixed-rate execution. Using fixed-delay the next execution of the timer event is tied to the completion of the previous one plus a delay, whereas in fixed rate an execution is queued periodically after a fixed amount of elapsed wall time.
 
Back
Top