论坛首页 Java企业应用论坛

spring2.0+quartz 使用

浏览 9029 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (3)
作者 正文
   发表时间:2008-12-11   最后修改:2008-12-15
方法一:
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;
	}

}

   发表时间:2008-12-12  
quartz 要使用1.5.0版本以上,之前有问题!
1 请登录后投票
   发表时间:2008-12-27  
请问一句,是不是项目启动就开始执行了?
0 请登录后投票
   发表时间:2009-05-26  
laozhou 写道

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

项目启动就可以运行。可以打一些日志,或者DEBUG跟踪一下就知道了。测试时,定时时间间隔可以短一些
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics