博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
quartz终止正在运行的任务
阅读量:5983 次
发布时间:2019-06-20

本文共 4239 字,大约阅读时间需要 14 分钟。

import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.net.URL;import java.nio.ByteBuffer;import java.nio.channels.Channels;import java.nio.channels.ClosedByInterruptException;import java.nio.channels.ReadableByteChannel;import java.nio.channels.WritableByteChannel;import java.util.Date;import org.apache.log4j.Logger;import org.quartz.InterruptableJob;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;import org.quartz.JobKey;import org.quartz.UnableToInterruptJobException;public class MyJob implements InterruptableJob {    private static Logger LOG = Logger.getLogger(MyJob.class);    private volatile boolean isJobInterrupted = false;    private JobKey jobKey = null;    private volatile Thread thisThread;    public MyJob() {    }    public void execute(JobExecutionContext context)            throws JobExecutionException {        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 " + new Date());        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 " + new Date());                System.out.println("Job " + jobKey + " completed at " + new Date());            }        }    }    // this method is called by the scheduler    public void interrupt() throws UnableToInterruptJobException {        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 void download(String address, String localFileName)            throws ClosedByInterruptException, IOException {        URL url = new URL(address);        ReadableByteChannel src = Channels.newChannel(url.openStream());        WritableByteChannel dest = new FileOutputStream(new File(localFileName)).getChannel();        try {            System.out.println("Downloading " + address + " to " + new File(localFileName).getCanonicalPath());            int size = fastChannelCopy(src, dest);            System.out.println("Download completed! " + (size / 1024 / 1024) + " MB");        } finally {            src.close();            dest.close();        }    }    // Code copied from http://thomaswabner.wordpress.com/2007/10/09/fast-stream-copy-using-javanio-channels/    private static int fastChannelCopy(final ReadableByteChannel src,            final WritableByteChannel dest) throws IOException {        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);        }        return total;    }}

 

转载地址:http://roeox.baihongyu.com/

你可能感兴趣的文章
poj3067
查看>>
給windowsXP穿上Linux Ubuntu的漂亮馬甲
查看>>
Windows Phone 7 真机调试
查看>>
图灵4月书讯【黑客与画家将在4月10日左右隆重上市】
查看>>
NoSQL for MySQL [HandlerSocket] 之安装与使用
查看>>
"爱说说"技术原理:与TXT交互及MDataTable对Json的功能扩展(二)
查看>>
WPF中,一个文件选择控件的例子
查看>>
程序员职业发展:如何作技术型领导
查看>>
企业应用的互联网化(2010年2月)
查看>>
事物复制的troubleshooting 1
查看>>
用闭包解决jQuery和其它框架的$符号冲突的问题
查看>>
文件锁与NFS文件锁
查看>>
还是那道有趣的题目
查看>>
Eval C#表达式计算
查看>>
HDU-1711-Number Sequence KMP
查看>>
javascript 空格替换 删除
查看>>
TermStore offline的问题
查看>>
poj3404
查看>>
归一化之readline学习
查看>>
软件工程 复习资料(3)
查看>>