zoukankan      html  css  js  c++  java
  • Quartz 配置

    配置

    以下配置为Application-Context中引用的配置。

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:task="http://www.springframework.org/schema/task"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    
            http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"
           default-lazy-init="true">
    
        <description>SpringTask配置</description>
    
        <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
    
        <task:annotation-driven/>
    
        <bean id="reminderEmailJob" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
            <property name="jobClass" value="com.sapphire.task.ReminderEmailTask"></property>
            <property name="durability" value="true"/>
        </bean>
    
        <bean id="reminderCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
            <property name="jobDetail" ref="reminderEmailJob"></property>
            <property name="cronExpression" value="0/5 * * * * ?"></property>
        </bean>
    
        <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <property name="triggers">
                <list>
                    <ref bean="reminderCronTrigger"/>
                </list>
            </property>
            <property name="schedulerContextAsMap">
                <map>
                    <!-- spring 管理的service需要放到这里,才能够注入成功 -->
                    <description>schedulerContextAsMap</description>
                    <entry key="userService" value-ref="userService"/>
                </map>
            </property>
        </bean>
    </beans>

    其中的配置包含几个部分,Job,Job中jobClass对应为自己写的task
    该Job还需要配置对应的Trigger,Trigger中的是Cron表达式,
    最后将其Trigger注册到SchedulerFactoryBean之中,这样就会自动执行的。

    注入Service

    quartz使用的不是web容器,所以无法加载到web容器之中的bean,所以需要以别的方式注入,也就是schedulerContextAsMap
    其中可以将Service以map的形式注入,用以生效。
    如上,代码就可以通过

    @Override
    protected void executeInternal(JobExecutionContext context)
          throws JobExecutionException {
       LOGGER.info("Execute reminder email task");
       UserService userService;
       try {
          userService =
                (UserService) context.getScheduler().getContext()
                      .get("userService");
       } catch (SchedulerException e) {
          throw new JobExecutionException(e);
       }
       User user = userService.getUserById(1);
       LOGGER.info("User info : " + user.getUsername());
    }

    来访问了,该类继承于QuartzJobBean

  • 相关阅读:
    矩阵特征值和椭圆长短轴的关系?
    Harris角点检测原理详解
    SIFT特征提取分析
    Sift中尺度空间、高斯金字塔、差分金字塔(DOG金字塔)、图像金字塔
    图像处理与计算机视觉的经典书籍
    霍夫变换
    熔断原理与实现Golang版
    如何利用go-zero在Go中快速实现JWT认证
    如何让服务在流量暴增的情况下保持稳定输出
    企业级RPC框架zRPC
  • 原文地址:https://www.cnblogs.com/qitian1/p/6461600.html
Copyright © 2011-2022 走看看