[Logo] Terracotta Discussion Forums
  [Search] Search   [Recent Topics] Recent Topics   [Members]  Member Listing   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
[Expert]
Good Eclipse configuration tutorial  XML
Forum Index -> Quartz
Author Message
davor707

neo

Joined: 04/12/2012 13:41:34
Messages: 6
Offline

Hello,

I am brand new to Quartz. I am trying to implement Quartz tasks into my senior project, but for right now I would be happy if I could get a 'Hello World' printed out at regular intervals.

Does anyone know of a step-by-step tutorial for setting up quartz in an eclipse dynamic web project.

In my web.xml file
Code:
 ...<servlet>
 		<display-name>Quartz Initializer Servlet</display-name>
 		<servlet-name>QuartzInitializer</servlet-name>
 		<servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet</servlet-class>
 		<init-param>
 			<param-name>shutdown-on-unload</param-name>
 			<param-value>true</param-value>
 		</init-param>
 		<init-param>
 			<param-name>start-scheduler-on-load</param-name>
 			<param-value>true</param-value>
 		</init-param>
 		<init-param>
 			<param-name>config-file</param-name>
 			<param-value>quartz.properties</param-value>
 		</init-param>
 		<load-on-startup>1</load-on-startup>
 	</servlet> ...


QuartzTest.java
Code:
 
 package testing;
 
 import static org.quartz.JobBuilder.*;
 import static org.quartz.SimpleScheduleBuilder.*;
 import static org.quartz.CronScheduleBuilder.*;
 import static org.quartz.CalendarIntervalScheduleBuilder.*;
 import static org.quartz.TriggerBuilder.*;
 import static org.quartz.DateBuilder.*;
 
 import org.quartz.JobDetail;
 import org.quartz.Scheduler;
 import org.quartz.SchedulerException;
 import org.quartz.Trigger;
 import org.quartz.impl.StdSchedulerFactory;
 
 public class QuartzTest {
 
     public static void main(String[] args) {
     	System.out.println("MAIN!!!!");
         try {
             // Grab the Scheduler instance from the Factory 
             Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
 
             // and start it off
             scheduler.start();
          // define the job and tie it to our HelloJob class
             JobDetail job = newJob(HelloJob.class)
                 .withIdentity("job1", "group1")
                 .build();
             
             // Trigger the job to run now, and then repeat every 40 seconds
             Trigger trigger = newTrigger()
                 .withIdentity("trigger1", "group1")
                 .startNow()
                 .withSchedule(simpleSchedule()
                         .withIntervalInSeconds(40)
                         .repeatForever())            
                 .build();
             
             // Tell quartz to schedule the job using our trigger
             scheduler.scheduleJob(job, trigger);
             scheduler.shutdown();
 
         } catch (SchedulerException se) {
             se.printStackTrace();
         }
     }
 }
 


HelloJob.java
Code:
 package testing;
 
 import org.quartz.Job;
 import org.quartz.JobExecutionContext;
 import org.quartz.JobExecutionException;
 
 public class HelloJob implements Job {
 
     public HelloJob() {
     }
 
     public void execute(JobExecutionContext context)
       throws JobExecutionException
     {
       System.err.println("Hello!  HelloJob is executing.");
     }
   }
 



quartz.properties
Code:
 
 
 #============================================================================
 # Configure Main Scheduler Properties  
 #============================================================================
 
 org.quartz.scheduler.instanceName: TestScheduler
 org.quartz.scheduler.instanceId: AUTO
 
 org.quartz.scheduler.skipUpdateCheck: true
 
 #============================================================================
 # Configure ThreadPool  
 #============================================================================
 
 org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
 org.quartz.threadPool.threadCount: 12
 org.quartz.threadPool.threadPriority: 5
 
 #============================================================================
 # Configure JobStore  
 #============================================================================
 
 org.quartz.jobStore.misfireThreshold: 60000
 
 org.quartz.jobStore.class: org.quartz.simpl.RAMJobStore
 
 #org.quartz.jobStore.class: org.quartz.impl.jdbcjobstore.JobStoreTX
 #org.quartz.jobStore.driverDelegateClass: org.quartz.impl.jdbcjobstore.PostgreSQLDelegate
 #org.quartz.jobStore.useProperties: false
 #org.quartz.jobStore.dataSource: myDS
 #org.quartz.jobStore.tablePrefix: QRTZ_
 #org.quartz.jobStore.isClustered: false
 
 #============================================================================
 # Configure Datasources  
 #============================================================================
 
 #org.quartz.dataSource.myDS.driver: org.postgresql.Driver
 #org.quartz.dataSource.myDS.URL: jdbc:postgresql://localhost/dev
 #org.quartz.dataSource.myDS.user: jhouse
 #org.quartz.dataSource.myDS.password: 
 #org.quartz.dataSource.myDS.maxConnections: 5
 


