java callable

多线程callable

package com.bmac.rechar.tdztask;

import java.io.IOException;
import java.net.SocketException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPListParseEngine;
import org.apache.commons.net.ftp.FTPReply;


public class NoZhiFuBao {
public static void main(String[] args) throws SocketException, IOException {
//zfb20181206.txt C:\huiandong\opt\weixin\bmac_collate\destdir\zfbfile
String path="/huiandong/opt/weixin/bmac_collate/destdir/zfbfile/";
boolean theFileIsExist = theFileIsExists(path,3,11);
System.out.println(theFileIsExist);


}

//调用方法判断是否存在对账记录
public void rechargeSettlement(String zfb,String wx,String qyd,String ykt){
int taskSize=4;
ExecutorService executorService = Executors.newFixedThreadPool(taskSize);


Callable<String> zfbSettle = new Callable<String>() {
@Override
public String call() throws Exception {
boolean theFileIsExists = theFileIsExists(zfb,3,11);
if(theFileIsExists){
return "yes";
}
return "no";
}
};

Callable<String> wxSettle = new Callable<String>() {
@Override
public String call() throws Exception {
boolean theFileIsExists = theFileIsExists(zfb,3,11);
if(theFileIsExists){
return "yes";
}
return "no";
}
};

Callable<String> qydSettle = new Callable<String>() {
@Override
public String call() throws Exception {
boolean theFileIsExists = theFileIsExists(zfb,3,11);
if(theFileIsExists){
return "yes";
}
return "no";
}
};


Callable<String> yktSettle = new Callable<String>() {
@Override
public String call() throws Exception {
boolean theFileIsExists = theFileIsExists(zfb,3,11);
if(theFileIsExists){
return "yes";
}
return "no";
}
};
Future<String> zfbExists = executorService.submit(zfbSettle);
Future<String> wxExists = executorService.submit(wxSettle);
Future<String> qydExists = executorService.submit(qydSettle);
Future<String> yktExists = executorService.submit(yktSettle);
}


//判断是否包含当日
static synchronized boolean theFileIsExists(String path,int beginIndex,int endIndex){
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect("node02");
ftpClient.setControlEncoding("UTF-8");
int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
}
ftpClient.login("root","123456");

FTPFile[] listFiles = ftpClient.listFiles(path);
if(isExist2(listFiles,3,11)){
return true;
}
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}

static boolean isExist2(FTPFile[] listNames,int beginIndex,int endIndex){
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
Calendar instance = Calendar.getInstance();
instance.add(Calendar.DATE, -1);
Date time = instance.getTime();
for(int i=0;i<listNames.length;i++){
String date=listNames[i].getName().substring(beginIndex, endIndex);
if(date!=null && date.equals(sdf.format(time))){
return true;
}
}
return false;
}


}

posted @ 2018-12-26 11:32  uuhh  阅读(379)  评论(0编辑  收藏  举报