Skip to Main Content

Swing and batch job

user11440683Oct 25 2017 — edited Nov 3 2017


I have written a java class that takes some considerable time to do what it does.

I want my end user to invoke this, amongst other functionality, from a java swing form.

I have developed my form.

I have used SwingWorker to launch a thread to run my batch job, which works okay.

Without tieing up the form I want the job to run so the user can perform other actions in the form, whilst the batch job is running. I also want the form to check in the background periodically if the batch job has finished successfully, or failed with error. Note my job may run for over an hour, so tieing up the form is not an option....

My problems are; -

The form freezes on launch of the background process

I am not sure if this approach will work with a batch process that does some fairly heavy lifting...

I am not sure if this is the best way to have a 'concurrent request' equivalent

I am using a thread for my batch process but from reading around threads suspect the job may be too 'heavy' for a thread, is there something I would be better using in this case?

Constructive suggestions gratefully received!

Code excerpt as below; -

public void actionPerformed(ActionEvent e){ 
                b.setText("Batch Launched");
                Progress prog =new Progress();
         prog.status ="Launched Daily Backup Routine";

     //swing safe thread

      SwingWorker<Boolean, Void> worker = new SwingWorker<Boolean, Void>() {
      protected Boolean doInBackground() throws Exception {
    // run batch job here

                  return true;

   // Can safely update the GUI from this method.
         protected void done() {
             boolean status;
           try {b.setText(prog.status);
     // Retrieve the return value of doInBackground.

    //think the below is my biggest problem that freezes the form
             status = get();
        if (status = true){b.setText("Completed backup successfully");
        else {b.setText("Backup failed");}

            } catch (InterruptedException e) {
     // This is thrown if the thread's interrupted.
          catch (Throwable e) {
     // This is thrown if we throw an exception
     // from doInBackground.

Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked due to inactivity on Nov 30 2017
Added on Oct 25 2017