Quartz DataSource配置

Spring使用Quartz配置数据源的方法
一、使用quartz.properties文件配置数据源
(1)在quartz.properties文件中需做如下配置:
org.quartz.scheduler.instanceName = scheduler
org.quartz.scheduler.instanceId = AUTO
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
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.MSSQLDelegate
org.quartz.jobStore.useProperties = false
org.quartz.jobStore.dataSource = myDS
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.isClustered = true
org.quartz.jobStore.clusterCheckinInterval = 20000


org.quartz.dataSource.myDS.driver = com.microsoft.sqlserver.jdbc.SQLServerDriver
org.quartz.dataSource.myDS.URL = jdbc:sqlserver://数据库所在的服务器的IP(比如:192.168.1.1):端口号(比如:1433);databaseName=数据库名称(比如:testDB)
org.quartz.dataSource.myDS.user = 用户名(比如:sa) 
org.quartz.dataSource.myDS.password = 密码(比如:123456)
org.quartz.dataSource.myDS.maxConnections = 5
org.quartz.dataSource.myDS.validationQuery=select count(0) from


(2)在spring的配置文件中加入如下配置:
<!—管理类:配置Scheduler信息,与spring容器的生命周期建立联系等功能 -->
<bean id="scheduler"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean"
lazy-init="false" autowire="no">
<!—读取quartz.properties配置文件 -->
    <property name="configLocation" value="classpath:quartz.properties"></property>
<property name="applicationContextSchedulerContextKey" value="applicationContextKey" />
<property name="autoStartup" value="true" />
<property name="startupDelay" value="10" />
</bean>


(3)动态添加Job定时任务时,使用SchedulerFactory这个类的getScheduler()方法。


二、在Spring文件中通过dataSource指定一个Spring管理的数据源,如果指定了该属性,即使在quartz.properties文件中设定了读取DB的数据配置,也会被dataSource覆盖。
(1)在quartz.properties文件中需要做如下配置:
org.quartz.scheduler.instanceName = scheduler
org.quartz.scheduler.instanceId = AUTO
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
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.MSSQLDelegate
org.quartz.jobStore.useProperties = false
org.quartz.jobStore.dataSource = myDS
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.isClustered = true
org.quartz.jobStore.clusterCheckinInterval = 20000


(2)在spring配置文件中,需要做如下设定:
<!-- DataSource設定  -->
<bean id="JNDI_TEST" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/test"/>
</bean>


<!—管理类:配置Scheduler信息,与spring容器的生命周期建立联系等功能 -->
<bean id="scheduler"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean"
lazy-init="false" autowire="no">
<!—读取quartz.properties配置文件 -->
    <property name="configLocation" value="classpath:quartz.properties"></property>
   <!—读取spring指定的数据源 -->
    <property name=”dataSource” ref=”JNDI_TEST”></property>
<property name="applicationContextSchedulerContextKey" value="applicationContextKey" />
<property name="autoStartup" value="true" />
<property name="startupDelay" value="10" />
</bean>


(3)动态添加Job定时任务
1)注入SchedulerFactoryBean这个类,如下所示:
@Autowired
private   SchedulerFactoryBean   schedulerFactoryBean;
2)使用SchedulerFactoryBean类的getScheduler()方法。
说明:如果要使用这种方法就必须要使用SchedulerFactoryBean这个类,不然读取spring指定的数据源是不会起作用的。




三、提供动态添加Job定时任务的参考代码
@Autowired
private   SchedulerFactoryBean   schedulerFactoryBean;


