From a2b74ef7826a99fc731056a54c7b6e9bd280843b Mon Sep 17 00:00:00 2001 From: bruse Date: Tue, 16 Dec 2014 12:21:28 +0000 Subject: [PATCH] Now the caller of job.poll(int) can pass the amount of seconds to wait between two status update requests. --- .../simstadt/nf4j/async/AsyncExportJob.java | 27 ++++-- .../simstadt/nf4j/async/AsyncImportJob.java | 22 +++-- src/eu/simstadt/nf4j/async/AsyncJob.java | 5 +- src/eu/simstadt/nf4j/async/Client.java | 85 +++++++++++++++++++ .../simstadt/nf4j/async/JobStatusEvent.java | 2 +- src/eu/simstadt/nf4j/async/Main.java | 71 ---------------- .../nf4j/async/PollJobStatusTask.java | 4 +- 7 files changed, 130 insertions(+), 86 deletions(-) create mode 100644 src/eu/simstadt/nf4j/async/Client.java delete mode 100644 src/eu/simstadt/nf4j/async/Main.java diff --git a/src/eu/simstadt/nf4j/async/AsyncExportJob.java b/src/eu/simstadt/nf4j/async/AsyncExportJob.java index 52ce145..69d3a96 100644 --- a/src/eu/simstadt/nf4j/async/AsyncExportJob.java +++ b/src/eu/simstadt/nf4j/async/AsyncExportJob.java @@ -22,10 +22,10 @@ public class AsyncExportJob extends ExportJob implements AsyncJob { /** - * While polling for the current job status, the polling thread will sleep for this amount of time within - * each iteration. + * While polling for the current job status, the polling thread will sleep for this amount of seconds + * before each status update request. */ - private final long DEFAULT_POLLING_INTERVAL = 5; // seconds + private final int DEFAULT_POLLING_INTERVAL = 5; // seconds /** There can only be one sending thread for each job at a time. */ private Thread sendThread; @@ -117,11 +117,17 @@ public synchronized void send() throws FailedTransmissionException, InvalidJobDe * Frequently queries the remote status of the nF export job and updates the local status accordingly. * The queries will be performed asynchronously in a separate thread. Job status listener will be notified * upon every new status change. - * + * + * Note, there can only be one polling thread at a time. Subsequent calls of poll() will stop the previously + * started poll threads. + * + * @param interval Amount of seconds to wait before the next status update request will be sent to the + * nF server. + * * @throws FailedTransmissionException If your job has not been sent yet, then you will get some of this. */ @Override - public synchronized void poll() throws FailedTransmissionException { + public synchronized void poll(int interval) throws FailedTransmissionException { if (status.compareTo(JobStatus.SENT) < 0) { throw new FailedTransmissionException("The job has not been sent to the nF yet!"); } @@ -129,10 +135,19 @@ public synchronized void poll() throws FailedTransmissionException { pollThread.interrupt(); } keepPolling = true; - pollThread = new Thread(new PollJobStatusTask(this, DEFAULT_POLLING_INTERVAL)); + pollThread = new Thread(new PollJobStatusTask(this, interval)); pollThread.start(); } + /** + * Convenience method for polling with a predefined default interval. + * + * @see poll(int) + */ + public synchronized void poll() throws FailedTransmissionException { + poll(DEFAULT_POLLING_INTERVAL); + } + /** * Connects to the nF and refreshes the status of this job. If there is no nF connector set, * this operation will throw a FailedTransmissionException. diff --git a/src/eu/simstadt/nf4j/async/AsyncImportJob.java b/src/eu/simstadt/nf4j/async/AsyncImportJob.java index b65cc22..f19d71c 100644 --- a/src/eu/simstadt/nf4j/async/AsyncImportJob.java +++ b/src/eu/simstadt/nf4j/async/AsyncImportJob.java @@ -20,10 +20,10 @@ public class AsyncImportJob extends ImportJob implements AsyncJob { /** - * While polling for the current job status, the polling thread will sleep for this amount of time within - * each iteration. + * While polling for the current job status, the polling thread will sleep for this amount of seconds + * before each status update request. */ - private final long DEFAULT_POLLING_INTERVAL = 5; // seconds + private final int DEFAULT_POLLING_INTERVAL = 5; // seconds private boolean jobTransmissionTriggered = false; @@ -94,10 +94,13 @@ public synchronized void send() throws InvalidJobDescriptorException, FailedTran * The queries will be performed asynchronously in a separate thread. Job status listener will be notified * upon every new status change. * + * @param interval Amount of seconds to wait before the next status update request will be sent to the + * nF server. + * * @throws FailedTransmissionException If your job has not been sent yet, then you will get some of this. */ @Override - public synchronized void poll() throws FailedTransmissionException { + public synchronized void poll(int interval) throws FailedTransmissionException { if (status.compareTo(JobStatus.SENT) < 0) { throw new FailedTransmissionException("The job has not been sent to the nF yet!"); } @@ -105,10 +108,19 @@ public synchronized void poll() throws FailedTransmissionException { pollThread.interrupt(); } keepPolling = true; - pollThread = new Thread(new PollJobStatusTask(this, DEFAULT_POLLING_INTERVAL)); + pollThread = new Thread(new PollJobStatusTask(this, interval)); pollThread.start(); } + /** + * Convenience method for polling with a predefined default interval. + * + * @see poll(int) + */ + public synchronized void poll() throws FailedTransmissionException { + poll(DEFAULT_POLLING_INTERVAL); + } + /** * Connects to the nF and refreshes the status of this job. If there is no nF connector set, * this operation will throw a FailedTransmissionException. diff --git a/src/eu/simstadt/nf4j/async/AsyncJob.java b/src/eu/simstadt/nf4j/async/AsyncJob.java index 604988f..25a4fa1 100644 --- a/src/eu/simstadt/nf4j/async/AsyncJob.java +++ b/src/eu/simstadt/nf4j/async/AsyncJob.java @@ -16,10 +16,13 @@ public interface AsyncJob { * Frequently queries the remote status of the nF export job and updates the local status accordingly. * The queries will be performed asynchronously in a separate thread. Job status listener will be notified * upon every new status change. + * + * @param interval Amount of seconds to wait before the next status update request will be sent to the + * nF server. * * @throws FailedTransmissionException If your job has not been sent yet, then you will get some of this. */ - public void poll() throws FailedTransmissionException; + public void poll(int interval) throws FailedTransmissionException; /** * Cancels all ongoing asynchronous operations of this job as soon as possible. Operations to be canceled diff --git a/src/eu/simstadt/nf4j/async/Client.java b/src/eu/simstadt/nf4j/async/Client.java new file mode 100644 index 0000000..d90bf95 --- /dev/null +++ b/src/eu/simstadt/nf4j/async/Client.java @@ -0,0 +1,85 @@ +package eu.simstadt.nf4j.async; + +import java.io.File; + +import eu.simstadt.nf4j.FailedTransmissionException; +import eu.simstadt.nf4j.InvalidJobDescriptorException; +import eu.simstadt.nf4j.JobStatus; + +/** + * This is a class for development and test purposes. It is not needed for production. You may want to delete it. + * + * @author Marcel Bruse + */ +public class Client implements JobStatusListener { + + public AsyncExportJob job; + + public static void main(String[] args) { + Client client = new Client(); + client.processJob(); + } + + public void processJob() { + // Describe the export job + ExportJobDescription description= ExportJobDescription.getDefaultDescriptor(); + description.setInitiator("2758"); + description.setAccount("Bruse"); + description.setProduct("WU3"); + description.setJobnumber("Bruse_0815"); + + // Set at least one unit. You could also define a region polygon. + Unit unit = Unit.getDefaultUnit(); + unit.setValue("821"); + description.addUnit(unit); + + // Set at least one layer + Layer layer = Layer.getDefaultLayer(); + layer.setProduct("WU3"); + layer.setName("GML"); + description.addLayer(layer); + + // Create a new export job with the job description and a HTTP connection + job = new AsyncExportJob(description, new HTTPConnection("193.196.136.164")); + + // Register a job status listener + job.addJobStatusListener(this); + + try { + // Send the export job to the nF server in a separate thread + job.send(); + } catch (FailedTransmissionException ex) { + // There was a problem with your connection or the job has been sent twice + ex.printStackTrace(); + } catch (InvalidJobDescriptorException ex) { + // The export job description is invalid + ex.printStackTrace(); + } + } + + // This client is a job status listener and will get notified upon job status changes + @Override + public void jobStatusChanged(JobStatusEvent event) { + JobStatus status = (JobStatus) event.getSource(); + System.out.println(status); + if (status == JobStatus.FINISHED) { + try { + // Start an asynchronous download of the resulting CityGML file + job.downloadResult(); + } catch (FailedTransmissionException ex) { + // Only finished jobs can download files + ex.printStackTrace(); + } + } else if (status == JobStatus.DOWNLOAD) { + try { + // Obtain the CityGML file handle + File file = job.getResult(); + System.out.println("CityGML at " + file.getAbsolutePath()); + } catch (FailedTransmissionException ex) { + // Download may have failed + ex.printStackTrace(); + } + } + } + +} \ No newline at end of file diff --git a/src/eu/simstadt/nf4j/async/JobStatusEvent.java b/src/eu/simstadt/nf4j/async/JobStatusEvent.java index d8d0c3b..15359a8 100644 --- a/src/eu/simstadt/nf4j/async/JobStatusEvent.java +++ b/src/eu/simstadt/nf4j/async/JobStatusEvent.java @@ -8,7 +8,7 @@ /** * Every time when the status of a job progresses, one of this events will be created and sent - * to all off the registered job status listeners of the job. Job status listeners implement + * to all of the job status listeners registered at the job. Job status listeners implement * the jobStatusChanged() method which takes this JobStatusEvent as its argument. This event will * contain the job status as the event source and listener can read the source and decide how to * deal with the new status of its observed job. diff --git a/src/eu/simstadt/nf4j/async/Main.java b/src/eu/simstadt/nf4j/async/Main.java deleted file mode 100644 index 224e76a..0000000 --- a/src/eu/simstadt/nf4j/async/Main.java +++ /dev/null @@ -1,71 +0,0 @@ -package eu.simstadt.nf4j.async; - -import java.io.File; - -import eu.simstadt.nf4j.FailedTransmissionException; -import eu.simstadt.nf4j.InvalidJobDescriptorException; -import eu.simstadt.nf4j.JobStatus; - -/** - * This is a class for development and test purposes. It is not needed for production. You may want to delete it. - * - * @author Marcel Bruse - */ -public class Main implements JobStatusListener { - - public AsyncExportJob job; - - public void doit() { - ExportJobDescription jobDescriptor = ExportJobDescription.getDefaultDescriptor(); - jobDescriptor.setProduct("WU3"); - - Unit unit = Unit.getDefaultUnit(); - unit.setValue("821"); - jobDescriptor.addUnit(unit); - - Layer layer = Layer.getDefaultLayer(); - layer.setProduct("WU3"); - layer.setName("GML"); - jobDescriptor.addLayer(layer); - - job = new AsyncExportJob(jobDescriptor, new HTTPConnection("193.196.136.164")); - job.addJobStatusListener(this); - - try { - job.send(); - } catch (FailedTransmissionException ex) { - ex.printStackTrace(); - } catch (InvalidJobDescriptorException ex) { - ex.printStackTrace(); - } - - System.out.println("Main thread is back again"); - } - - public static void main(String[] args) { - Main m = new Main(); - m.doit(); - } - - @Override - public void jobStatusChanged(JobStatusEvent event) { - System.out.println(event.getSource() + ": " + event.getMessage().orElse("(no message)")); - if (event.getSource() == JobStatus.FINISHED) { - try { - job.downloadResult(); - } catch (FailedTransmissionException ex) { - // TODO Auto-generated catch block - ex.printStackTrace(); - } - } else if (event.getSource() == JobStatus.DOWNLOAD) { - try { - File f = job.getResult(); - System.out.println("File can be read for job "+ event.getJob().getId() +": " + f.getAbsolutePath()); - } catch (FailedTransmissionException ex) { - // TODO Auto-generated catch block - ex.printStackTrace(); - } - } - } - -} \ No newline at end of file diff --git a/src/eu/simstadt/nf4j/async/PollJobStatusTask.java b/src/eu/simstadt/nf4j/async/PollJobStatusTask.java index 0de2fc3..6279a25 100644 --- a/src/eu/simstadt/nf4j/async/PollJobStatusTask.java +++ b/src/eu/simstadt/nf4j/async/PollJobStatusTask.java @@ -18,7 +18,7 @@ public class PollJobStatusTask implements Runnable { * Don't flood your nF server with status request. This interval ensures that your server will receive * a status request within every time interval. */ - private long interval; + private int interval; /** * Constructor with asynchronous job and the poll interval. @@ -26,7 +26,7 @@ public class PollJobStatusTask implements Runnable { * @param job The job to update frequently. * @param interval The time interval for one request. */ - public PollJobStatusTask(AsyncJob job, long interval) { + public PollJobStatusTask(AsyncJob job, int interval) { this.job = job; this.interval = interval; } -- GitLab