maven项目中quartz的使用

在项目中,定时器的使用还是比较常见的。最近领导要我做一个员工日常考核分数排名,要求是根据员工每个人不同的岗位,每月月初固定生成一张相对应的考核表,管理层每个星期可填写,再一月统计最后的得分。一开始我是想用存储过程去实现的,我们领导说不用存储过程,我就决定用定时任务实现这个功能。

第一步:在pom.xml添加依赖

<dependency>
  <groupId>org.quartz-scheduler</groupId>
  <artifactId>quartz</artifactId>
  <version>2.2.1</version>
</dependency>

第二步:创建一个任务

@Service("quartzJob")
public class QuartzJob {

  @Autowired
  private WeekAssessmentScoreService weekAssessmentScoreService;
  @Autowired
  private DailyAssessmentService dailyAssessmentService;
  @Autowired
  private SystemUserService systemUserService;

  public void execute() {
    System.out.println("执行定时任务=========================================================");
    Map map  = new HashMap();
    List departmentIds = new ArrayList();
    departmentIds.add(4);
    departmentIds.add(9);
    departmentIds.add(10);
    map.put("departmentIds",departmentIds);
    map.put("status",1);
    List<SystemUserDetailed> systemUserDetaileds = systemUserService.listAllUsers(map);
    for (SystemUserDetailed systemUserDetailed : systemUserDetaileds) {
      DailyAssessment dailyAssessment = new DailyAssessment();
      dailyAssessment.setSystemUserId(systemUserDetailed.getId());
      dailyAssessment.setSystemUserName(systemUserDetailed.getName());
      dailyAssessment.setDepartmentId(systemUserDetailed.getDepartmentId());
      dailyAssessment.setRoleId(systemUserDetailed.getRoleId());
      dailyAssessment.setMonth(Integer.valueOf(DatetimeUtils.format(new Date(), "yyyyMM")));
      if(dailyAssessment.getDepartmentId() == 19){
        //信息流竞价助理
        if (dailyAssessment.getRoleId() == 10) {
          int length = 19;
          int insert = dailyAssessmentService.insertSelective(dailyAssessment);
          int i = weekAssessmentScoreAdd(dailyAssessment, length);
        }
        //信息流竞价专员
        if (dailyAssessment.getRoleId() == 9 ) {
          int length = 18;
          int insert = dailyAssessmentService.insertSelective(dailyAssessment);
          int i = weekAssessmentScoreAdd(dailyAssessment, length);
        }
        //信息流数据统计员
        if (dailyAssessment.getRoleId() == 26 ) {
          int length = 11;
          int insert = dailyAssessmentService.insertSelective(dailyAssessment);
          int i = weekAssessmentScoreAdd(dailyAssessment, length);
        }
        //信息流竞价主管
        if (dailyAssessment.getRoleId() == 8){
          int length = 17;
          int insert = dailyAssessmentService.insertSelective(dailyAssessment);
          int i = weekAssessmentScoreAdd(dailyAssessment, length);
        }
      }else{
        //竞价经理、竞价主管、竞价培训师
        if (dailyAssessment.getRoleId() == 8
            || dailyAssessment.getRoleId() == 23 || dailyAssessment.getRoleId() == 31) {
          int length = 17;
          int insert = dailyAssessmentService.insertSelective(dailyAssessment);
          int i = weekAssessmentScoreAdd(dailyAssessment, length);
        }
        //竞价助理
        if (dailyAssessment.getRoleId() == 10 ) {
          int length = 16;
          int insert = dailyAssessmentService.insertSelective(dailyAssessment);
          int i = weekAssessmentScoreAdd(dailyAssessment, length);
        }
        //竞价专员、竞价总监助理
        if (dailyAssessment.getRoleId() == 9 || dailyAssessment.getRoleId() == 11) {
          int length = 15;
          int insert = dailyAssessmentService.insertSelective(dailyAssessment);
          int i = weekAssessmentScoreAdd(dailyAssessment, length);
        }
        //竞价数据统计员
        if (dailyAssessment.getRoleId() == 26 ) {
          int length = 13;
          int insert = dailyAssessmentService.insertSelective(dailyAssessment);
          int i = weekAssessmentScoreAdd(dailyAssessment, length);
        }
      }
    }
  }

