JobDetailBean.java
Upload User: jiancairen
Upload Date: 2007-08-27
Package Size: 26458k
Code Size: 4k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * Copyright 2002-2004 the original author or authors.
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */ 
  16. package org.springframework.scheduling.quartz;
  17. import java.util.Map;
  18. import org.quartz.JobDetail;
  19. import org.quartz.Scheduler;
  20. import org.springframework.beans.factory.BeanNameAware;
  21. import org.springframework.beans.factory.InitializingBean;
  22. import org.springframework.context.ApplicationContext;
  23. import org.springframework.context.ApplicationContextAware;
  24. /**
  25.  * Convenience subclass of Quartz' JobDetail class that eases bean-style
  26.  * usage.
  27.  *
  28.  * <p>JobDetail itself is already a JavaBean but lacks sensible defaults.
  29.  * This class uses the Spring bean name as job name, and the Quartz
  30.  * default group ("DEFAULT") as job group if not specified.
  31.  *
  32.  * @author Juergen Hoeller
  33.  * @since 18.02.2004
  34.  * @see #setName
  35.  * @see #setGroup
  36.  * @see org.springframework.beans.factory.BeanNameAware
  37.  * @see org.quartz.Scheduler#DEFAULT_GROUP
  38.  */
  39. public class JobDetailBean extends JobDetail
  40.     implements BeanNameAware, ApplicationContextAware, InitializingBean {
  41. private String beanName;
  42. private ApplicationContext applicationContext;
  43. private String applicationContextJobDataKey;
  44. /**
  45.  * Register objects in the JobDataMap via a given Map.
  46.  * <p>These objects will be available to this Job only,
  47.  * in contrast to objects in the SchedulerContext.
  48.  * <p>Note: When using persistent Jobs whose JobDetail will be kept in the
  49.  * database, do not put Spring-managed beans or an ApplicationContext
  50.  * reference into the JobDataMap but rather into the SchedulerContext.
  51.  * @param jobDataAsMap Map with String keys and any objects as values
  52.  * (for example Spring-managed beans)
  53.  * @see SchedulerFactoryBean#setSchedulerContextAsMap
  54.  */
  55. public void setJobDataAsMap(Map jobDataAsMap) {
  56. getJobDataMap().putAll(jobDataAsMap);
  57. }
  58. public void setBeanName(String beanName) {
  59. this.beanName = beanName;
  60. }
  61. public void setApplicationContext(ApplicationContext applicationContext) {
  62. this.applicationContext = applicationContext;
  63. }
  64. /**
  65.  * Set the key of an ApplicationContext reference to expose in the JobDataMap,
  66.  * for example "applicationContext". Default is none.
  67.  * Only applicable when running in a Spring ApplicationContext.
  68.  * <p>In case of a QuartzJobBean, the reference will be applied to the Job
  69.  * instance as bean property. An "applicationContext" attribute will correspond
  70.  * to a "setApplicationContext" method in that scenario.
  71.  * <p>Note that BeanFactory callback interfaces like ApplicationContextAware
  72.  * are not automatically applied to Quartz Job instances, because Quartz
  73.  * itself is reponsible for the lifecycle of its Jobs.
  74.  * <p><b>Note: When using persistent job stores where JobDetail contents will
  75.  * be kept in the database, do not put an ApplicationContext reference into
  76.  * the JobDataMap but rather into the SchedulerContext.</b>
  77.  * @see SchedulerFactoryBean#setApplicationContextSchedulerContextKey
  78.  * @see org.springframework.context.ApplicationContext
  79.  */
  80. public void setApplicationContextJobDataKey(String applicationContextJobDataKey) {
  81. this.applicationContextJobDataKey = applicationContextJobDataKey;
  82. }
  83. public void afterPropertiesSet() {
  84. if (getName() == null) {
  85. setName(this.beanName);
  86. }
  87. if (getGroup() == null) {
  88. setGroup(Scheduler.DEFAULT_GROUP);
  89. }
  90. if (this.applicationContextJobDataKey != null) {
  91. if (this.applicationContext == null) {
  92. throw new IllegalStateException(
  93.     "JobDetailBean needs to be set up in an ApplicationContext " +
  94.     "to be able to handle an 'applicationContextJobDataKey'");
  95. }
  96. getJobDataMap().put(this.applicationContextJobDataKey, this.applicationContext);
  97. }
  98. }
  99. }