spring的基础文件配置
1、applicationContext.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 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 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <!-- This will automatically locate any and all property files you have within your classpath, provided they fall under the META-INF/spring directory. The located property files are parsed and their values can then be used within application context files in the form of ${propertyKey}. --> <context:property-placeholder location="classpath*:META-INF/spring/*.properties" /> <context:spring-configured /> <task:annotation-driven /> <context:component-scan base-package="com.mogenesis"> <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation" /> </context:component-scan> <bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource"> <property name="driverClassName" value="${database.driverClassName}" /> <property name="url" value="${database.url}" /> <property name="username" value="${database.username}" /> <property name="password" value="${database.password}" /> </bean> <bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> <tx:annotation-driven transaction-manager="transactionManager" /> <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory"> <property name="dataSource" ref="dataSource" /> <property name="persistenceXmlLocation" value="classpath*:/META-INF/persistence-spring.xml" /> <property name="loadTimeWeaver"> <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" /> </property> </bean> </beans>
2
编写job
package com.mogenesis.mobileadplatform.scheduling; import java.io.Serializable; import org.quartz.JobDataMap; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.quartz.StatefulJob; import org.springframework.scheduling.quartz.QuartzJobBean; /** * @author :xiaofancn * @version :2011-12-22 上午11:00:00 * */ public class ExampleJob extends QuartzJobBean implements StatefulJob, Serializable { private static final long serialVersionUID = 1L; @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { JobDataMap jobDataMap = context.getJobDetail().getJobDataMap(); System.out.println(ExampleJob.class); System.out.println("记忆时间" + jobDataMap.get("PreviousFireTime")); System.out.println("================================================"); // 本次计划任务的开始结束时间 System.out.println(context.getTrigger().getPreviousFireTime()); // throw new JobExecutionException(); System.out.println(context.getTrigger().getNextFireTime()); // 上次任务的开始时间和本次任务的结束时间 System.out.println(context.getPreviousFireTime()); System.out.println(context.getNextFireTime()); System.out.println("================================================"); jobDataMap.put("PreviousFireTime", context.getNextFireTime()); } }
3、quartz的属性配置文件
quartz.properties
#default config org.quartz.scheduler.instanceName = DefaultQuartzScheduler org.quartz.scheduler.rmi.export = false org.quartz.scheduler.rmi.proxy = false org.quartz.scheduler.wrapJobExecutionInUserTransaction = false #ThreadPool config org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool org.quartz.threadPool.threadCount = 10 org.quartz.threadPool.threadPriority = 5 org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true org.quartz.jobStore.misfireThreshold = 60000 #jobStore RAM #org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore #jobStore Database org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.PostgreSQLDelegate org.quartz.jobStore.tablePrefix = QRTZ_ org.quartz.jobStore.dataSource = qzDS
4、quartz的配置文件
applicationContext-quartz.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 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 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <bean name="exampleJob" class="org.springframework.scheduling.quartz.JobDetailBean"> <!--requestsRecovery属性为true,则当Quartz服务被中止后,再次启动任务时会尝试恢复执行之前未完成的所有任务 --> <property name="requestsRecovery" value="true" /> <property name="jobClass" value="com.mogenesis.mobileadplatform.scheduling.ExampleJob" /> <property name="jobDataAsMap"> <map> <entry key="timeout" value="5" /> </map> </property> </bean> <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="exampleJob" /> <!-- run every morning at 6 AM --> <property name="cronExpression" value="1/5 * * * * ?" /> </bean> <!-- 另一个事务管理器, Jdbc单数据源事务 --> <bean id="quartzTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- Scheduler --> <!-- <property name="waitForJobsToCompleteOnShutdown" value="true" /> 这个是可选,QuartzScheduler 启动时更新己存在的Job,这样就不用每次修改targetObject后删除qrtz_job_details表对应记录了 <property name="overwriteExistingJobs" value="true" /> <property name="transactionManager" ref="quartzTransactionManager" /> --> <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="autoStartup" value="true" /> <property name="waitForJobsToCompleteOnShutdown" value="true" /> <property name="transactionManager" ref="quartzTransactionManager" /> <property name="exposeSchedulerInRepository" value="true" /> <!-- 这个是必须的,QuartzScheduler 延时启动,应用启动完后 QuartzScheduler 再启动 --> <property name="startupDelay" value="10" /> <property name="applicationContextSchedulerContextKey" value="applicationContext" /> <property name="configLocation" value="classpath:META-INF/quartz/quartz.properties" /> <property name="triggers"> <list> <ref bean="cronTrigger" /> </list> </property> </bean> </beans>
4、使用quartz提供的sql文件,初始化我们的PostgreSQL数据库
F:\quartz-1.8.5\quartz-1.8.5\docs\dbTables