首页 文章

如何在Quartz中使用SQLite?

提问于
浏览
0

我正在尝试在应用程序中使用带有SQLite的quartz . 当我阅读文档here时,我注意到他们没有在可用的数据库中提到SQLite . 他们说:

JDBCJobStore几乎适用于任何数据库,它已广泛用于Oracle,PostgreSQL,MySQL,MS SQLServer,HSQLDB和DB2 . 要使用JDBCJobStore,必须首先为Quartz创建一组数据库表以供使用 . 您可以在Quartz发行版的“docs / dbTables”目录中找到表创建SQL脚本 .

所以,从这个问题: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)

2 回答

  • 2

    问题似乎是sqlite jdbc驱动程序does not support the method ResultSet.getBlob() .

    但是quartz使用此方法来检索分配给Job的JobDataMap .

    如果您仍然希望使用带有sqlite的quartz,您可以扩展StdJDBCDelegate并检索/设置blob,如建议的in this answer .

    乍一看,您似乎只能覆盖这些方法

    • StdJDBCDelegate.getObjectFromBlob()和

    • StdJDBCDelegate.getJobDataFromBlob()

    因为我不确定以后会遇到更多问题(例如你可以看到sqlite jdbcdriver有更多unsupported ResultSet methods)我宁愿建议使用一个开箱即用的数据库 .

  • 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
    

相关问题