Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Spring/Quartz: Basic tutorial code not working

I am trying to get a basic example of Spring and Quartz to work. Here is the tutorial I am following: http://www.mkyong.com/spring/spring-quartz-scheduler-example/

I have copied everything as exactly as far as I can tell, but I don't see anything in the output window.

spring-quartz.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="runMeTask" class="com.ksc.jobs.RunMeTask" />

    <!-- Spring Quartz -->
    <bean name="runMeJob" class="org.springframework.scheduling.quartz.JobDetailBean">
        <property name="jobClass" value="com.ksc.jobs.RunMeJob" />
        <property name="jobDataAsMap">
            <map>
                <entry key="runMeTask" value-ref="runMeTask" />
            </map>
        </property>

    </bean>

    <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
        <property name="jobDetail" ref="runMeJob" />
        <property name="repeatInterval" value="5000" />
        <property name="startDelay" value="1000" />

    </bean>

    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="jobDetails">
            <list>
                <ref bean="runMeJob" />
            </list>
        </property>

        <property name="triggers">
            <list>
                <ref bean="simpleTrigger" />
            </list>
        </property>
    </bean>

</beans>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <import resource="spring-quartz.xml"/>
</beans>

Project Structure

enter image description here

EDIT

Do I need to manually start the job somehow? I see in that tutorial and in others, they usually start a quartz job inside a main() method. Coming from a .NET background that seems a little odd to me, as I only know main methods to be in desktop apps; not web apps. In ASP.NET we have Global.asax where we can call code to run when the web app starts. Is there some equivalent in Java? Or maybe this isn't even the problem at all? I'm just guessing here. If anyone has any idea about how to solve this or even just gives me a working sample (can be different to the one above even; I'm not fussy)... then I would most appreciate it. I just need a basic working sample that I can then later modify to my requirements.

EDIT 2

Here's the glassfish server output, if it helps:

[NOTE: Removed due to body length of this post being too long. Else I wouldn't be able to post Edit #3 below.]

EDIT 3

Some progress, but still not working. I have added the log4j file as suggested by @shuttsy and here is the new output from glassfish:

Launching GlassFish on Felix platform
INFO: Running GlassFish Version: GlassFish Server Open Source Edition 3.1.2.2 (build 5)
INFO: Registered org.glassfish.ha.store.adapter.cache.ShoalBackingStoreProxy for persistence-type = replicated in BackingStoreFactoryRegistry
INFO: Grizzly Framework 1.9.50 started in: 0ms - bound to [0.0.0.0:3700]
INFO: Grizzly Framework 1.9.50 started in: 31ms - bound to [0.0.0.0:8080]
INFO: Grizzly Framework 1.9.50 started in: 15ms - bound to [0.0.0.0:8181]
INFO: Grizzly Framework 1.9.50 started in: 15ms - bound to [0.0.0.0:4848]
INFO: Grizzly Framework 1.9.50 started in: 0ms - bound to [0.0.0.0:7676]
INFO: The Admin Console is already installed, but not yet loaded.
INFO: GlassFish Server Open Source Edition 3.1.2.2 (5) startup time : Felix (1,750ms), startup services(771ms), total(2,521ms)
INFO: HV000001: Hibernate Validator 4.3.0.Final
INFO: JMX005: JMXStartupService had Started JMXConnector on JMXService URL service:jmx:rmi://Matt-Laptop:8686/jndi/rmi://Matt-Laptop:8686/jmxrmi
INFO: Grizzly Framework 1.9.50 started in: 0ms - bound to [0.0.0.0:8080]
INFO: Grizzly Framework 1.9.50 started in: 0ms - bound to [0.0.0.0:8181]
INFO: Created EjbThreadPoolExecutor with thread-core-pool-size 16 thread-max-pool-size 32 thread-keep-alive-seconds 60 thread-queue-capacity 2147483647 allow-core-thread-timeout false 
INFO: SEC1002: Security Manager is OFF.
INFO: SEC1010: Entering Security Startup Service
INFO: SEC1143: Loading policy provider com.sun.enterprise.security.provider.PolicyWrapper.
INFO: SEC1115: Realm [admin-realm] of classtype [com.sun.enterprise.security.auth.realm.file.FileRealm] successfully created.
INFO: SEC1115: Realm [file] of classtype [com.sun.enterprise.security.auth.realm.file.FileRealm] successfully created.
INFO: SEC1115: Realm [certificate] of classtype [com.sun.enterprise.security.auth.realm.certificate.CertificateRealm] successfully created.
INFO: SEC1011: Security Service(s) Started Successfully
INFO: WEB0169: Created HTTP listener [http-listener-1] on host/port [0.0.0.0:8080]
INFO: WEB0169: Created HTTP listener [http-listener-2] on host/port [0.0.0.0:8181]
INFO: WEB0169: Created HTTP listener [admin-listener] on host/port [0.0.0.0:4848]
INFO: WEB0171: Created virtual server [server]
INFO: WEB0171: Created virtual server [__asadmin]
INFO: WEB0172: Virtual server [server] loaded default web module []
SEVERE: Exception while visiting com/sun/gjc/spi/JdbcObjectsFactory.class of size 3615
java.lang.NullPointerException
    at org.glassfish.hk2.classmodel.reflect.impl.TypesImpl.getType(TypesImpl.java:78)
    at org.glassfish.hk2.classmodel.reflect.impl.ModelClassVisitor.visit(ModelClassVisitor.java:119)
    at org.objectweb.asm.ClassReader.accept(Unknown Source)
    at org.objectweb.asm.ClassReader.accept(Unknown Source)
    at org.glassfish.hk2.classmodel.reflect.Parser$5.on(Parser.java:363)
    at org.glassfish.hk2.classmodel.reflect.util.JarArchive.onSelectedEntries(JarArchive.java:125)
    at org.glassfish.hk2.classmodel.reflect.Parser.doJob(Parser.java:348)
    at org.glassfish.hk2.classmodel.reflect.Parser.access$300(Parser.java:70)
    at org.glassfish.hk2.classmodel.reflect.Parser$3.call(Parser.java:307)
    at org.glassfish.hk2.classmodel.reflect.Parser$3.call(Parser.java:296)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)

INFO: EclipseLink, version: Eclipse Persistence Services - 2.3.2.v20111125-r10461
INFO: file:/C:/Users/Matt/Documents/NetBeansProjects/KSC/build/web/WEB-INF/classes/_KSCPU login successful
WARNING: Multiple [2] JMX MBeanServer instances exist, we will use the server at index [0] : [com.sun.enterprise.v3.admin.DynamicInterceptor@28245558].
WARNING: JMX MBeanServer in use: [com.sun.enterprise.v3.admin.DynamicInterceptor@28245558] from index [0] 
WARNING: JMX MBeanServer in use: [com.sun.jmx.mbeanserver.JmxMBeanServer@3a683b33] from index [1] 
SEVERE: log4j:ERROR log4j called after unloading, see http://logging.apache.org/log4j/1.2/faq.html#unload.
SEVERE: java.lang.IllegalStateException: Class invariant violation
    at org.apache.log4j.LogManager.getLoggerRepository(LogManager.java:199)
    at org.apache.log4j.LogManager.getLogger(LogManager.java:228)
    at org.slf4j.impl.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:64)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:253)
    at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:156)
    at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:132)
    at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:272)
    at org.springframework.beans.TypeConverterDelegate.<clinit>(TypeConverterDelegate.java:53)
    at sun.misc.Unsafe.ensureClassInitialized(Native Method)
    at sun.reflect.UnsafeFieldAccessorFactory.newFieldAccessor(UnsafeFieldAccessorFactory.java:43)
    at sun.reflect.ReflectionFactory.newFieldAccessor(ReflectionFactory.java:140)
    at java.lang.reflect.Field.acquireFieldAccessor(Field.java:949)
    at java.lang.reflect.Field.getFieldAccessor(Field.java:930)
    at java.lang.reflect.Field.get(Field.java:372)
    at org.glassfish.web.loader.WebappClassLoader.clearReferences(WebappClassLoader.java:1833)
    at org.glassfish.web.loader.WebappClassLoader.stop(WebappClassLoader.java:1662)
    at org.glassfish.web.loader.WebappClassLoader.preDestroy(WebappClassLoader.java:1631)
    at org.glassfish.deployment.common.DeploymentContextImpl.getClassLoader(DeploymentContextImpl.java:236)
    at org.glassfish.deployment.common.DeploymentContextImpl.getClassLoader(DeploymentContextImpl.java:186)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:450)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:240)
    at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:389)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$1.execute(CommandRunnerImpl.java:348)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:363)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1085)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1200(CommandRunnerImpl.java:95)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1291)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1259)
    at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:461)
    at com.sun.enterprise.v3.admin.AdminAdapter.service(AdminAdapter.java:212)
    at com.sun.grizzly.tcp.http11.GrizzlyAdapter.service(GrizzlyAdapter.java:179)
    at com.sun.enterprise.v3.server.HK2Dispatcher.dispath(HK2Dispatcher.java:117)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$Hk2DispatcherCallable.call(ContainerMapper.java:354)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:722)

