Commit a2b74ef7 authored by bruse's avatar bruse
Browse files

Now the caller of job.poll(int) can pass the amount of seconds to wait between...

Now the caller of job.poll(int) can pass the amount of seconds to wait between two status update requests.
parent c51f5397
......@@ -22,10 +22,10 @@
public class AsyncExportJob extends ExportJob<ExportJobDescription> 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.
......
......@@ -20,10 +20,10 @@
public class AsyncImportJob extends ImportJob<ImportJobDescription> 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.
......
......@@ -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
......
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
......@@ -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.
......
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
......@@ -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;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment