spring 中配置文件,定义 每天6:10:10 执行任务(6点10分10秒执行任务)
<description>upop 任务执行信息 </description>
<!-- 系统SchedulerFactory -->
<!-- -->
<bean id="upopQuartzSchedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="upopTrigger"/>
</list>
</property>
<property name="autoStartup" value="true"/>
</bean>
<!-- 每天 6:10:10触发一次 -->
<bean id="upopTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="upopJobDetail"/>
<property name="cronExpression" value="10 10 6 * * ?"/>
</bean>
<!-- -->
<bean id="upopJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="clearFileService"/>
<property name="targetMethod" value="upopAndChinaNums"/>
<property name="concurrent" value="false"/>
</bean>
clearFileService :执行自动任务类的实例。
upopAndChinaNums : 执行任务类中要执行的方法。
类定义:
@Service("clearFileService")
public class ClearFileServiceImp implements ClearFileService{
static private Logger log = LoggerFactory.getLogger(ClearFileServiceImp.class);
@Resource
private ClearFileDao clearFileDao;
/**
* 对账信息
*/
public void upopAndChinaNums() throws ServiceException{
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("transtime", "20141229");
List<UpopCheckPojo> list = clearFileDao.upopAndChinaNums(map);
if(list !=null && list.size() > 0){
UpopAndCheck upopAndCheck = new UpopAndCheck(list);
//准备参数
Date date = Tools.dateDay(Calendar.getInstance().getTime(), -1);
log.info("对账日期:"+date);
//本地文件名称前缀
String filePfx = FileUp.getItem("upop.up.ftp.down.fileName");
//对账文件名中日期格式
String dateFormat = FileUp.getItem("upop.up.ftp.fileName_date");
String dateFile = Tools.dateToStr(date, dateFormat);
//本地路径 upop.up.ftp.localpath=e:/
String localPath =FileUp.getItem("upop.up.ftp.localpath");
StringBuilder sbFileName = new StringBuilder(20);
sbFileName.append(filePfx).append(dateFile);
boolean bFile = upopAndCheck.writeFile(localPath+sbFileName.toString(), "GBK");
log.info("生成对账文件状态:"+bFile);
//上传对账日志
if(bFile){
ftpResult(sbFileName.toString());
}
}else{
log.info("今天没有成功对账记录 日期:"+Calendar.getInstance().getTime());
}
}
public boolean ftpResult(String fileName){
boolean flag = false;
try {
long start = System.currentTimeMillis();
String ip =FileUp.getItem("upop.up.ftp.ip").trim();
String ftpName = FileUp.getItem("upop.up.ftp.name").trim();
String ftpPwd = FileUp.getItem("upop.up.ftp.pwd").trim();
String ftpPort = FileUp.getItem("upop.up.ftp.port").trim();
int port = Integer.parseInt(ftpPort);
//#ftp服务器路径
String serverPath = FileUp.getItem("upop.up.ftp.serverpath");
//#本地路径
String localpath = FileUp.getItem("upop.up.ftp.localpath");
FileUp fileUp = new FileUp();
fileUp.connectServer(ftpName, ftpPwd, ip, port);
flag = fileUp.fileUp(serverPath, localpath, fileName);
fileUp.closeConnect();
long end = System.currentTimeMillis();
log.info("上传用时:"+(end - start));
} catch (Exception e) {
log.error("文件上传错误:"+e.getMessage());
}
return flag;
}
}