这里以oracle数据库为例):
qrtz_blob_triggers,qrtz_calendars,
qrtz_cron_triggers,
qrtz_fired_triggers,
qrtz_job_details,
qrtz_job_listeners,
qrtz_locks,
qrtz_paused_trigger_grps,
qrtz_scheduler_state,
qrtz_simple_triggers,
qrtz_triggers,
qrtz_trigger_listeners
加入quartz 所需要的包:
放入jboss-4.2.3.GA\server\all\lib下
先把jboss自带的quartz jar删除掉。包括(quartz-ra.rar)都删除掉。commons-dbcp-1.3.jar
commons-pool-1.5.4.jar
jta-1.1.jar
log4j-1.2.14.jar
quartz-all-1.8.5.jar
slf4j-api-1.6.0.jar
slf4j-log4j12-1.6.0.jar
3.创建quartz-service.xml文件(放入jboss-4.2.3.GA\server\all\deploy下),文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<server>
<mbean code="org.quartz.ee.jmx.jboss.QuartzService"
name="user:service=QuartzService,name=QuartzService">
<attribute name="JndiName">Quartz</attribute>
<attribute name="StartScheduler">true</attribute>
<attribute name="Properties">
#============================================================================
# Configure Main Scheduler Properties
#============================================================================
#==========集群名称,每个节点的名字都一样===========
org.quartz.scheduler.instanceName = quartzjbossdemopartitionName
#==========集群每个节点ID=======
org.quartz.scheduler.instanceId = AUTO
#============================================================================
# Configure ThreadPool
#============================================================================
#=======线程====
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 25
org.quartz.threadPool.threadPriority = 5
#============================================================================
# Configure JobStore
#============================================================================
org.quartz.jobStore.misfireThreshold = 60000
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.oracle.OracleDelegate
org.quartz.jobStore.dataSource = QUARTZ
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.isClustered = true
org.quartz.jobStore.clusterCheckinInterval = 30000
#============================================================================
# Configure Datasources
#============================================================================
#=================数据库写上自己的数据库======
org.quartz.dataSource.QUARTZ.driver = oracle.jdbc.driver.OracleDriver
org.quartz.dataSource.QUARTZ.URL = jdbc:oracle:thin:@192.168.111.24:1521:db org.quartz.dataSource.QUARTZ.user = rootmq
org.quartz.dataSource.QUARTZ.password = rootmq
org.quartz.dataSource.QUARTZ.maxConnections = 5
org.quartz.dataSource.QUARTZ.validationQuery=select 0 from dual
</attribute>
</mbean>
</server>
4. java代码
每一个 Quartz Job 必须有一个实现了 org.quartz.Job 接口的具体类.
public class JbossJob implements Job {
public void execute(JobExecutionContext arg0) throws JobExecutionException {
System.out.println("hello jbossJob .....");
}
}
5. 具体调用
public class StartOrCloseScheduler implements Serializable{
/**
*
*/
private static final long serialVersionUID = -2266323478579408291L;
private static Logger myLogger = Logger.getLogger(StartOrCloseScheduler.class);
private static Scheduler sched = null;
/**
* 启动任务
* jobName: job名称
* jobGroupName: job组名称
* triggerName: trigger名称
* triggerGroupName:trigger组名称
*/
@SuppressWarnings("rawtypes")
public static void start(String jobName,String jobGroupName,
String triggerName,String triggerGroupName,
Class c, String str) {
try {
InitialContext ctx = new InitialContext();
sched = (Scheduler) ctx.lookup("Quartz");
System.out.println("Scheduler:" + sched);
} catch (NamingException e) {
e.printStackTrace();
}
JobDetail job = new JobDetail(jobName,jobGroupName, c);
System.out.println("JobDetail:" + job);
Trigger trigger = new CronTrigger(triggerName,triggerGroupName);
System.out.println("Trigger:" + trigger);
try {
((CronTrigger) trigger).setCronExpression(str);
sched.scheduleJob(job, trigger);
System.out.println("job:" + job);
sched.start();
} catch (Exception e) {
myLogger.error("开启一个任务"+jobName+e.getMessage());
e.printStackTrace();
}
}
/**
* 移除一个任务
* @param jobName: job名称
* @param jobGroupName: job组名称
* @param triggerName: trigger名称
* @param triggerGroupName: trigger组名称
* @throws SchedulerException
*/
public static void removeJob(String jobName,String jobGroupName,
String triggerName,String triggerGroupName) {
try {
InitialContext ctx = new InitialContext();
sched = (Scheduler) ctx.lookup("Quartz");
} catch (NamingException e) {
e.printStackTrace();
}
try {
//停止触发器
sched.pauseTrigger(triggerName,triggerGroupName);
//移除触发器
sched.unscheduleJob(triggerName,triggerGroupName);
//删除任务
sched.deleteJob(jobName,jobGroupName);
} catch (SchedulerException e) {
myLogger.info("移除一个任务"+jobName+e.getMessage());
}
}
}