【问题标题】:cannot initialize Quartz Scheduler in Spring无法在 Spring 中初始化 Quartz Scheduler
【发布时间】:2018-07-27 06:24:47
【问题描述】:

我正在尝试在 Spring 3.0.0 中使用 Quartz 1.8.5(我也尝试过 Quartz2.0 - 同样的问题)。 但是,我不想将 Spring 的实用程序包装器用于 Quartz 调度程序、触发器、作业...... 我想从我的应用程序中以编程方式创建所有这些对象,就像在 没有容器的独立 Java 应用程序 - 有很多原因,但主要是因为我有以下要求 动态触发器和 JobDetails...

所以,我在类路径中有我的 quartz.properties,我在其中定义了一个 JobStoreTX:

org.quartz.scheduler.instanceName: NotificationsScheduler
org.quartz.scheduler.instanceId: AUTO
org.quartz.scheduler.skipUpdateCheck: true

org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount: 3
org.quartz.threadPool.threadPriority: 5

org.quartz.jobStore.misfireThreshold: 60000

org.quartz.jobStore.class: org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.useProperties: false
org.quartz.jobStore.dataSource: qzDS
org.quartz.jobStore.tablePrefix: QRTZ_
org.quartz.jobStore.isClustered: false

org.quartz.dataSource.qzDS.driver: com.mysql.jdbc.Driver
org.quartz.dataSource.qzDS.URL: jdbc:mysql://localhost:3306/myschema
org.quartz.dataSource.qzDS.user: myuser
org.quartz.dataSource.qzDS.password: mypwd
org.quartz.dataSource.qzDS.maxConnections: 5

然后,我有一个简单的 Initializer 类,几乎与 Quartz 发行版的 example1 一样, 它使用了同一示例中未修改的 HelloJob:

public class NotificationInitializer {

public void init(){
try {
    SchedulerFactory schdFact = new StdSchedulerFactory("quartz.properties");
    Scheduler schd = schdFact.getScheduler();
    schd.start();

    JobDetail jd = new JobDetail("hellojob", Scheduler.DEFAULT_GROUP, HelloJob.class);
    Trigger t = TriggerUtils.makeImmediateTrigger(2, 1000);
    t.setStartTime(new Date());

    schd.scheduleJob(jd, t);
    // wait long enough so that the scheduler as an opportunity to run 
    ..........

    schd.shutdown(true);
    } catch (SchedulerException e) {
    .....
    }
}   
}

我还有一个简单的单元测试调用这个类:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/applicationContext.xml")
public class NotificationInitializerTest {
    private final static Logger logger = LoggerFactory
        .getLogger(NotificationInitializerTest.class);

    @BeforeClass
    public static void setupClass() {
        PropertyConfigurator.configure(Loader
        .getResource("spring/log4j.properties"));
        PropertyConfigurator.configure(Loader
            .getResource("spring/quartz.properties"));
    }


    @Test
    public void testInit() {
        NotificationInitializer ni = new NotificationInitializer();
        ni.init();
        logger.info("NotificationInitializer.init() finished OK");
    }

}

当我运行这个单元测试时,我遇到了以下错误 - 请参阅下面的完整堆栈跟踪:

引起:org.springframework.beans.factory.BeanCreationException: 创建名为 'org.springframework.jdbc.datasource.init.DataSour ceInitializer#0' 的 bean 时出错: 调用 init 方法失败;嵌套异常是 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 查询为空

知道出了什么问题吗?

另外,我现在正在设置 JobStoreTX - 只是为了确保我可以让它工作, 但最终我想使用 JobStoreCMT 来插入 Spring 的事务管理。 我找不到任何关于如何做到这一点的好的文档/示例,特别是如果我不想使用 Spring 的调度程序包装器和硬编码所有触发器定义...

谢谢!

java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:308)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDeendencies (DependencyInjectionTestExecutionListener.java:109 )
    at org.springframework.test.context.support.Dependenc yInjectionTestExecutionListener.prepareTestInstanc e(DependencyInjectionTestExecutionListener.java:75 )
    at org.springframework.test.context.TestContextManage r.prepareTestInstance(TestContextManager.java:333)
    at org.springframework.test.context.junit4.SpringJUni t4ClassRunner.createTest(SpringJUnit4ClassRunner.j ava:220)
    at 
    .....

