Spring定时任务的简单例子

时间:2021-01-05 07:49:10
最近项目需要弄多个定时任务用爬虫批量抓取数据,用的spring定时job功能,简单总结下来:

package util;
import org.apache.log4j.*;
public class TaskJob {
       public static Logger log = Logger
                    .getLogger(TaskJob.class);
      public void SayHello() {
              //TODO Auto-generated method stub
              try{
                    log.info("处理任务开始>........");
                    // 业务逻辑代码调用
                    System.out.println("时间[" + newjava.util.Date().toLocaleString()
                                  + "]----->大家好啊!");
                    log.info("处理任务结束!");
              }catch (Exception e) {
                    log.error("处理任务出现异常", e);
              }
      }

}

2.接下来的在spring 里配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <beanname="taskJob" class="util.TaskJob" />

    <beanid="methodInvokingJobDetail"class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
      <property name="targetObject">