java quartz 停止_quartz终止正在运行的任务

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;

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值