public void newTrigger(Testbean params,HttpServletRequest request) throws ClassNotFoundException,SchedulerException {
String strFunName = params.getFunName();
String strHour = params.getHour();
String strMinute = params.getMinute();
String strRemark = params.getRemark();
String strGroup = params.getGroup();
String strWeeks = params.getWeeks();
String strEveryDay = params.getEveryDay();
Scheduler sched = null;
/**------DB连接读取quartz.properties配置文件start-------*/
//SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();
//sched = schedFact.getScheduler();
/**------DB连接读取quartz.properties配置文件end-------*/
//DB连接使用spring指定数据源
sched = schedulerFactoryBean.getScheduler();
JobDetail oldJobDetail = null;
//创建排程时使用(用来区分相同报表不可设置同一时间的排程)
String funNameSchedule = "";
//周的判断还有问题,后续再修改
if(strGroup.equalsIgnoreCase("Weekly")){
//只選一周中的任意一天
if(strWeeks.length() == 3){
funNameSchedule=strFunName+"_"+strHour+strMinute+"_"+strWeeks;
}else{  //选择一周中的任意两天或是两天以上
String[] param = strWeeks.split(",");
for (String selectWeek : param) {
if(StringUtils.isNotBlank(selectWeek)){
funNameSchedule = strFunName+"_"+strHour+strMinute
+"_"+selectWeek;
//单个判断选择的同报表同时间的排程是否已存在
oldJobDetail = sched.getJobDetail(JobKey.jobKey
(funNameSchedule, strGroup));
if(oldJobDetail != null){
//相同报表相同时间的排程已存在
//提示排程已存在
}
}
}
funNameSchedule = strFunName+"_"+strHour+strMinute+"_"
+strWeeks.replace(',', '_');
}
}else if(strGroup.equalsIgnoreCase("Permonth")){
funNameSchedule = strFunName+"_"+strHour+strMinute+"_"+strEveryDay;
}else{
funNameSchedule = strFunName+"_"+strHour+strMinute;
}
//创建触发器时使用
String strFunNameRandom = strFunName + "_" + getRandomCode();
// 查看同样的排程是否已存在(相同报表不可以设定相同时间)
oldJobDetail = sched.getJobDetail(JobKey.jobKey(funNameSchedule, strGroup));
if (oldJobDetail != null) {
//提示排程已存在
} else {
// 创建一个排程作业
@SuppressWarnings("unchecked")
JobDetail jobDetail = JobBuilder
.newJob((Class<? extends Job>) Class
.forName("com.test.schedule."
+ strFunName.toLowerCase() + "."+ strFunName))
.withIdentity(funNameSchedule, strGroup)
.withDescription(strRemark).build();
String strTrigger = strFunNameRandom + "Trigger";
// 创建一个触发器控制作业的排程
CronScheduleBuilder cronScheduleBuilder = null;
String strCronExpression = "";
if (strGroup.equalsIgnoreCase("Everyday")) {// 每天
strCronExpression = "0 " + Integer.valueOf(strMinute) + " "
+ Integer.valueOf(strHour) + " * * ?";
} else if (strGroup.equalsIgnoreCase("Weekly")) {// 每周
strCronExpression = "0 " + Integer.valueOf(strMinute) + " "
+ Integer.valueOf(strHour) + " ? * " + strWeeks;
} else if (strGroup.equalsIgnoreCase("Permonth")) {// 每月
strCronExpression = "0 " + Integer.valueOf(strMinute) + " "
+ Integer.valueOf(strHour) + " " + strEveryDay + " * ?";
} else if (strGroup.equalsIgnoreCase("Cycle")) {// 周期
strCronExpression = "0"
+ (!strMinute.equals("00") ? " */"
+ Integer.valueOf(strMinute) : " *")
+ (!strHour.equals("00") ? " */"
+ Integer.valueOf(strHour) : " *") + " * * ?";
}
cronScheduleBuilder = CronScheduleBuilder
.cronSchedule(strCronExpression);
CronTrigger trigger = (CronTrigger) TriggerBuilder.newTrigger()
.withIdentity(strTrigger, strGroup)
.withDescription(strRemark)
.withSchedule(cronScheduleBuilder).build();
// 排程作业
sched.scheduleJob(jobDetail, trigger);
sched.start();
}
}





