Main Tutorials

How to list all Jobs in the Quartz Scheduler

Below are two code snippets to show you how to list all Quartz jobs. Quartz 2 APIs are changed a lot, so syntax is different from Quartz 1.x.

1. Quartz 2.1.5 example


Scheduler scheduler = new StdSchedulerFactory().getScheduler();
    	
   for (String groupName : scheduler.getJobGroupNames()) {

     for (JobKey jobKey : scheduler.getJobKeys(GroupMatcher.jobGroupEquals(groupName))) {
				
	  String jobName = jobKey.getName();
	  String jobGroup = jobKey.getGroup();
				
	  //get job's trigger
	  List<Trigger> triggers = (List<Trigger>) scheduler.getTriggersOfJob(jobKey);
	  Date nextFireTime = triggers.get(0).getNextFireTime(); 

		System.out.println("[jobName] : " + jobName + " [groupName] : "
			+ jobGroup + " - " + nextFireTime);

	  }

    }

2. Quartz 1.8.6 example


    Scheduler scheduler = new StdSchedulerFactory().getScheduler();

    //loop all group
    for (String groupName : scheduler.getJobGroupNames()) {

	//loop all jobs by groupname
	for (String jobName : scheduler.getJobNames(groupName)) {
				
          //get job's trigger
	  Trigger[] triggers = scheduler.getTriggersOfJob(jobName,groupName);
	  Date nextFireTime = triggers[0].getNextFireTime();

	  System.out.println("[jobName] : " + jobName + " [groupName] : "
			+ groupName + " - " + nextFireTime);

	}

    }
Note
You may also interest at this example – list all jobs and display on JSF page.

References

  1. Quartz list job cookbook

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
6 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
bbq1980
7 years ago

Hi, what are the reasons when a scheduled job is not listed? It appears, once it has been executed at least one. But when it is scheduled (e.g. starting in 6 days for the first time), it does not appear in the list.

Kusuma Thatavarthi
9 years ago

can i get whether a particular job using {jobid,groupname} is scheduled or not?

Alex Knopov
4 years ago

A bit better performance can be achieved for Quartz 2.x with single loop

Set<TriggerKey> allTriggerKeys = scheduler.getTriggerKeys(GroupMatcher.anyTriggerGroup());
for (TriggerKey triggerKey: allTriggerKeys)
{
scheduledTriggers.add(scheduler.getTrigger(triggerKey));
}

Magesh Babu
10 years ago

Hi,

Can i get the list of completed job names in quartz

hadjila
10 years ago

Hi yong,

I’ve create class who named NcaJobListener and implement joblistener, then i would integrate this in my xml file of scheduler.
This is the file xml.

