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文件:
3.在spring-servlet.xml中引入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 listValue) {
if (map.containsKey(customerCode)) {
List
list.add(listValue);
} else {
List
list.add(listValue);
map.put(customerCode, list);
}
}
}
5.最后启动服务之前,准备好数据库,在quartz1.8.6文档中找到相应数据库的sql,如tables_oracle.sql,创建好相应的表即可。