Hi,
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");
b.setEnabled(false);
Progress prog =new Progress();
prog.status ="Launched Daily Backup Routine";
prog.stage=0;
//swing safe thread
SwingWorker<Boolean, Void> worker = new SwingWorker<Boolean, Void>() {
@Override
protected Boolean doInBackground() throws Exception {
// run batch job here
DailyBatch.main(null);
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");
b.setEnabled(true);}
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.
}
}