我试图在应用程序中使用石英和SQLite。当我阅读文档here时,我注意到他们在可用的数据库中没有提到SQLite。他们说:
JDBCJobStore几乎适用于任何数据库,它已被广泛使用 与Oracle,PostgreSQL,MySQL,MS SQLServer,HSQLDB和DB2。使用 JDBCJobStore,您必须首先为其创建一组数据库表 石英使用。您可以在中找到表创建SQL脚本 Quartz发行版的“docs / dbTables”目录。
所以,从这个问题:Which setup script to use for setting up quartz sqlite table?我使用derby脚本作为我的sqlite脚本应用。
问题是当我尝试在先前插入的作业中安排触发器时。这是我的代码的一部分:
// and start it off
scheduler.start();
Map<String, String> map = new HashMap<>();
map.put("key", "value");
JobDataMap jdm = new JobDataMap(map);
JobKey key = new JobKey("job1", "key1");
if(!scheduler.checkExists(key)){
JobDetail job = newJob(HelloJob.class).withIdentity(key).storeDurably().usingJobData(jdm).build();
addJob(scheduler, job);
// Trigger the job to run now, and then repeat every 40 seconds
Trigger trigger = newTrigger()
.withIdentity("trigger1", "group1")
.startNow()
.forJob(job)
.withSchedule(simpleSchedule()
.withIntervalInSeconds(40)
.repeatForever())
.build();
// Tell quartz to schedule the job using our trigger
scheduler.scheduleJob(trigger); // here is where I get an error
}
Thread.sleep(60000);
scheduler.shutdown();
我的quartz.properties是这样的:
org.quartz.scheduler.instanceName = MyScheduler
org.quartz.threadPool.threadCount = 5
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.dataSource.SQLiteDB.driver = org.sqlite.JDBC
org.quartz.dataSource.SQLiteDB.URL = jdbc:sqlite:bota.db
org.quartz.dataSource.SQLiteDB.maxConnections = 30
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.dataSource = SQLiteDB
我使用的是sqlite v-3.8.11.2和quartz v-2.2.2。这是我在日志中得到的:
org.quartz.JobPersistenceException: Couldn't store trigger 'group1.trigger1' for 'key1.job1' job:Couldn't retrieve job: not implemented by SQLite JDBC driver [See nested exception: org.quartz.JobPersistenceException: Couldn't retrieve job: not implemented by SQLite JDBC driver [See nested exception: java.sql.SQLException: not implemented by SQLite JDBC driver]]
at org.quartz.impl.jdbcjobstore.JobStoreSupport.storeTrigger(JobStoreSupport.java:1223)
at org.quartz.impl.jdbcjobstore.JobStoreSupport$4.executeVoid(JobStoreSupport.java:1159)
at org.quartz.impl.jdbcjobstore.JobStoreSupport$VoidTransactionCallback.execute(JobStoreSupport.java:3715)
at org.quartz.impl.jdbcjobstore.JobStoreSupport$VoidTransactionCallback.execute(JobStoreSupport.java:3713)
at org.quartz.impl.jdbcjobstore.JobStoreSupport.executeInNonManagedTXLock(JobStoreSupport.java:3799)
at org.quartz.impl.jdbcjobstore.JobStoreTX.executeInLock(JobStoreTX.java:93)
at org.quartz.impl.jdbcjobstore.JobStoreSupport.storeTrigger(JobStoreSupport.java:1155)
at org.quartz.core.QuartzScheduler.scheduleJob(QuartzScheduler.java:932)
at org.quartz.impl.StdScheduler.scheduleJob(StdScheduler.java:258)
at javaapplication2.JavaApplication2.main(JavaApplication2.java:174)
Caused by: org.quartz.JobPersistenceException: Couldn't retrieve job: not implemented by SQLite JDBC driver [See nested exception: java.sql.SQLException: not implemented by SQLite JDBC driver]
at org.quartz.impl.jdbcjobstore.JobStoreSupport.retrieveJob(JobStoreSupport.java:1396)
at org.quartz.impl.jdbcjobstore.JobStoreSupport.storeTrigger(JobStoreSupport.java:1205)
... 9 more
Caused by: java.sql.SQLException: not implemented by SQLite JDBC driver
at org.sqlite.jdbc4.JDBC4ResultSet.unused(JDBC4ResultSet.java:320)
at org.sqlite.jdbc4.JDBC4ResultSet.getBlob(JDBC4ResultSet.java:345)
at com.mchange.v2.c3p0.impl.NewProxyResultSet.getBlob(NewProxyResultSet.java:285)
at org.quartz.impl.jdbcjobstore.StdJDBCDelegate.getObjectFromBlob(StdJDBCDelegate.java:3190)
at org.quartz.impl.jdbcjobstore.StdJDBCDelegate.selectJobDetail(StdJDBCDelegate.java:860)
at org.quartz.impl.jdbcjobstore.JobStoreSupport.retrieveJob(JobStoreSupport.java:1385)
... 10 more
BUILD STOPPED (total time: 11 seconds)
答案 0 :(得分:2)
问题似乎是sqlite jdbc驱动程序does not support the method ResultSet.getBlob()。
但是quartz使用此方法来检索分配给作业的JobDataMap。
如果您仍想使用带有sqlite的quartz,您可以扩展StdJDBCDelegate并检索/设置blob,如建议的in this answer。
乍一看,您似乎只能覆盖方法
因为我不确定以后会遇到更多问题(例如你可以看到sqlite jdbcdriver有更多unsupported ResultSet methods)我宁愿建议使用一个无法使用的数据库
答案 1 :(得分:0)
根据mam10eks的建议,以下代码对我有用。
package com.example.quartz.sqlite;
import org.quartz.impl.jdbcjobstore.StdJDBCDelegate;
import java.io.ByteArrayInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
public class CustomJDBCDelegate extends StdJDBCDelegate {
@Override
protected Object getObjectFromBlob(ResultSet rs, String colName) throws ClassNotFoundException, IOException, SQLException {
byte[] bytes = rs.getBytes(colName);
Object map = null;
ByteArrayInputStream bais = null;
ObjectInputStream ois = null;
try {
bais = new ByteArrayInputStream(bytes);
ois = new ObjectInputStream(bais);
map = ois.readObject();
} catch (EOFException ex1) {
bais.close();
} catch (IOException e) {
// Error in de-serialization
e.printStackTrace();
}
return map;
}
@Override
protected Object getJobDataFromBlob(ResultSet rs, String colName) throws ClassNotFoundException, IOException, SQLException {
return getObjectFromBlob(rs, colName);
}
}
现在将以下内容放入quartz.properties
org.quartz.jobStore.driverDelegateClass=com.example.quartz.sqlite.CustomJDBCDelegate