qq_32106973 2018-01-23 09:55 采纳率: 87.5%
浏览 1052
已采纳

求大神帮忙,根据已有代码补充一个方法 判断当前时间为周日并执行定时任务

private static final Logger log = Logger.getLogger(ElectricContractServiceImpl.class);
private ElectricContractDao electricContractDao;
private InquiryContractDAO inquiryContractDAO;
@SuppressWarnings("unchecked")
public void AutoExcute() throws Exception {
    String fileRealPath = "";// 附件的完整路径
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    //格式化对象
    String strD = sdf.format(new Date());
    //当天执行,获取前一天数据(现为同步前一周数据)
    String beginTime = DateConvertor.dateToString(DateConvertor.offsetDate(new Date(), -7, "day"), "yyyy-MM-dd HH:mm:ss");
    String endTime = DateConvertor.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss");
    List<Contract> list = electricContractDao.findContract(beginTime, endTime);
    // 根路径
    String base = FtpPropertiesReader.getProperty("ftpElectricLocalDir");
    String filename = "ContractInfo_" + strD; // 文件名
    fileRealPath = base + filename + ".csv";
    File file = new File(fileRealPath);// 新建文件
    if (!file.exists()) {
        file.createNewFile();
    }
    if (log.isDebugEnabled()) {
        log.debug("创建文件-开始");
    }
    FileOutputStream outStr = new FileOutputStream(file);
    BufferedOutputStream buff = new BufferedOutputStream(outStr);
  • 写回答

6条回答 默认 最新

  • shushanke 2018-01-23 15:59
    关注

    建议使用Quartz来管理定时任务。
    如果是你的项目用了Spring,那就可以通过配置来实现定时任务。
    我这里用了Spring4.2.6,Quartz2.3.0(2.0.2以及更新版本),slf4j-api-1.7.25.jar、slf4j-log4j12-1.7.25.jar、log4j1.2.17、commons-logging1.2等。
    1、写个任务执行类
    import java.text.SimpleDateFormat;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;

    public class MyJobExecutor {
    private static final Log log = LogFactory.getLog(MyJobExecutor.class);
    private static final SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public void autoExcute() {
        String now = datetimeFormat.format(new java.util.Date());
        log.info("当前时刻:" + now + ",开始执行定时任务...");
        // 具体的操作...
    }
    

    }

    2、配置定时任务的触发器
    <?xml version="1.0" encoding="UTF-8"?>
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
    spring quartz beans
    <!-- 任务执行类的Bean配置,路径自定义-->

    <!-- 1、配置需要定时任务类和执行方法 -->
    <bean id="myJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <!-- 指定任务类 -->
        <property name="targetObject" ref="myExecutor" />
        <!-- 执行任务的方法 -->
        <property name="targetMethod" value="autoExcute" />
    </bean>
    
    <!-- 2、配置触发器 -->
    <bean id="myTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="myJobDetail" />
        <!-- 每隔10秒钟触发一次 -->
        <!-- <property name="cronExpression" value="0/10 * * * * ?" /> -->
        <!-- 每周日上午7点30分触发一次。表达式网上可查找相关资料 。-->
        <property name="cronExpression" value="0 30 7 ? * SUN"/>
    </bean>
    
    <!-- 3、触发定时任务 -->
    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <!-- 可配置多个触发器 -->
                <ref bean="myTrigger" />
            </list>
        </property>
    </bean>
    

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料