importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;importjava.net.URL;importjava.nio.ByteBuffer;importjava.nio.channels.Channels;importjava.nio.channels.ClosedByInterruptException;importjava.nio.channels.ReadableByteChannel;importjava.nio.channels.WritableByteChannel;importjava.util.Date;importorg.apache.log4j.Logger;importorg.quartz.InterruptableJob;importorg.quartz.JobExecutionContext;importorg.quartz.JobExecutionException;importorg.quartz.JobKey;importorg.quartz.UnableToInterruptJobException;public class MyJob implementsInterruptableJob {private static Logger LOG = Logger.getLogger(MyJob.class);private volatile boolean isJobInterrupted = false;private JobKey jobKey = null;private volatileThread thisThread;publicMyJob() {
}public voidexecute(JobExecutionContext context)throwsJobExecutionException {
thisThread=Thread.currentThread();//LOG.info("Thread name of the current job: " + thisThread.getName());
System.out.println("Thread name of the current job: " +thisThread.getName());
jobKey=context.getJobDetail().getKey();//LOG.info("Job " + jobKey + " executing at " + new Date());
System.out.println("Job " + jobKey + " executing at " + newDate());try{
String fileUrl= "http://d2zwv9pap9ylyd.cloudfront.net/terracotta-3.6.1.tar.gz"; //59 MB
String localFile = "d:/terracotta-3.6.1.tar.gz";
download(fileUrl, localFile);
}catch(ClosedByInterruptException e) {
LOG.info("Caught ClosedByInterruptException... exiting job.");
e.printStackTrace();
}catch(IOException e) {
LOG.info("Caught IOException... exiting job.", e);
e.printStackTrace();
}finally{if(isJobInterrupted) {
LOG.info("Job " + jobKey + " did not complete");
System.out.println("Job " + jobKey + " did not complete");
}else{
LOG.info("Job " + jobKey + " completed at " + newDate());
System.out.println("Job " + jobKey + " completed at " + newDate());
}
}
}//this method is called by the scheduler
public void interrupt() throwsUnableToInterruptJobException {
LOG.info("Job " + jobKey + " -- INTERRUPTING --");
System.out.println("Job " + jobKey + " -- INTERRUPTING --");
isJobInterrupted= true;if (thisThread != null) {//this called cause the ClosedByInterruptException to happen
thisThread.interrupt();
}
}private voiddownload(String address, String localFileName)throwsClosedByInterruptException, IOException {
URL url= newURL(address);
ReadableByteChannel src=Channels.newChannel(url.openStream());
WritableByteChannel dest= new FileOutputStream(newFile(localFileName)).getChannel();try{
System.out.println("Downloading " + address + " to " + newFile(localFileName).getCanonicalPath());int size =fastChannelCopy(src, dest);
System.out.println("Download completed! " + (size / 1024 / 1024) + " MB");
}finally{
src.close();
dest.close();
}
}//Code copied fromhttp://thomaswabner.wordpress.com/2007/10/09/fast-stream-copy-using-javanio-channels/
private static int fastChannelCopy(finalReadableByteChannel src,final WritableByteChannel dest) throwsIOException {final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);int count = 0;int total = 0;while ((count = src.read(buffer)) != -1) {
total+=count;//prepare the buffer to be drained
buffer.flip();//write to the channel, may block
dest.write(buffer);//If partial transfer, shift remainder down//If buffer is empty, same as doing clear()
buffer.compact();
}//EOF will leave buffer in fill state
buffer.flip();//make sure the buffer is fully drained.
while(buffer.hasRemaining()) {
dest.write(buffer);
}returntotal;
}
}