开发者

cannot initialize Quartz Scheduler in Spring

I'm trying to use Quartz 1.8.5 (and I also tried Quartz2.0 - same issue) with Spring 3.0.0. However, I do not want to use Spring's utility wrappers for Quartz schedulers, triggers, jobs... I would like to create all these objects programmatically from my application, just like in a standalone Java app with no container - for many reasons, but mainly because i have requirements for dynamic triggers and JobDetails...

So, I have my quartz.properties on the classpath, where I define a 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

Then, I have a simple Initializer class, taken almost as is from example1 of Quartz distribution, which is using an un-modified HelloJob from the same example:

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) {
    .....
    }
}   
}

And I also have a simple unit test that calls this class:

@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");
    }

}

And when I run this unit test I'm getting the following errors - see the full stack trace below:

Caused by: org.springframework.beans.factory.BeanCreationException: 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.MySQLSyntaxErrorException: Query was empty

Any idea what went wrong?

Also, I am setting up JobStoreTX for now - just to make sure I can get it working, but eventually I would like to use the JobStoreCMT toplug intoSpring's transaction management. I could not find any good documentation/examples on how to do that, especially if I don't wa开发者_StackOverflownt to use Spring's wrappers for schedulers and hardcode all trigger definitions...

Thanks!

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)
    ...


I recently came across this issue too, (I was using Java7 under Mac OS X), the following worked for me:

first of all, test if the method java.net.InetAddress.getLocalHost() works, if it throws exception like:

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)

run the following to workaround it:

scutil --set HostName "localhost" 

Hope it's helpful to you.


@blob : you were right on target. After spending hours trying to figure out why the NotificationInitializer was not initialized (as the error message suggested....) I finally tried to remove the Quartz DB initialization script from my Spring config - and everything worked like a charm!

<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>

I just wish Spring error messages were a bit more specific... :)

Now, I still don't understand why I cannot create the DB tables for Quartz this way - the same scripts works just fine when run directly on the DB. Maybe some weird sequence of startup actions in Spring?

thanks!


Only a guess: Check if you need to specify a connection validation query. Something like validationQuery="select 1"


Changing "org.quartz.scheduler.instanceId" from AUTO to 10 in quartz.properties file worked for me.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