web.xml and quartz.properties are located in the WEB-INF folder.
QuartzTest.java and HelloJob.java are just in the testing folder inside the src folder.
quartz-all-2.1.3.jar is in the lib folder.

Error from the console
Code:
INFO: QuartzInitializer: Quartz Scheduler failed to initialize: org.quartz.SchedulerException: Properties file: 'quartz.properties' could not be read. [See nested exception: java.io.FileNotFoundException: quartz.properties (The system cannot find the file specified)]
 Apr 12, 2012 2:30:06 PM org.apache.catalina.core.StandardContext loadOnStartup
 SEVERE: Servlet /Capstone threw load() exception
 java.io.FileNotFoundException: quartz.properties (The system cannot find the file specified)
 	at java.io.FileInputStream.open(Native Method)
 	at java.io.FileInputStream.<init>(Unknown Source)
 	at java.io.FileInputStream.<init>(Unknown Source)
 	at org.quartz.impl.StdSchedulerFactory.initialize(StdSchedulerFactory.java:500)
 	at org.quartz.impl.StdSchedulerFactory.<init>(StdSchedulerFactory.java:309)
 	at org.quartz.ee.servlet.QuartzInitializerServlet.getSchedulerFactory(QuartzInitializerServlet.java:251)
 	at org.quartz.ee.servlet.QuartzInitializerServlet.init(QuartzInitializerServlet.java:183)
 	at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1266)
 	at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1185)
 	at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1080)
 	at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5015)
 	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5302)
 	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
 	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1568)
 	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1558)
 	at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
 	at java.util.concurrent.FutureTask.run(Unknown Source)
 	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
 	at java.lang.Thread.run(Unknown Source)
 


Any input at all would be greatly appreciated. Thank you
adahanne

master

Joined: 03/20/2012 23:14:46
Messages: 92
Offline

did you try to put quartz.properties int he WEB-INF/classes folder ?
see http://quartz-scheduler.org/documentation/quartz-2.x/quick-start , the "properties file" chapter
davor707

neo

Joined: 04/12/2012 13:41:34
Messages: 6
Offline

I just tried moving the properties file there. Thanks it did seem to fix my problem of being unable to locate file. I also changed my quartz.properties file to point (hopefully) at my QuartzTest file.

quartz.properties
Code:
 org.quartz.scheduler.instanceName = QuartzTest
 org.quartz.threadPool.threadCount = 3
 org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
 


