无法在Spring中初始化Quartz Scheduler

时间:2011-07-07 01:18:28

标签: spring configuration quartz-scheduler

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

所以,我在classpath上有我的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 toplug进入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)
    ...

4 个答案:

答案 0 :(得分:3)

我最近也遇到过这个问题,(我在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" 

希望它对你有所帮助。

答案 1 :(得分: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中有一些奇怪的启动动作序列?

谢谢!

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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