<?xml version="1.0" encoding="UTF-8"?>
<!-- ==================================================================== 
	SVN-Author : $LastChangedBy: cdue $ SVN-Date : $LastChangedDate: 2013-02-22 
	19:12:35 +0100 (Fri, 22 Feb 2013) $ SVN-Revision : $LastChangedRevision: 
	1201 $ SVN-Header : $HeadURL: https://svn.prosodie/NCA/trunk/NCAScheduler/src/schedulerAppContext.xml 
	$ SVN-Id : $Id: schedulerAppContext.xml 1388 2013-03-27 08:12:22Z cdue $ 
	==================================================================== -->

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

    
	<!-- Import NCA Batch applicationContext: mandatory to be able to run jobs usin jobLauncher. -->
	<import resource="applicationContext.xml" />

	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${DBConnectionFactory.default.driver}" />
		<property name="url" value="${DBConnectionFactory.default.url}" />
		<property name="username" value="${DBConnectionFactory.default.user}" />
		<property name="password" value="${DBConnectionFactory.default.password}" />
	</bean>

	<bean id="jobRepository"
		class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
		<property name="transactionManager" ref="transactionManager" />
	</bean>

	<bean id="transactionManager"
		class="org.springframework.batch.support.transaction.ResourcelessTransactionManager">
	</bean>

	<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>file:${app.home}/cfg/nca.properties</value>
				<value>file:${app.home}/cfg/${module.name}/${module.name}.properties
				</value>
			</list>
		</property>
	</bean>

	<bean id="entityManagerFactory"
		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="jpaVendorAdapter">
			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
				<property name="showSql" value="${hibernate.show_sql}" />
			</bean>
		</property>
		<property name="jpaPropertyMap">
			<map>
				<entry key="hibernate.cache.use_second_level_cache" value="${hibernate.cache.use_second_level_cache}" />
				<entry key="hibernate.cache.use_query_cache" value="${hibernate.cache.use_query_cache}" />
				<entry key="hibernate.cache.provider_class" value="${hibernate.cache.provider_class}" />
				<entry key="hibernate.dialect" value="${hibernate.dialect}" />
				<entry key="hibernate.show_sql" value="${hibernate.show_sql}" />
				<entry key="hibernate.connection.show_sql" value="true" />
				<entry key="hibernate.id.new_generator_mappings" value="false" />
			</map>
		</property>
	</bean>


	<bean id="jobRegistry"
		class="org.springframework.batch.core.configuration.support.MapJobRegistry" />
	
	<bean id="taskExecutor" class="org.springframework.core.task.SimpleAsyncTaskExecutor" /> <!-- Or ThreadPoolTaskExecutor... -->

	<bean id="jobLauncher" class="com.prosodie.nca.batch.launch.JpaIteratorJobLauncher">
		<property name="jobRepository" ref="jobRepository" />
		<property name="entityManagerFactory" ref="entityManagerFactory" />
		<property name="taskExecutor" ref="taskExecutor" />
	</bean>

	<bean
		class="org.springframework.batch.core.configuration.support.JobRegistryBeanPostProcessor">
		<property name="jobRegistry" ref="jobRegistry" />
	</bean>

	<bean id="mapJobRepository"
		class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean"
		lazy-init="true" autowire-candidate="false" />


	<!-- ***************************************************** -->
	<!-- Notilus project codes export jobDetail definition.    -->
	<!-- ***************************************************** -->

	<bean id="notilusProjectCodesExportJobDetail"
		class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
		<property name="jobClass"
			value="com.prosodie.nca.scheduler.JobLauncherDetails" />
		<property name="group" value="quartz-batch" />
		<!-- <property name="concurrentExectionDisallowed" value="true" /> -->

		<property name="jobDataAsMap">

			<bean class="org.quartz.JobDataMap">
				<constructor-arg>
					<map>
						<entry key="jobName" value="batch.export.notilus.project.codes" />
						<!-- <entry key="input.jpa.queryString" value="select codeBanque from Emetteur" /> -->
						<entry key="maxRetry" value="3" />
						<entry key="listenerDelay" value="2000" />
						<!-- The alarmManagedByJob parameter set to false tells the job not to send ExploitAlarms on job failure.  -->
						<entry key="alarmManagedByJob" value="false" />
					</map>
				</constructor-arg>
			</bean>
		</property>
	</bean>








	<!-- ***************************************************** -->
	<!-- Notilus exemple project nca    -->
	<!-- ***************************************************** -->

	  <bean id="notilusProjectCodesExempleNca"
		class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
		<property name="jobClass"
			value="com.prosodie.nca.scheduler.JobLauncherDetailsExemple" />
		<property name="group" value="quartz-batch" />
		<!--  Concurrent 
		<property name="concurrentExectionDisallowed" value="true" /> -->

		  <property name="jobDataAsMap">

			<bean class="org.quartz.JobDataMap">
				<constructor-arg>
					<map>
						<entry key="jobName" value="batch.exemple.projet.nca" />
						 <!--   <entry key="input.jpa.queryString" value="select codeBanque from Emetteur" /> -->
						 <entry key="maxRetry" value="3" />
						<entry key="listenerDelay" value="2000" />
						<!-- The alarmManagedByJob parameter set to false tells the job not to send ExploitAlarms on job failure.  -->
						<entry key="alarmManagedByJob" value="false" />
					</map>
				</constructor-arg>
			</bean>
		</property>
	</bean> 
	
	
	
	
	
	
	
	
	



	<!-- *************************************************************** -->
	<!-- Declare a job listener that will take care of job retries, etc. -->
	<!-- *************************************************************** -->
	
	
	
	
	<!-- ***************************************************** -->
	<!-- Here is the Scheduler Factory that will trigger jobs. -->
	<!-- ***************************************************** -->
	

	<bean id="ncaSchedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="schedulerName" value="ncaScheduler" />
		<property name="dataSource" ref="dataSource" />
		<property name="waitForJobsToCompleteOnShutdown" value="true" />
		<property name="overwriteExistingJobs" value="true"/>
		<!-- concurrence
		 <property name="DisallowConcurrentExecution" value="true"> -->
		
		<property name="schedulerContextAsMap">
			<map>
				<entry key="jobLocator" value-ref="jobRegistry" />
				<entry key="jobLauncher" value-ref="jobLauncher" />
			</map>
		</property>
		
	
	    
	    
<property name="listener">

<bean id="ncaJobListener" class="com.prosodie.nca.scheduler.NcaJobListener">
        <property name="name" value="nca_job_listener"/>
 </bean>




