`
fei089
  • 浏览: 6386 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
最近访客 更多访客>>
社区版块
存档分类
最新评论

spring2.0+quartz 使用

阅读更多
方法一:
1.quartz_job.xml文件配置:
<?xml version="1.0" encoding="UTF-8"?>
<quartz version="1.5">
	<job>
<job-detail>
  <name>assess.HastenJob</name> 
  <group>taskGroup</group> 
  <job-class>com.aspire.simp.app.assess.common.job.AssessHastenJob</job-class> 
  <volatility>false</volatility> 
  <durability>false</durability> 
  <recover>false</recover> 
  <job-data-map allows-transient-data="true">
  <entry>
      <key>serviceName</key> 
      <value>assess.HastenJob</value> 
  </entry>
  </job-data-map>
</job-detail>
  
<trigger>
<cron>
  <name>AssessHastenJobTrigger</name> 
  <group>triggerGroup</group> 
  <job-name>assess.HastenJob</job-name> 
  <job-group>taskGroup</job-group> 
  <cron-expression>1/20 * * * * ?</cron-expression> 
  </cron>
</trigger>
</job>
</quartz>


2.quartz.properties配置
#
# Configure Main Scheduler Properties 
#

org.quartz.scheduler.instanceName = AssessScheduler
org.quartz.scheduler.instanceId = one

#
# Configure ThreadPool 
#

org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 120
org.quartz.threadPool.threadPriority = 8

#
# Configure JobStore 
#

org.quartz.jobStore.misfireThreshold = 5000

org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore

# ===========================================================================
# Configure SchedulerPlugins  ===============================================
# ===========================================================================
org.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingTriggerHistoryPlugin

org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.JobInitializationPlugin
org.quartz.plugin.jobInitializer.fileName = quartz_jobs.xml
org.quartz.plugin.jobInitializer.overWriteExistingJobs = false
org.quartz.plugin.jobInitializer.failOnFileNotFound = true
org.quartz.plugin.jobInitializer.scanInterval = 50

org.quartz.plugin.shutdownhook.class = org.quartz.plugins.management.ShutdownHookPlugin
org.quartz.plugin.shutdownhook.cleanShutdown = true


3.JAVA类

public class AssessHastenJob implements Job
{
	private static Log logger = LogFactory.getLog(AssessHastenJob.class);
	
	private AssessConfigManager assessConfigManager = null;
	private SimsConfigBiz simsConfigBiz = null;
	
	public void execute(JobExecutionContext arg0) throws JobExecutionException
	{
             SimsConfigBiz simsConfigBiz = (SimsConfigBiz)BeanLocator.getInstance().getBean("simsConfigBiz");
	    AssessConfigManager assessConfigManager =(AssessConfigManager) BeanLocator.getInstance().getBean("assessConfigManager");
			
	
	}
}


4.系统初始化类
/**
	 * 初始化quartz配置(quartz配置文件目录等).
	 */
	public void initQuartz() {
		// 初始化quartz配置(quartz配置文件目录等).
		// 从init-param中得到quartz.properties配置文件相对路径.
		String quartzConfig = this.getInitParameter("quartzConfig");
		if (quartzConfig == null || quartzConfig.length() == 0) {
			logger.info("没有指定quartz配置文件相对路径!");
			return;
		}

		// 得到quartz配置文件全路径名
		String quartzConfigFileName = ConfigurationHelper
				.getFullFileName(quartzConfig);
		FileInputStream fileInputStream = null;
		try {
			fileInputStream = new FileInputStream(quartzConfigFileName);
		} catch (FileNotFoundException e) {
			logger.error("找不到quartz配置文件,文件名:" + quartzConfigFileName);
			return;
		}

		// 得到quartz配置文件的目录
		String quartzConfigPath = StringUtils.substringBeforeLast(
				quartzConfigFileName, "/");

		// 加载配置文件
		Properties properties = new Properties();
		try {
			properties.load(fileInputStream);

			// 设置quartz_jobs.xml路径
			properties.setProperty("org.quartz.plugin.jobInitializer.fileName",
					quartzConfigPath + File.separator + "quartz_jobs.xml");

		} catch (IOException e) {
			logger.error("加载quartz配置文件失败,文件名:" + quartzConfigFileName);
			return;
		}

		// 启动quartz
		StdSchedulerFactory factory = null;
		try {
			factory = new StdSchedulerFactory(properties);
		} catch (SchedulerException e) {
			logger.error("初始化quartz失败,失败原因:" + e.getMessage());
			return;
		}

		// 启动quartz任务调度器
		try {
			scheduler = factory.getScheduler();
			scheduler.start();
		} catch (SchedulerException e) {
			logger.error("启动quartz任务调度失败,失败原因:" + e.getMessage());
			return;
		}

		logger.info("启动quartz任务调度成功!");
	}


5.web.xml
<servlet>
		<servlet-name>defaultSystemInit</servlet-name>
		<servlet-class>com.aspire.simp.app.common.servlet.DefaultSystemInitServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
		<init-param>
			<!--log4j配置文件相对路径(子系统必须要配置log4j)-->
			<param-name>log4jConfig</param-name>
			<param-value>log4j/log4j.xml</param-value>
		</init-param>
		<init-param>
			<!--attachment配置文件相对路径(如果子系统需要附件上载才需要配置)-->
			<param-name>quartzConfig</param-name>
			<param-value>quartz/quartz.properties</param-value>
		</init-param>


  	</servlet>

方法二:
1.spring 配置文件加下面这段就行了


 
<!-- 定时服务定义 -->
	<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<!-- 自动启动 -->
		<property name="autoStartup">
			<value>true</value>
		</property>
		<property name="triggers">
			<list>
				<ref local="assessHastenTaskTrigger"/>
			</list>
		</property>
	</bean>

<!-- 定时器定义 -->
	<bean id="assessHastenTaskTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail">
			<ref bean="assessHastenJobDetail"/>
		</property>
		<property name="cronExpression">
			<!-- 每20分钟运行一次 -->
			<value>0 1/20 * * * ?</value>
		</property>		
	</bean>

	<!-- JobDetail定义 -->
	<bean id="assessHastenJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject"><ref bean="assessHastenJob"/></property>
		<property name="targetMethod"><value>execute</value></property>
		<property name="concurrent" value="false"/>
	</bean>		
	
	<!-- Job定义 -->
	<bean id="assessHastenJob" class="com.aspire.simp.app.assess.common.job.AssessHastenJob">
		<property name="assessConfigManager" ref="assessConfigManager"/>
		<property name="simsConfigBiz" ref="simsConfigBiz"/>
   </bean>


2.JAVA类

public class AssessHastenJob 
{
	private static Log logger = LogFactory.getLog(AssessHastenJob.class);
	
	private AssessConfigManager assessConfigManager = null;
	private SimsConfigBiz simsConfigBiz = null;
	
	public void execute()
	{
	
	}
         public void setAssessConfigManager(AssessConfigManager assessConfigManager)
	{
		this.assessConfigManager = assessConfigManager;
	}

	public void setSimsConfigBiz(SimsConfigBiz simsConfigBiz)
	{
		this.simsConfigBiz = simsConfigBiz;
	}

}

分享到:
评论
4 楼 fei089 2009-05-26  
laozhou 写道

请问一句,是不是项目启动就开始执行了?

项目启动就可以运行。可以打一些日志,或者DEBUG跟踪一下就知道了。测试时,定时时间间隔可以短一些
3 楼 12616383 2009-05-10  
Quarzt已经配置好  tomcat 5.*启动报错   大佬 帮忙看下

严重: Error listenerStart
2009-5-8 16:00:39 org.apache.catalina.core.StandardContext start
严重: Context [/irdpcms] startup failed due to previous errors



  
<!--配置一个Job-->   
    <bean id="testJobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">   
            <property name="jobClass">   
                <value>com.zfsoft.irdpcms.action.QuartzJob</value>   
            </property>   
               
            <property name="jobDataAsMap">   
                <map>   
                    <entry key="pringThing">   
                        <value>this is the first Quarzt</value>   
                    </entry>     
                       
                    <entry key="printName">   
                        <value>leo</value>   
                    </entry>             
                </map>   
            </property>   
    </bean>   
       
    <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">   
        <property name="jobDetail">   
            <ref bean="testJobDetail"/>   
        </property>   
           
        <property name="startDelay">   
            <value>10</value>   
        </property>   
           
        <property name="repeatInterval">   
            <value>2000</value>   
        </property>   
           
        <property name="repeatCount">   
            <value>20</value>   
        </property>   
           
    </bean>   
       
    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">   
        <property name="autoStartup">   
            <value>true</value>   
        </property>   
           
        <property name="triggers">   
            <list>   
                <ref bean="simpleTrigger"/>   
            </list>   
        </property>   
    </bean>   



2 楼 laozhou 2008-12-27  
请问一句,是不是项目启动就开始执行了?
1 楼 swen00 2008-12-12  
quartz 要使用1.5.0版本以上,之前有问题!

相关推荐

Global site tag (gtag.js) - Google Analytics