jboss6 quartz2.0.2集成配置使用

先上mbean 路径jboss/server/all/deploy/schedule-service.xml

<mbean code="org.jboss.varia.scheduler.Scheduler" name="jboss.piosan.util:service=QuartzShedule">
<attribute name="StartAtStartup">true</attribute>
<attribute name="SchedulableClass">com.test.schedule.QuartzSchedule</attribute>
<attribute name="SchedulableArgumentTypes">java.lang.String</attribute>
<attribute name="SchedulableArguments">E:/java/jboss-6.1.0.Final/server/all/deploy/quartzConf/quartz.properties</attribute>
<attribute name="InitialStartDate">NOW</attribute>
<attribute name="SchedulePeriod">1000</attribute>
<attribute name="InitialRepetitions">1</attribute>
</mbean>

对应的quartz配置文件为deploy下的quartzConf/quartz.properties


#============================================================================
# Configure Main Scheduler Properties
#============================================================================

org.quartz.scheduler.instanceName = MyScheduler
org.quartz.scheduler.instanceId = AUTO

org.quartz.scheduler.skipUpdateCheck = true

#============================================================================
# Configure ThreadPool
#============================================================================

org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 3
org.quartz.threadPool.threadPriority = 5

#============================================================================
# Configure JobStore
#============================================================================

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

 

#============================================================================
# Configure Datasources
#============================================================================

 

 

#============================================================================
# Configure Plugins
#============================================================================

org.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingJobHistoryPlugin

org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin
org.quartz.plugin.jobInitializer.fileNames = E:/java/jboss-6.1.0.Final/server/all/deploy/quartzConf/quartz_data.xml
org.quartz.plugin.jobInitializer.failOnFileNotFound = true
org.quartz.plugin.jobInitializer.scanInterval = 120
org.quartz.plugin.jobInitializer.wrapInUserTransaction = false

 

接下来是quartz_data.xml即上面properties中指定的job配置文件

<?xml version="1.0" encoding="UTF-8"?>
<job-scheduling-data xmlns="http://www.quartz-scheduler.org/xml/JobSchedulingData"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.quartz-scheduler.org/xml/JobSchedulingDatahttp://www.quartz-scheduler.org/xml/job_scheduling_data_1_8.xsd"
version="1.8">

<pre-processing-commands>
<delete-jobs-in-group>*</delete-jobs-in-group> <!-- clear all jobs in scheduler -->
<delete-triggers-in-group>*</delete-triggers-in-group> <!-- clear all triggers in scheduler -->
</pre-processing-commands>

<processing-directives>
<!-- if there are any jobs/trigger in scheduler of same name (as in this file), overwrite them -->
<overwrite-existing-data>true</overwrite-existing-data>
<!-- if there are any jobs/trigger in scheduler of same name (as in this file), and over-write is false, ignore them rather then generating an error -->
<ignore-duplicates>false</ignore-duplicates>
</processing-directives>

<schedule>
<job>
<name>MyJobTest</name>
<job-class>com.test.schedule.TestSchedule</job-class>
<job-data-map>
<entry>
<key>name</key>
<value>someValue</value>
</entry>
<entry>
<key>someOtherKey</key>
<value>someOtherValue</value>
</entry>
</job-data-map>
</job>


<trigger>
<cron>
<name>MyJobTest</name>
<job-name>MyJobTest</job-name>
<misfire-instruction>MISFIRE_INSTRUCTION_FIRE_ONCE_NOW</misfire-instruction>
<cron-expression>0 10/1 * * * ?</cron-expression>
</cron>
</trigger>

</schedule>
</job-scheduling-data>

在这个配置文件中我测试了参数的传递。就是
<job-data-map>
<entry>
<key>name</key>
<value>someValue</value>
</entry>
<entry>
<key>someOtherKey</key>
<value>someOtherValue</value>
</entry>
</job-data-map>

如果不需要可以省略

下面上代码哦

我们是通过jboss的mbean去加载我们的job的所以先来个Schedulable

  1. package com.test.schedule;
  2. import java.io.File;
  3. import java.util.Date;
  4. import org.jboss.varia.scheduler.Schedulable;
  5. import org.quartz.Scheduler;
  6. import org.quartz.SchedulerException;
  7. import org.quartz.impl.StdSchedulerFactory;
  8. publicclass QuartzSchedule implements Schedulable{
  9. private org.quartz.SchedulerFactory factory=null;
  10. private Scheduler sd=null;
  11. public QuartzSchedule(String fileName){
  12. File file=new File(fileName);
  13. if(file.exists()){
  14. System.setProperty("QUARTZ",file.getParent());
  15. try {
  16. factory=new StdSchedulerFactory(fileName);
  17. } catch (SchedulerException e) {
  18. // TODO Auto-generated catch block
  19. e.printStackTrace();
  20. }
  21. }else{
  22. System.out.println(fileName+"-----------------------");
  23. }
  24. }
  25. publicvoid perform(Date arg0, long arg1) {
  26. if(factory!=null){
  27. System.out.println("调度任务开始启动!------------");
  28. try {
  29. sd=factory.getScheduler();
  30. sd.start();
  31. } catch (SchedulerException e) {
  32. // TODO Auto-generated catch block
  33. e.printStackTrace();
  34. }
  35. System.out.println("Quartz启动完毕");
  36. }
  37. }
  38. }
package com.test.schedule;

import java.io.File;
import java.util.Date;

import org.jboss.varia.scheduler.Schedulable;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;


public class QuartzSchedule implements Schedulable{
	private org.quartz.SchedulerFactory factory=null;
	private Scheduler sd=null;
	public QuartzSchedule(String fileName){
		File file=new File(fileName);
		if(file.exists()){
			System.setProperty("QUARTZ",file.getParent());
			try {
				factory=new StdSchedulerFactory(fileName);
			} catch (SchedulerException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			System.out.println(fileName+"-----------------------");
		}
		
	}
	public void perform(Date arg0, long arg1) {
		if(factory!=null){
			System.out.println("调度任务开始启动!------------");
			try {
				sd=factory.getScheduler();
				sd.start();
			} catch (SchedulerException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("Quartz启动完毕");
		}
	}

}


要实现schedulable接口先初始化SchedulerFactory之后就简单了。启动任务就OK

在当前测试项目中我建立了一个名为MyJobTest的任务代码如下

package com.test.schedule;

import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class TestSchedule implements Job {

public void execute(JobExecutionContext context) throws JobExecutionException {
JobDataMap map=context.getJobDetail().getJobDataMap();
System.out.println("我是调度任务,我正在执行!!!!!!!!!!!!!!!"+map.get("name"));
}

}

ok全部结束注意发布地址是server/all/lib下面。另外我用的是比较新的quartz所以要替换jboss/common/lib
下面的quartz-x.x.jar为quartz-2.0.2.jar quartz-jboss-2.0.2.jar

然后重启看看吧

你可能感兴趣的:(quartz)