我的环境是windows 下的。所以路径都写死了。如果是linux‘环境下面的话就得重写了。特别要注意哦
先上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
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
然后重启看看吧。OK