Timer schedule 与Time cancel

我们看到,有一个ExcelDownloadTask 任务,做的工作是等人ftp并下载指定目录下的所有文件到本地.

我们还有一个类DoExcelDownload 提供2个方法,分别负责启动ExcelDownloadTask 和停止ExcelDownloadTask .

现在如果放假个文件到ftp指定目录下,启动任务并在20毫秒之后停止任务,我们发现输出结果是:

>>>>>>>>>>>>>>>>>main

>>>>>>>>>>>>>>>>start task

>>>>>>>>>>>>>>>>>run

>>>>>>>>>>>>>>>>>end task

get file:1.txt

get file:2.txt

get file:3.txt

get file:4.txt

get file:5.txt

get file:6.txt

即一触发task,time停了也不会影响最后一次任务.

 

package com.ssc.cm.datamart.excelDownload;

import java.util.Timer;

import org.apache.log4j.Logger;

public class DoExcelDownload {

private static final Logger LOGGER = Logger.getLogger(DoExcelDownload.class);

private static Timer time = null;

public static void main(String[] args) throws InterruptedException {

LOGGER.info(">>>>>>>>>>>>>>>>>main");

start();

Thread.sleep(20);

cancel();

}

public static void start(){

LOGGER.info(">>>>>>>>>>>>>>>>start task");

time = new Timer();

time.schedule(new ExcelDownloadTask(), 0,60000L);

}

public static void cancel(){

LOGGER.info(">>>>>>>>>>>>>>>>>end task");

if(time!=null){

time.cancel();

}

}

}

 

 

 

 

 

 

package com.ssc.cm.datamart.excelDownload;

 

import java.io.File;

 

import java.io.IOException;

 

import java.util.Calendar;

 

import java.util.Date;

 

import java.util.HashSet;

 

import java.util.Set;

 

import java.util.Timer;

 

import java.util.TimerTask;

 

import org.apache.log4j.Logger;

 

import com.enterprisedt.net.ftp.FTPClient;

 

import com.enterprisedt.net.ftp.FTPException;

 

import com.enterprisedt.net.ftp.FTPMessageCollector;

 

public class ExcelDownloadTask extends TimerTask{

private static final Logger LOGGER = Logger.getLogger(ExcelDownloadTask.class);

@Override

 

public void run() {

LOGGER.info(">>>>>>>>>>>>>>>>>run");

File file = new File("");

String localFolder = file.getAbsolutePath()+"\\newfiles\\";

FTPClient ftp = null;

try {

ftp = new FTPClient();

ftp.setRemoteHost(host);

FTPMessageCollector listener = new FTPMessageCollector();

ftp.setMessageListener(listener);

ftp.connect();

ftp.login(user, userName);

ftp.chdir(dir);

String[] files;

files = ftp.dir();

Set<File> localFiles = new HashSet<File>();

if (files.length > 0) {

for(String fileName : files){

LOGGER.info("get file:"+fileName);

String localFile = localFolder + fileName.substring(0,fileName.lastIndexOf("."))+ (new Date().getTime())+fileName.substring(fileName.lastIndexOf("."));

ftp.get(localFile, fileName);

localFiles.add(new File(localFile));

ftp.delete(fileName);

}

}

ftp.quit();

} catch (IOException e) {

LOGGER.info(e.getMessage());

} catch (FTPException e) {

LOGGER.info(e.getMessage());

}finally {

if (ftp.connected()) {

try {

ftp.quit();

} catch (IOException e) {

e.printStackTrace();

} catch (FTPException e) {

e.printStackTrace();

}

}

}

}

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值