废话不多说上代码:
定义定是加载类
<bean id="DiscountTask"
class="com.paic.common.dmz.DiscountTask"></bean>
<!-- 任务调度 -->
<bean id="DiscountTaskJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject">
<ref bean="DiscountTask"></ref>
</property>
<!-- 调用类中的方法 -->
<property name="targetMethod">
<value>startTask</value>
</property>
</bean>
<!-- 调度计划每隔1分钟执行一次 -->
<bean id="loginTimesTaskTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail">
<ref bean="DiscountTaskJob"/>
</property>
<!-- 首次执行 延迟一分钟-->
<property name="startDelay" value="6000"/>
<property name="repeatInterval" value="6000"/>
</bean>
<!-- 定时计划 每天7点执行-->
<!--
<bean id="loginTimesTaskTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="DiscountTaskJob" />
</property>
<property name="cronExpression" value="0 0 7 ? * *"/>
</bean>
-->
<bean id="loginTimesTaskJobSchedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref local="loginTimesTaskTrigger"/>
</list>
</property>
</bean>
DiscountTask 类:
public class DiscountTask {
public static boolean RUNNING = false;// 任务当前运行状态
private static Object OBJECT = new Object(); // 资源锁
public DiscountTask() {
}
public void startTask() {
// 如果已存在任务运行,则直接结束当前任务
if (RUNNING){
return;
}
// 锁定资源锁 避免多任务同时运行
synchronized (OBJECT){
// 经运行状态改为正在运行状态
RUNNING = true;
try{
//这里谢处理逻辑
System.out.println("22222");
} catch (Exception e){
LOGGER.error(e);
} finally{
// 将运行状态该为结束非运行状态
RUNNING = false;
}
}
}
}