0

I have an app that connects reads file on remote server. File dynamically updates that's why I use Timer class to reread this file periodically. Workflow is the following:

  • Open window where text will be displayed.

  • Start reading file (reread once per 15 sec using Timer)

  • In 15 seconds window is filled with data or I receive exceptions in log. Exceptions are suppressed and I continue trying to read data.

Exceptions are my problem, because user doesn't know what is happening now with an app. There are at least two Exceptions I ran at: - If file is absent, I receive FileNotFoundException. - If server is on maintenance I receive other Exception (I catch it, so its name doesn't matter).

Here is how above looks like in code:

public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
            final RemoteReader reader = new RemoteReader();
            Timer timer = new Timer(15000, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    try {
                        reader.getMainPanel().readData();
                    } catch (IOException e) {
            //Here is a counter that increases after each exception throw
            if(counter >5) {
                JOptionPane.showOptionDialog(chat,
                    e.getMessage(),
                    e.getClass().getName(),
                    JOptionPane.OK_CANCEL_OPTION,
                    JOptionPane.INFORMATION_MESSAGE,
                    null,
                    new String[]{"Retry", "Cancel"}, //on Retry - make another 5 tries to read file, on cancel - close the window
                    null);
                counter = 0;
            }
                        e.printStackTrace();
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }
            });
            timer.start();
            }
        });
    }

    public String readData() throws IOException {
    // read and process data before returning it
    // but for test purpose:
        //BufferedReader br = new BufferedReader(new InputStreamReader(this.url.openStream()));
    throw new IOException("cannot read file");
     }

What I want to do is to add JProgressBar. On openning the main window progress bar appears and then data is read. If IOException throws 5 times in a row, show option dialog. Otherwise hide progress bar and show data. If remote file becomes unavailable, show option dialog. And pressing the retry button, show progress bar... then workflow starts from the very beginning.

Some code examples would help me, but I don't expect solution for the whole issue - advice, how it should be done in right way from design point of view will be enough. Samples of Oracle a little bit vague for me.

Dragon
  • 2,431
  • 11
  • 42
  • 68

1 Answers1

1

Even if WatchService, seen here, is not available, I'd still use SwingWorker, seen here and here.

  • You have considerable latitude in pacing the doInBackground() thread, e.g. Thread.sleep(15 * 1000).

  • You can setProgress() in the background and listen for any PropertyChangeEvent in the GUI.

  • You can update any GUI components in process(), which runs on the EDT.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I still don't understand how to control PropertyChangeEvent on exception (FileNotFoundException). I added a boolean flag to control whether exception was thrown or not. Depending on this flag I show or hide progress bar window. But I don't know how to fire this event. For example: public void readData() { try { //read file } catch (FileNotFoundException ex) { isException = true; //Here I must fire PropertyChangeEvent, if I'm not mistaken and create somewhere a progress bar window. } Or I think in a wrong way? – Dragon Mar 20 '13 at 12:40
  • Yes, you can propagate a `PropertyChangeEvent` to signal a `FileNotFoundException`, but you can also transmit the result in your `publish()/process()` implementation. Also consider a [*WatchService for Java 6*](http://stackoverflow.com/q/7968488/230513). – trashgod Mar 20 '13 at 14:43