spring jobdemo
时间: 2023-07-12 12:45:01 浏览: 92
好的,下面是一个简单的Spring框架中使用Quartz进行Job调度的示例代码:
首先,需要在Spring配置文件中定义Quartz的SchedulerFactoryBean和JobDetailBean:
```xml
<bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!-- 配置JobFactory -->
<property name="jobFactory">
<bean class="org.springframework.scheduling.quartz.SpringBeanJobFactory" />
</property>
</bean>
<bean id="jobDetailBean" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="com.example.MyJob" />
<property name="durability" value="true" />
<property name="requestsRecovery" value="true" />
<property name="jobDataMap">
<map>
<entry key="myParam" value="myValue" />
</map>
</property>
</bean>
```
然后,定义Trigger并将其与Job关联起来:
```xml
<bean id="simpleTrigger" class="org.quartz.SimpleTrigger">
<property name="name" value="myTrigger" />
<property name="startTime" value="2022-01-01T00:00:00Z" />
<property name="repeatInterval" value="60000" />
<property name="repeatCount" value="10" />
</bean>
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="jobDetailBean" />
<property name="cronExpression" value="0 0/5 * * * ?" />
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="simpleTrigger" />
<ref bean="cronTrigger" />
</list>
</property>
</bean>
```
最后,编写Job类:
```java
public class MyJob implements Job {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
String myParam = jobDataMap.getString("myParam");
System.out.println("执行MyJob,参数myParam的值为:" + myParam);
}
}
```
这样就完成了一个简单的Spring框架中使用Quartz进行Job调度的示例。
阅读全文