Caused by: org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'org.springframework.jdbc.datasource.init.DataSour ceInitializer#0': Invocation of init method failed; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorEx ception: Query was empty
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.initializeBean(Abstract AutowireCapableBeanFactory.java:1401)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:512)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:450)
    at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 90)
    at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:222)
    at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:287 )
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:189)
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.preInstantiateSingletons(Defaul tListableBeanFactory.java:557)
    at org.springframework.context.support.AbstractApplic ationContext.finishBeanFactoryInitialization(Abstr actApplicationContext.java:842)
    at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:416)
    at org.springframework.test.context.support.AbstractG enericContextLoader.loadContext(AbstractGenericCon textLoader.java:84)
    at org.springframework.test.context.support.AbstractG enericContextLoader.loadContext(AbstractGenericCon textLoader.java:1)
    at org.springframework.test.context.TestContext.loadA pplicationContext(TestContext.java:280)
    at org.springframework.test.context.TestContext.getAp plicationContext(TestContext.java:304)
    ... 25 more

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorEx ception: Query was empty
    at sun.reflect.NativeConstructorAccessorImpl.newInsta nce0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInsta nce(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newI nstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:40 6)
    at com.mysql.jdbc.Util.getInstance(Util.java:381)
    at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:1030)
    at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:956)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.ja va:3491)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.ja va:3423)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:19 36)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java :2060)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionIm pl.java:2536)
    at com.mysql.jdbc.StatementImpl.executeUpdate(Stateme ntImpl.java:1564)
    at com.mysql.jdbc.StatementImpl.executeUpdate(Stateme ntImpl.java:1485)
    at org.apache.commons.dbcp.DelegatingStatement.execut eUpdate(DelegatingStatement.java:228)
    at org.springframework.jdbc.datasource.init.ResourceD atabasePopulator.executeSqlScript(ResourceDatabase Populator.java:157)
    at org.springframework.jdbc.datasource.init.ResourceD atabasePopulator.populate(ResourceDatabasePopulato r.java:110)
    at org.springframework.jdbc.datasource.init.DataSourc eInitializer.afterPropertiesSet(DataSourceInitiali zer.java:69)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.invokeInitMethods(Abstr actAutowireCapableBeanFactory.java:1460)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.initializeBean(Abstract AutowireCapableBeanFactory.java:1398)
    ...

【问题讨论】:

  • 你能发布你的spring bean配置吗?您是否在 spring 应用程序上下文加载时使用脚本初始化石英的数据库?
  • 是的,就是这个原因!在下面查看我更详细的答案...谢谢!

标签: spring configuration quartz-scheduler


【解决方案1】:

我最近也遇到了这个问题,(我在 Mac OS X 下使用 Java7),以下对我有用:

首先,测试java.net.InetAddress.getLocalHost() 方法是否有效,是否抛出异常:

Caused by: java.net.UnknownHostException: leo-mbp: nodename nor servname provided, or not known
  at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
  at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:901)
  at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1293)
  at java.net.InetAddress.getLocalHost(InetAddress.java:1469)

运行以下解决方法:

scutil --set HostName "localhost" 

希望对你有帮助。

【讨论】:

  • 哇!不明白那是什么,似乎无关,但它对我有用。谢谢!
【解决方案2】:

@blob :你的目标是正确的。在花了几个小时试图弄清楚为什么 NotificationInitializer 没有被初始化(如错误消息所建议的那样......)之后,我终于尝试从我的 Spring 配置中删除 Quartz DB 初始化脚本 - 一切都像一个魅力!

<jdbc:initialize-database data-source="dataSource"
    enabled="${database.initschema}">
    <jdbc:script location="${database.schemaLoc}" />
    <jdbc:script location="${database.seedLoc}" />
    <!-- <jdbc:script location="classpath:db/tables_mysql_innodb_1.8.5.sql" /> -->
</jdbc:initialize-database>

我只是希望 Spring 错误消息更具体一点... :)

现在,我仍然不明白为什么我不能以这种方式为 Quartz 创建数据库表 - 直接在数据库上运行时,相同的脚本工作得很好。也许在 Spring 中有一些奇怪的启动操作序列?

谢谢!

【讨论】:

    【解决方案3】:

    只是猜测:检查是否需要指定连接验证查询。就像是 validationQuery="select 1"

    【讨论】:

      【解决方案4】:

      在 quartz.properties 文件中将“org.quartz.scheduler.instanceId”从 AUTO 更改为 10 对我有用。

      【讨论】:

        猜你喜欢
        • 2015-09-27
        • 1970-01-01
        • 2017-02-02
        • 1970-01-01
        • 1970-01-01
        • 2021-12-04
        • 1970-01-01
        • 2016-06-09
        • 2021-09-11
        相关资源
        最近更新 更多