SEVERE:     at org.apache.log4j.LogManager.getLoggerRepository(LogManager.java:199)
SEVERE:     at org.apache.log4j.LogManager.getLogger(LogManager.java:228)
SEVERE:     at org.slf4j.impl.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:64)
SEVERE:     at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:253)
SEVERE:     at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:156)
SEVERE:     at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:132)
SEVERE:     at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:272)
SEVERE:     at org.springframework.beans.TypeConverterDelegate.<clinit>(TypeConverterDelegate.java:53)
SEVERE:     at sun.misc.Unsafe.ensureClassInitialized(Native Method)
SEVERE:     at sun.reflect.UnsafeFieldAccessorFactory.newFieldAccessor(UnsafeFieldAccessorFactory.java:43)
SEVERE:     at sun.reflect.ReflectionFactory.newFieldAccessor(ReflectionFactory.java:140)
SEVERE:     at java.lang.reflect.Field.acquireFieldAccessor(Field.java:949)
SEVERE:     at java.lang.reflect.Field.getFieldAccessor(Field.java:930)
SEVERE:     at java.lang.reflect.Field.get(Field.java:372)
SEVERE:     at org.glassfish.web.loader.WebappClassLoader.clearReferences(WebappClassLoader.java:1833)
SEVERE:     at org.glassfish.web.loader.WebappClassLoader.stop(WebappClassLoader.java:1662)
SEVERE:     at org.glassfish.web.loader.WebappClassLoader.preDestroy(WebappClassLoader.java:1631)
SEVERE:     at org.glassfish.deployment.common.DeploymentContextImpl.getClassLoader(DeploymentContextImpl.java:236)
SEVERE:     at org.glassfish.deployment.common.DeploymentContextImpl.getClassLoader(DeploymentContextImpl.java:186)
SEVERE:     at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:450)
SEVERE:     at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:240)
SEVERE:     at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:389)
SEVERE:     at com.sun.enterprise.v3.admin.CommandRunnerImpl$1.execute(CommandRunnerImpl.java:348)
SEVERE:     at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:363)
SEVERE:     at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1085)
SEVERE:     at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1200(CommandRunnerImpl.java:95)
SEVERE:     at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1291)
SEVERE:     at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1259)
SEVERE:     at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:461)
SEVERE:     at com.sun.enterprise.v3.admin.AdminAdapter.service(AdminAdapter.java:212)
SEVERE:     at com.sun.grizzly.tcp.http11.GrizzlyAdapter.service(GrizzlyAdapter.java:179)
SEVERE:     at com.sun.enterprise.v3.server.HK2Dispatcher.dispath(HK2Dispatcher.java:117)
SEVERE:     at com.sun.enterprise.v3.services.impl.ContainerMapper$Hk2DispatcherCallable.call(ContainerMapper.java:354)
SEVERE:     at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
SEVERE:     at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
SEVERE:     at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
SEVERE:     at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
SEVERE:     at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
SEVERE:     at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
SEVERE:     at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
SEVERE:     at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
SEVERE:     at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
SEVERE:     at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
SEVERE:     at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
SEVERE:     at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
SEVERE:     at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
SEVERE:     at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
SEVERE:     at java.lang.Thread.run(Thread.java:722)
INFO: EJB5181:Portable JNDI names for EJB EventApplicationService: [java:global/KSC/EventApplicationService!com.ksc.services.EventApplicationService, java:global/KSC/EventApplicationService]
INFO: EJB5181:Portable JNDI names for EJB SiteSettingService: [java:global/KSC/SiteSettingService!com.ksc.services.SiteSettingService, java:global/KSC/SiteSettingService]
INFO: EJB5181:Portable JNDI names for EJB EventsService: [java:global/KSC/EventsService, java:global/KSC/EventsService!com.ksc.services.EventsService]
INFO: EJB5181:Portable JNDI names for EJB UserService: [java:global/KSC/UserService, java:global/KSC/UserService!com.ksc.services.UserService]
INFO: EJB5181:Portable JNDI names for EJB RoleService: [java:global/KSC/RoleService!com.ksc.services.RoleService, java:global/KSC/RoleService]
INFO: EJB5181:Portable JNDI names for EJB AnnouncementService: [java:global/KSC/AnnouncementService!com.ksc.services.AnnouncementService, java:global/KSC/AnnouncementService]
INFO: EJB5181:Portable JNDI names for EJB UserInRoleService: [java:global/KSC/UserInRoleService, java:global/KSC/UserInRoleService!com.ksc.services.UserInRoleService]
INFO: WELD-000900 1.1.8 (Final)
INFO: PWC1412: WebModule[null] ServletContext.log():No Spring WebApplicationInitializer types detected on classpath
INFO: PWC1412: WebModule[null] ServletContext.log():Initializing Spring root WebApplicationContext
SEVERE: log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
SEVERE: log4j:WARN Please initialize the log4j system properly.
SEVERE: log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
INFO: PWC1412: WebModule[null] ServletContext.log():Initializing Spring FrameworkServlet 'dispatcher'
INFO: WEB0671: Loading application [KSC] at [/KSC]
INFO: KSC was successfully deployed in 10,070 milliseconds.