  private int weekAssessmentScoreAdd(DailyAssessment dailyAssessment, int param) {
    int insert = 0;
    for (int x = 1; x <= param; x++) {
      WeekAssessmentScore weekAssessmentScore = new WeekAssessmentScore();
      weekAssessmentScore.setDailyAssessmentId(dailyAssessment.getId());
      weekAssessmentScore.setType(x);
      insert = weekAssessmentScoreService.insertSelective(weekAssessmentScore);
    }
    return insert;
  }
}

注意:需要在创建的任务类上添加@Service注解

第三步:spring配置

<!--开启任务注解扫描-->

<context:component-scan base-package="com.cll.platform.web.task" />

<!-- 配置任务线性池 --> 
<task:executor id="executor" pool-size="5" />
<task:scheduler id="scheduler" pool-size="10" />

<!-- 启用annotation方式 --> 
<task:annotation-driven executor="executor" scheduler="scheduler" />
<!-- 要调用的工作类 -->
<bean id="quartzJob" class="com.cll.platform.web.task.QuartzJob"></bean>
<!-- 定义调用对象和调用对象的方法 -->
<bean id="jobtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <!-- 调用的类 -->
    <property name="targetObject">
        <ref bean="quartzJob"/>
    </property>
    <!-- 调用类中的方法 -->
    <property name="targetMethod">
        <value>execute</value>
    </property>
</bean>
<!-- 定义触发时间 -->
<bean id="doTime" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
    <property name="jobDetail">
        <ref bean="jobtask"/>
    </property>
    <!-- cron表达式 -->
    <property name="cronExpression">
       <!-- <value>0 0 6 1 * ?</value>-->
        <value>0 30 9 * * ?</value>
    </property>
</bean>
<!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序  -->
<bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
        <list>
            <ref bean="doTime"/>
        </list>
    </property>
</bean>

https://www.chenlilin.cn/

Quartz 是一个开源的作业调度框架,可以用于在特定时间触发特定作业的执行。在 Java 项目使用 Quartz,一般需要以下步骤: 1. 引入 Quartz 依赖 在 Maven 项目,可以在 pom.xml 文件添加以下依赖: ``` <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.3.2</version> </dependency> ``` 2. 创建作业类 创建一个实现 Job 接口的作业类,该类需要实现 execute 方法,用于定义作业执行的逻辑。 ``` public class MyJob implements Job { public void execute(JobExecutionContext context) throws JobExecutionException { // 定义作业执行的逻辑 } } ``` 3. 创建触发器 创建一个触发器,用于指定作业的执行时间。可以使用 Cron 表达式或者简单的时间间隔来定义触发器。 ``` Trigger trigger = TriggerBuilder.newTrigger() .withIdentity("myTrigger", "group1") .withSchedule( SimpleScheduleBuilder.simpleSchedule() .withIntervalInSeconds(10) .repeatForever()) .build(); ``` 4. 创建调度器 创建一个调度器,并将作业和触发器添加到调度器。 ``` Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); JobDetail jobDetail = JobBuilder.newJob(MyJob.class) .withIdentity("myJob", "group1") .build(); scheduler.scheduleJob(jobDetail, trigger); ``` 5. 启动调度器 启动调度器,开始执行作业。 ``` scheduler.start(); ``` 以上是使用 QuartzJava 项目实现作业调度的基本步骤。需要注意的是,Quartz 还提供了很多高级特性,比如支持集群部署、支持持久化作业等。如果需要使用这些特性,可以参考 Quartz 的官方文档进行配置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值