java 定时器配置信息 采用spring-quartz方式

本文介绍了如何在Java中使用Spring-Quartz进行定时任务配置,包括JobDetail、Trigger的设置,以及Cron表达式来定义执行频率。通过示例代码详细解析了配置文件中各个bean的作用,帮助理解定时器的工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


定时器在java用的比较普遍,这里稍作整理下 ,主要是配置中的信息整理

后续会继续补充


<?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:context="http://www.springframework.org/schema/context" 
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">


<bean id="unifyJob" class="cn.huimin100.pay.center.controller.trade.RefundController"></bean>
    <bean id="refundTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="unifyJob"></property>
<property name="targetMethod" value="execute"></property>
<property name="concurrent" value="false"></property>
    </bean>
<bean id="refundTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="refundTask"></property>
<property name="cronExpression" value="0 0/2 * * * ?"></property>
</bean>


<!-- 定时器执行 -->
<bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="refundTrigger"/>
</list>
</property>
</bean>
</beans>


-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

说明:<bean id="unifyJob" class="cn.huimin100.pay.center.controller.trade.RefundController"></bean>  指明定时器存在的位置 id 别名


<bean id="refundTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="unifyJob"></property>  指明目标定时器的目标对象
<property name="targetMethod" value="execute"></property> 指明 定时器的执行方法名称
<property name="concurrent" value="false"></property> 自己设定
    </bean>



<bean id="refundTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="refundTask"></property>   指明定时器的任务配置
<property name="cronExpression" value="0 0/2 * * * ?"></property>  定时器执行的时间间隔
</bean>


<bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="refundTrigger"/>  指定执行定时器
</list>
</property>
</bean>


---------------------------------------------------------------------------


执行类 Controller类中:


@RequestMapping("/execute")
@ResponseBody
public void execute() {
    refundService.execute();
}

----方法:


@Override
public void execute() {
    MDC.put("title", "执行退款");
    logger.info("执行定时任务,查找此时退款的文件 ");
    final File[] filesFinal = FileUtil.searchTimeFile(Config.getRefundNew(), new Date());
    if (filesFinal.length > 0) {
        for (int i = 0; i < filesFinal.length; i++) {
            final int j = i;
            new Thread() {
                public void run() {
                    File file = filesFinal[j];
                    RefundCreateModel model = JsonUtil.jsonToBean(FileUtil.readFile(file), RefundCreateModel.class);
                    logger.info("下单参数: " + model.toString());
                    String channel = JsonUtil.jsonToArray(model.getChannelGroup())[0] + "";
                    logger.info("执行渠道:" + channel + ",执行方法: " + model.getMethodName() + ",文件名称:" + file.getName() + " 开始执行");
                    InvokeUtil.executeInvoke(channel + "." + refund.split(":")[0], model.getMethodName(),
                            new Object[]{model, file.getName().split("_")[0]}, new Class[]{model.getClass(), String.class});
                    logger.info("执行渠道:" + channel + ",执行方法: " + model.getMethodName() + ",文件名称:" + file.getName() + " 执行完毕");
                }
            }.start();
        }
    }
}


-------------------------------------


在web.xml中加载配置文件


<!-- 加载spring配置文件 -->
<context-param>
  <param-name>contextConfigLocation</param-name>
  <!-- 如果是监听多个文件,可用‘,’隔开 -->
  <param-value>classpath:spring-context.xml,classpath:spring-quartz.xml</param-value>
</context-param>


后续补充!!!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值