console
Code:
 INFO: QuartzInitializer: Quartz Initializer Servlet loaded, initializing Scheduler...
 15:02:49.892 [pool-2-thread-1] INFO  org.quartz.impl.StdSchedulerFactory - Using default implementation for ThreadExecutor
 15:02:49.927 [pool-2-thread-1] INFO  o.quartz.core.SchedulerSignalerImpl - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
 15:02:49.927 [pool-2-thread-1] INFO  org.quartz.core.QuartzScheduler - Quartz Scheduler v.2.1.3 created.
 15:02:49.927 [pool-2-thread-1] INFO  org.quartz.simpl.RAMJobStore - RAMJobStore initialized.
 15:02:49.932 [pool-2-thread-1] INFO  org.quartz.core.QuartzScheduler - Scheduler meta-data: Quartz Scheduler (v2.1.3) 'QuartzTest' with instanceId 'NON_CLUSTERED'
   Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
   NOT STARTED.
   Currently in standby mode.
   Number of jobs executed: 0
   Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 3 threads.
   Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.
 
 15:02:49.932 [pool-2-thread-1] INFO  org.quartz.impl.StdSchedulerFactory - Quartz scheduler 'QuartzTest' initialized from the specified file : 'quartz.properties' from the class resource path.
 15:02:49.932 [pool-2-thread-1] INFO  org.quartz.impl.StdSchedulerFactory - Quartz scheduler version: 2.1.3
 15:02:49.932 [pool-2-thread-1] INFO  org.quartz.core.QuartzScheduler - Scheduler QuartzTest_$_NON_CLUSTERED started.
 15:02:49.932 [QuartzTest_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 0 triggers
 


Any further tips on how to get the scheduler started?
adahanne

master

Joined: 03/20/2012 23:14:46
Messages: 92
Offline

FYI, org.quartz.scheduler.instanceName does not need to match your class name
If you want to see quartz do some scheduling, you should let it enough time to do so !
Make your main thread in your test class sleep for a little while to see your job executing.
You have examples there : http://svn.terracotta.org/svn/quartz/branches/quartz-2.1.x/examples/ that could get you started, not mentioning that the official doc. http://quartz-scheduler.org/documentation/quartz-2.x/quick-start
davor707

neo

Joined: 04/12/2012 13:41:34
Messages: 6
Offline

adahanne wrote:
FYI, org.quartz.scheduler.instanceName does not need to match your class name
 


I am a little confused, why does it not matter? How would QuartzTest be called without it being registered somewhere?

adahanne wrote:
If you want to see quartz do some scheduling, you should let it enough time to do so !
Make your main thread in your test class sleep for a little while to see your job executing.
 


I added a call to thread.sleep, but the scheduler still doesn't start.
hhuynh

cherubim

Joined: 06/16/2006 11:54:06
Messages: 757
Offline

You should call the scheduler.start() only after adding trigger and jobs, etc.

Then call Thread.sleep(xxx), then scheduler.shutdown()

Repost your QuartzTest change here if it didn't work after that setup
davor707

neo

Joined: 04/12/2012 13:41:34
Messages: 6
Offline

hhuynh wrote:
You should call the scheduler.start() only after adding trigger and jobs, etc.

Then call Thread.sleep(xxx), then scheduler.shutdown()

Repost your QuartzTest change here if it didn't work after that setup 


QuartzTest.Java
Code:
 
 package testing;
 
 import static org.quartz.JobBuilder.*;
 import static org.quartz.SimpleScheduleBuilder.*;
 import static org.quartz.CronScheduleBuilder.*;
 import static org.quartz.CalendarIntervalScheduleBuilder.*;
 import static org.quartz.TriggerBuilder.*;
 import static org.quartz.DateBuilder.*;
 
 import org.quartz.JobDetail;
 import org.quartz.Scheduler;
 import org.quartz.SchedulerException;
 import org.quartz.Trigger;
 import org.quartz.impl.StdSchedulerFactory;
 
 public class QuartzTest {
 
     public static void main(String[] args) {
     	System.out.println("MAIN!!!!");
         try {
             // Grab the Scheduler instance from the Factory 
             Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
 
             // define the job and tie it to our HelloJob class
             JobDetail job = newJob(HelloJob.class)
                 .withIdentity("job1", "group1")
                 .build();
             
             // Trigger the job to run now, and then repeat every 40 seconds
             Trigger trigger = newTrigger()
                 .withIdentity("trigger1", "group1")
                 .startNow()
                 .withSchedule(simpleSchedule()
                         .withIntervalInSeconds(40)
                         .repeatForever())            
                 .build();
             
             // Tell quartz to schedule the job using our trigger
             scheduler.scheduleJob(job, trigger);
             // and start it off
             scheduler.start();
             try {
 				Thread.sleep(60000);
 			} catch (InterruptedException e) {
 				// TODO Auto-generated catch block
 				e.printStackTrace();
 			}
             scheduler.shutdown();
 
         } catch (SchedulerException se) {
             se.printStackTrace();
         }
     }
 }
 


Console
Code:
 20:31:14.928 [pool-2-thread-1] INFO  org.quartz.core.QuartzScheduler - Quartz Scheduler v.2.1.3 created.
 Apr 12, 2012 8:31:14 PM org.apache.catalina.core.ApplicationContext log
 INFO: QuartzInitializer: Scheduler has been started...
 Apr 12, 2012 8:31:14 PM org.apache.catalina.core.ApplicationContext log
 INFO: QuartzInitializer: Storing the Quartz Scheduler Factory in the servlet context at key: org.quartz.impl.StdSchedulerFactory.KEY
 20:31:14.931 [pool-2-thread-1] INFO  org.quartz.simpl.RAMJobStore - RAMJobStore initialized.
 20:31:14.933 [pool-2-thread-1] INFO  org.quartz.core.QuartzScheduler - Scheduler meta-data: Quartz Scheduler (v2.1.3) 'QuartzTest' with instanceId 'NON_CLUSTERED'
   Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
   NOT STARTED.
   Currently in standby mode.
   Number of jobs executed: 0
   Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 3 threads.
   Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.
 
 20:31:14.933 [pool-2-thread-1] INFO  org.quartz.impl.StdSchedulerFactory - Quartz scheduler 'QuartzTest' initialized from the specified file : 'quartz.properties' from the class resource path.
 20:31:14.933 [pool-2-thread-1] INFO  org.quartz.impl.StdSchedulerFactory - Quartz scheduler version: 2.1.3
 20:31:14.934 [pool-2-thread-1] INFO  org.quartz.core.QuartzScheduler - Scheduler QuartzTest_$_NON_CLUSTERED started.
 20:31:14.940 [QuartzTest_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 0 triggers
 Apr 12, 2012 8:31:14 PM org.apache.coyote.AbstractProtocol start
 INFO: Starting ProtocolHandler ["http-bio-5000"]
 Apr 12, 2012 8:31:14 PM org.apache.coyote.AbstractProtocol start
 INFO: Starting ProtocolHandler ["ajp-bio-8009"]
 Apr 12, 2012 8:31:14 PM org.apache.catalina.startup.Catalina start
 INFO: Server startup in 5826 ms
 20:31:15.972 [Timer-0] DEBUG org.quartz.utils.UpdateChecker - Checking for available updated version of Quartz...
 20:31:43.323 [QuartzTest_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 0 triggers
 20:31:44.758 [JdbcPooledConnectionSource connection tester] DEBUG c.j.o.j.JdbcPooledConnectionSource - tested connection #29340241, got 1
 20:32:08.842 [QuartzTest_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 0 triggers
 20:32:14.760 [JdbcPooledConnectionSource connection tester] DEBUG c.j.o.j.JdbcPooledConnectionSource - tested connection #29340241, got 1
 20:32:34.387 [QuartzTest_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 0 triggers
 20:32:44.761 [JdbcPooledConnectionSource connection tester] DEBUG c.j.o.j.JdbcPooledConnectionSource - tested connection #29340241, got 1
 20:32:59.032 [QuartzTest_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 0 triggers
 


It still never appears to do its work
adahanne

master

Joined: 03/20/2012 23:14:46
Messages: 92
Offline

I' m confused ...
Are you trying to run a Junit Test case or are you deploying a servlet making calls to Quartz on Tomcat ?
Because I see some Tomcat logs in your logs, and you gave us a Junit Test class code ...
davor707

neo

Joined: 04/12/2012 13:41:34
Messages: 6
Offline

adahanne wrote:
I' m confused ...
Are you trying to run a Junit Test case or are you deploying a servlet making calls to Quartz on Tomcat ?
Because I see some Tomcat logs in your logs, and you gave us a Junit Test class code ...
 


I'm trying to make calls to Quartz on Tomcat, the Junit Test are part of the project, but not what I'm working on right now
davor707

neo

Joined: 04/12/2012 13:41:34
Messages: 6
Offline

I guess my most important question is: how does the quartz scheduler know anything about QuartzTest.java when there are so many other java classes with main() methods in the project? Surely I need to register it somewhere? My guess would be in quartz.properties but I can't figure out how.
 
Forum Index -> Quartz
Go to:   
Powered by JForum 2.1.7 © JForum Team