1.quartz.properties文件:
#============================================================================
# Configure Main Scheduler Properties
#============================================================================
org.quartz.scheduler.instanceName = mapScheduler
#最多只能有3个线程并发
org.quartz.threadPool.threadCount = 3
org.quartz.scheduler.instanceId = AUTO
#============================================================================
# Configure JobStore 配置数据存储的方式
#============================================================================
#所有的Quartz数据,例如Job和Trigger的细节信息被存储在内存(数据库)中。
#org.quartz.jobStore.class = org.quartz.simpl.JDBCJobStore
#org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
#配置事务。JobStoreTX来让Quartz帮你管理事务(这是最普遍的选择);使用JobStoreCMT,Quartz就会让应用服务器容器来管理事务
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
#如果数据库没有其他指定的代理,那么就试用代理StdJDBCDelegate。
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.isClustered = true
2.applicationContext-quartz.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="puchNoticeJob" class="com.sosgps.customized.hbwysh.quartz.PushNoticeJob"></bean>
<bean id="puchNoticeTask" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass">
<value>com.sosgps.customized.hbwysh.quartz.MyDetailQuartzJobBean</value>
</property>
<property name="jobDataAsMap">
<map>
<entry key="targetObject" value="puchNoticeJob" />
<entry key="targetMethod" value="execute" />
</map>
</property>
</bean>
<!-- ======================== 调度触发器 ======================== -->
<bean id="puchNoticeCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="puchNoticeTask" />
</property>
<property name="cronExpression">
<value>0 0 9 * * ?</value>
</property>
</bean>
<!-- ======================== 调度工厂 ======================== -->
<bean id="mapScheduler" lazy-init="false" autowire="no"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="configLocation" value="classpath:quartz.properties" />
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="triggers">
<list>
<ref bean="puchNoticeCronTrigger" />
</list>
</property>
<property name="applicationContextSchedulerContextKey" value="applicationContext" />
</bean>
</beans>
3.在spring-servlet.xml中引入applicationContext-quartz.xml
<import resource="applicationContext-quartz.xml" />
4.配置中用到的几个类:
MyDetailQuartzJobBean
package com.sosgps.customized.hbwysh.quartz;
import java.lang.reflect.Method;
import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class MyDetailQuartzJobBean extends QuartzJobBean {
private static final Logger LOGGER = Logger.getLogger(MyDetailQuartzJobBean.class);
private String targetObject;
private String targetMethod;
private ApplicationContext applicationContext;
protected void executeInternal(JobExecutionContext context)
throws JobExecutionException {
try {
LOGGER.info("execute [" + targetObject + "] at once>>>>>>");
Object otargetObject = applicationContext.getBean(targetObject);
Method m = null;
try {
m = otargetObject.getClass().getMethod(targetMethod, new Class[] {});
m.invoke(otargetObject, new Object[] {});
} catch (SecurityException e) {
LOGGER.error(e);
} catch (NoSuchMethodException e) {
LOGGER.error(e);
}
} catch (Exception e) {
throw new JobExecutionException(e);
}
}
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
public void setTargetObject(String targetObject) {
this.targetObject = targetObject;
}
public void setTargetMethod(String targetMethod) {
this.targetMethod = targetMethod;
}
}
PushNoticeJob
package com.sosgps.customized.hbwysh.quartz;
import java.io.Serializable;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import com.sosgps.dao.customized.wysw.dao.RefCustomerDeviceService;
import com.sosgps.dao.customized.wysw.vo.RefCustomerDeviceVO;
import com.sosgps.dao.domain.Customer;
import com.sosgps.dao.generator.SEQKey;
import com.sosgps.dao.generator.SeqGenerator;
import com.sosgps.dao.mdm.service.CustomerService;
import com.sosgps.dao.mdm.service.RefEmployeeCustomerService;
import com.sosgps.dao.mdm.service.RefEmployeeDeviceService;
import com.sosgps.push.service.PushService;
import com.sosgps.push.vo.MessageObject;
import com.sosgps.web.util.ReqSession;
public class PushNoticeJob implements Serializable {
private static final long serialVersionUID = -3364631834467951396L;
private static int counter = 0;
private static DateFormat simpleDateFormat = new java.text.SimpleDateFormat("MMdd");
@Autowired
private RefEmployeeDeviceService refEmployeeDeviceService;
@Autowired
private RefEmployeeCustomerService refEmployeeCustomerService;
@Autowired
private RefCustomerDeviceService refCustomerDeviceService;
@Autowired
private SeqGenerator seqGenerator;
@Autowired
@Qualifier("pushService")
private PushService pushService;
public void execute() {
//具体执行方法
}
/**
* 组装map值
*
* @param map
* @param customerCode
* @param listValue
*/
private void generateMapValue(HashMap<String, List<String>> map, String customerCode,
String listValue) {
if (map.containsKey(customerCode)) {
List<String> list = map.get(customerCode);
list.add(listValue);
} else {
List<String> list = new ArrayList<String>();
list.add(listValue);
map.put(customerCode, list);
}
}
}
5.最后启动服务之前,准备好数据库,在quartz1.8.6文档中找到相应数据库的sql,如tables_oracle.sql,创建好相应的表即可。