SendExportJobTask.java 1.62 KB
Newer Older
bruse's avatar
bruse committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package eu.simstadt.nf4j.async;

import java.util.Optional;

import eu.simstadt.nf4j.FailedTransmissionException;
import eu.simstadt.nf4j.InvalidJobDescriptorException;
import eu.simstadt.nf4j.JobStatus;

/**
 * This task sends an export job to your nF server asynchronously within a separate send thread. Once the send
 * operation has finished all of the jobs status listeners will be notified. You can cancel this task by calling
 * job.cancel().
 *  
 * @author Marcel Bruse
 */
public class SendExportJobTask implements Runnable {
	
	/** The job to be sent to your nF server. */
	private AsyncExportJob job;

	/**
	 * Constructor with the export job to be sent. 
	 * 
	 * @param job The export job to be sent.
	 */
	public SendExportJobTask(AsyncExportJob job) {
		this.job = job;
	}
	
	/**
	 * This methods performs the actual send operation asynchronously in a separate send thread.
	 * Job status listeners will be notified once the operation finishes or fails.
	 */
	@Override
	public void run() {
		try {
			HTTPConnection connector = (HTTPConnection) job.getConnector();
			connector.sendAndUpdateExportJob(job);
			job.poll();
		} catch (InvalidJobDescriptorException ex) {
			signalError("Job cancel because of an invalid job description!");
		} catch (FailedTransmissionException ex) {
			signalError("The job transmission failed. There seams to be a problem with the connector!");
		}
	}
	
	/**
48
	 * This method is superfluous I guess? TODO: Please check and refactor it.
bruse's avatar
bruse committed
49
50
51
52
53
	 */
	private void signalError(String errorMessage) {
		job.setStatus(JobStatus.UNKOWN, Optional.of(errorMessage));
	}

54
}