So, we have a new error here (which may or may not be related to my original question) about Quartz. It seems the error is JDBC related. I am using sqljdbc4 driver: http://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=11774

like image 578
Matt Avatar asked Dec 19 '12 11:12

Matt


3 Answers

Your glassfish output suggests your log4j configuration isn't correct. Do you have a log4j.properties file? Try adding one with basic content of:

log4j.rootLogger=INFO,stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %-5p - %m (%c)%n

Spring will log the xml file loaded and beans created, but you aren't getting this info due to the log4j issue.

Note: You can achieve the same result (which is a task firing off every 5 seconds) with a much simpler Spring xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">

<bean id="runMeTask" class="com.mkyong.common.RunMeTask" />

<task:scheduled-tasks>
    <task:scheduled ref="runMeTask" method="printMe" cron="0/5 * * * * * " />
</task:scheduled-tasks>

Try this instead, do you get your output now?

like image 94
shuttsy Avatar answered Oct 31 '22 12:10

shuttsy


Finally I found a working solution!!!

Basically, I took everything that was in spring-quartz.xml and stuck it in my dispatcher-servlet.xml instead... now it is running.. lovely...

I'd still like to know why the other way didn't work though. I'm guessing that either:

  1. The line: <import resource="spring-quartz.xml"/> didn't work.. or:
  2. The applicationContext.xml file itself is not being loaded..

I'm no java expert, so I don't know the answer to that one. If anyone does have an idea, please post it here. Anyway, thanks everyone for trying. :-)

like image 1
Matt Avatar answered Oct 31 '22 13:10

Matt


Yes, the problem was the applicationContext.xml file was not loaded.

In as web application the default configuration file loaded is the -servlet.xml file.

Normally all your web tire related beans will be declared in this configuration file.

If you want separate context file to be loaded, you need to specify it in the web.xml file Ex:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/applicationContext.xml 
    </param-value>
</context-param>

<context-param>
    <param-name>contextClass</param-name>
    <param-value>
        org.springframework.web.context.support.XmlWebApplicationContext
    </param-value>
</context-param>

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

In your case either you can move the <import resource="spring-quartz.xml"/> to dispatcher-servlet.xml or you can add the support for applicationContext.xml file

like image 1
Arun P Johny Avatar answered Oct 31 '22 11:10

Arun P Johny