</property>
		<property name="triggers">
<list>
			<!-- Add a trigger definition for Notilus project codes export job. -->
			<bean id="notilusProjectCodesExportCronTrigger"
				class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
				<property name="jobDetail" ref="notilusProjectCodesExportJobDetail" />
				<!-- <property name="cronExpression" value="0/50 * * * * ?" /> -->
				<property name="cronExpression" value="50 * * * * ?" />
				<property name="group" value="quartz-batch" />
			</bean>


			<!-- Add a trigger definition for Notilus project codes exemple job. -->
			<bean id="notilusProjectCodesExempleCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
				<property name="jobDetail" ref="notilusProjectCodesExempleNca" />
				<property name="cronExpression" value="30 * * * *  ?"/>
			
				<property name="group" value="quartz-batch" />
			</bean>
			
			
			

</list>
		</property>
		
		<!-- Make the Spring Context Available -->
		<!-- <property name="applicationContextSchedulerContextKey" value="schedulerApplicationContext" /> -->
			
		<property name="quartzProperties">
			<props>
				<prop key="org.quartz.scheduler.skipUpdateCheck">${quartz.scheduler.skipUpdateCheck}</prop>

				<prop key="org.quartz.scheduler.instanceName">${quartz.scheduler.instanceName}</prop>
				<prop key="org.quartz.scheduler.instanceId">${quartz.scheduler.instanceId}</prop>
				<prop key="org.quartz.threadPool.class">${quartz.threadPool.class}</prop>
				<prop key="org.quartz.threadPool.threadCount">${quartz.threadPool.threadCount}</prop>
				<prop key="org.quartz.threadPool.threadPriority">${quartz.threadPool.threadPriority}</prop>
				<prop key="org.quartz.jobStore.misfireThreshold">${quartz.jobStore.misfireThreshold}</prop>
				<prop key="org.quartz.jobStore.class">${quartz.jobStore.class}</prop>

				<prop key="org.quartz.jobStore.driverDelegateClass">${quartz.jobStore.driverDelegateClass}</prop>
				<prop key="org.quartz.jobStore.useProperties">${quartz.jobStore.useProperties}</prop>
				<prop key="org.quartz.jobStore.dataSource">${quartz.jobStore.dataSource}</prop>
				<prop key="org.quartz.jobStore.tablePrefix">${quartz.jobStore.tablePrefix}</prop>

				<!-- <prop key="org.quartz.dataSource.myDS.driver">com.sybase.jdbc4.jdbc.SybDriver</prop> 
					<prop key="org.quartz.dataSource.myDS.URL">jdbc:sybase:Tds:192.168.154.39:5001/NCA?Language=us_english&amp;charset=iso_1</prop> 
					<prop key="org.quartz.dataSource.myDS.user">sa</prop> <prop key="org.quartz.dataSource.myDS.password"></prop> 
					<prop key="org.quartz.dataSource.myDS.maxConnections">5</prop> <prop key="org.quartz.dataSource.myDS.validationQuery">select 
					0 from dual</prop> -->
			</props>
		</property>
	</bean>

</beans>

But when you run this they have some mistakes:
Exception in thread “main” org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘ncaSchedulerFactoryBean’ defined in class path resource [schedulerAppContext.xml]: Cannot create inner bean ‘ncaJobListener’ of type [com.prosodie.nca.scheduler.NcaJobListener] while setting bean property ‘listener’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘ncaJobListener’ defined in class path resource [schedulerAppContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.prosodie.nca.scheduler.NcaJobListener]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.prosodie.nca.scheduler.NcaJobListener.()
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:281)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:120)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1360)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1118)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:567)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83)
at com.prosodie.nca.scheduler.StandaloneScheduler.main(StandaloneScheduler.java:46)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘ncaJobListener’ defined in class path resource [schedulerAppContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.prosodie.nca.scheduler.NcaJobListener]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.prosodie.nca.scheduler.NcaJobListener.()
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:997)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:943)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:270)
… 15 more
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.prosodie.nca.scheduler.NcaJobListener]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.prosodie.nca.scheduler.NcaJobListener.()
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:72)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:990)
… 19 more
Caused by: java.lang.NoSuchMethodException: com.prosodie.nca.scheduler.NcaJobListener.()
at java.lang.Class.getConstructor0(Unknown Source)
at java.lang.Class.getDeclaredConstructor(Unknown Source)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:67)
… 20 more

Please can i help me for correct this mistakes.
Thanks

Armen
11 years ago

You have all better solutions in your site, thanks. How is you know enormous kind of technologies?:)