org.quartz.SchedulerConfigException: Failure occured during job recovery. [See nested exception: org.quartz.JobPersistenceException: Failed to obtain DB connection from data source 'myDS': org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Access denied for user 'root'@'localhost' (using password: YES)) [See nested exception: org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Access denied for user 'root'@'localhost' (using password: YES))]] at org.quartz.impl.jdbcjobstore.JobStoreSupport.schedulerStarted(JobStoreSupport.java:692) at org.quartz.core.QuartzScheduler.start(QuartzScheduler.java:567) at org.quartz.impl.StdScheduler.start(StdScheduler.java:142) at servlet.remind.RemindStartListener.contextInitialized(RemindStartListener.java:28) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5077) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5591) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1574) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1564) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) Caused by: org.quartz.JobPersistenceException: Failed to obtain DB connection from data source 'myDS': org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Access denied for user 'root'@'localhost' (using password: YES)) [See nested exception: org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Access denied for user 'root'@'localhost' (using password: YES))] at org.quartz.impl.jdbcjobstore.JobStoreSupport.getConnection(JobStoreSupport.java:778) at org.quartz.impl.jdbcjobstore.JobStoreTX.getNonManagedTXConnection(JobStoreTX.java:71) at org.quartz.impl.jdbcjobstore.JobStoreSupport.executeInNonManagedTXLock(JobStoreSupport.java:3784) at org.quartz.impl.jdbcjobstore.JobStoreSupport.recoverJobs(JobStoreSupport.java:834) at org.quartz.impl.jdbcjobstore.JobStoreSupport.schedulerStarted(JobStoreSupport.java:690) ... 12 more Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Access denied for user 'root'@'localhost' (using password: YES)) at org.apache.commons.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1549) at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1388) at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044) at tool.remind.MyPoolingconnectionProvider.getConnection(MyPoolingconnectionProvider.java:63) at org.quartz.utils.DBConnectionManager.getConnection(DBConnectionManager.java:108) at org.quartz.impl.jdbcjobstore.JobStoreSupport.getConnection(JobStoreSupport.java:775) ... 16 more Caused by: java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4187) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4119) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:927) at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1709) at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1252) at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2483) at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2516) at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2301) at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:834) at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:422) at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416) at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:346) at org.apache.commons.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38) at org.apache.commons.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:582) at org.apache.commons.dbcp.BasicDataSource.validateConnectionFactory(BasicDataSource.java:1556) at org.apache.commons.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1545) ... 21 more
scheduleManger执行成功,job以及trigger的相关信息也都有写在数据库中,但是根本就没有执行job,希望大家可以帮我解惑,谢谢 这是quartz.properties ``` org.quartz.scheduler.instanceName = DefaultQuartzScheduler org.quartz.scheduler.rmi.export: false org.quartz.scheduler.rmi.proxy: false org.quartz.scheduler.wrapJobExecutionInUserTransaction: false org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool org.quartz.threadPool.threadCount = 10 org.quartz.threadPool.threadPriority: 5 org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate org.quartz.jobStore.tablePrefix = QRTZ_ org.quartz.jobStore.dataSource = myDS org.quartz.dataSource.myDS.driver = com.mysql.jdbc.Driver org.quartz.dataSource.myDS.URL = jdbc:mysql://localhost:3306/quartz?characterEncoding=utf-8&useSSL=false org.quartz.dataSource.myDS.user = root org.quartz.dataSource.myDS.password = 1234 org.quartz.dataSource.myDS.maxConnections = 30 ``` 这是 QuartzManager.java 文件 public class QuartzManager { private static SchedulerFactory gSchedulerFactory = new StdSchedulerFactory(); /** * 增加任务 * @param jobClass 任务实现类 * @param jobName 任务名称 * @param interval 间隔时间 * @param data 数据 */ public static void addJob(Class<? extends Job> jobClass, String jobName, int interval, Map<String, Object> data) { try { Scheduler sched = gSchedulerFactory.getScheduler(); JobDetail jobDetail = JobBuilder.newJob(jobClass) .withIdentity(jobName, "test3")//任务名称和组构成任务key .build(); jobDetail.getJobDataMap().putAll(data); // 触发器 SimpleTrigger trigger = TriggerBuilder.newTrigger() .withIdentity(jobName, "ttest3")//触发器key .startAt(DateBuilder.futureDate(1, IntervalUnit.SECOND)) .withSchedule(SimpleScheduleBuilder.simpleSchedule() .withIntervalInSeconds(interval) .repeatForever()) .build(); sched.scheduleJob(jobDetail, trigger); // 启动 if (!sched.isShutdown()) { sched.start(); } } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) throws InterruptedException { Map<String, Object> map = new HashMap<String, Object>(); map.put("id", "1"); String jobname = "aaa"; addJob(TestJob.class, "aaa", 1, map); try { Thread.sleep(10000); System.out.println("main end----------"); System.exit(1); } catch (InterruptedException e) { e.printStackTrace(); } } ``` ``` 这是TestJob.Java文件 import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class TestJob implements Job { public TestJob(){ } public void execute(JobExecutionContext jobexecutioncontext) throws JobExecutionException { String id = jobexecutioncontext.getJobDetail().getJobDataMap().getString("id"); System.out.println("threadId: " + Thread.currentThread().getId() + ", id: " + id); try { System.out.println("=====================>睡眠开始时间:"+System.currentTimeMillis()); Thread.sleep(10000); System.out.println("=====================>睡眠结束时间:"+System.currentTimeMillis()); } catch (InterruptedException e) { e.printStackTrace(); } } }
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页