定时器在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>
后续补充!!!