quart动态定时

<bean id="simple" lazy-init="false" class="com.ces.interfaceSchdule.utils.InterfaceJobTrigger" init-method="run" >
</bean>




trigger:

/*
* Copyright 2005 - 2009 Terracotta, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*  
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
*/

package com.ces.interfaceSchdule.utils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.TriggerUtils;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ces.zwww.entity.ScheduleInfo;
import com.ces.zwww.service.ScheduleInfoLogService;
import com.ces.zwww.service.ScheduleInfoService;

/**
* This Example will demonstrate all of the basics of scheduling capabilities of
* Quartz using Simple Triggers.
*
* @author Bill Kratzer
*/
@Component
public class InterfaceJobTrigger {

@Autowired
private ScheduleInfoService shser;

@Autowired
private ScheduleInfoLogService shserver;

@Autowired
private InterfaceJob simpleJob;

public void run() throws Exception {
Logger log = LoggerFactory.getLogger(InterfaceJobTrigger.class);
List<ScheduleInfo> shIn = new ArrayList<ScheduleInfo>();
shIn = shser.getScheduleInfo();

//First we must get a reference to a scheduler
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();
long ts = 0;

ts = TriggerUtils.getNextGivenSecondDate(null, 15).getTime();

for (int i = 0; i < shIn.size(); i++) {
int zt =Integer.parseInt(shIn.get(i).getCfqzt());
if(zt==1){
String jobName = shIn.get(i).getRwmc();
String jobgroup = shIn.get(i).getRwfz();
String triggerName = shIn.get(i).getCfqmc();
String triggergroup = shIn.get(i).getCfqmc();

Long pl = Long.parseLong(shIn.get(i).getDspl());
JobDetail job = new JobDetail(jobName, jobgroup, simpleJob.getClass());
JobDataMap jobDataMap = new JobDataMap();
jobDataMap.put("interfaceName", shIn.get(i).getInterfacename());
jobDataMap.put("interfaceMethod", shIn.get(i).getMethod());
job.setJobDataMap(jobDataMap);

SimpleTrigger trigger = new SimpleTrigger(triggerName,
triggergroup, jobName, jobgroup, new Date(ts), null,
SimpleTrigger.REPEAT_INDEFINITELY, pl * 1000);
// CronTrigger trigger = new CronTrigger(triggerName,triggergroup,jobName
// ,jobgroup,new Date(ts),null,shIn.get(i).getDspl());
Date ft = sched.scheduleJob(job, trigger);
log.info(job.getFullName() + " will run at: " + ft
+ " and CronExpression: " + trigger.getJobName()
+ " Name " + trigger.getName());
log.info("job7 rescheduled to run at: " + ft);
sched.start();
// ScheduleInfoLog schLog = new ScheduleInfoLog();
// schLog.setId(i+5+"");
// schLog.setRwid(shIn.get(i));
// shserver.save(schLog);
}
}
}


}



job:



/*
* Copyright 2005 - 2009 Terracotta, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*  
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
*/

package com.ces.interfaceSchdule.utils;

import java.lang.reflect.Method;

import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
* <p>
* This is just a simple job that gets fired off many times by example 1
* </p>
*
* @author Bill Kratzer
*/
@Component
public class InterfaceJob implements Job {

private static Logger _log = LoggerFactory.getLogger(InterfaceJob.class);

private String interfaceName;
private String interfaceMethod;

public InterfaceJob() {

}

/**
* <p>
* Called by the <code>{@link org.quartz.Scheduler}</code> when a
* <code>{@link org.quartz.Trigger}</code> fires that is associated with the
* <code>Job</code>.
* </p>
*
* @throws JobExecutionException
*             if there is an exception while executing the job.
*/
public void execute(JobExecutionContext context)
throws JobExecutionException {
//
setJobParam(context);
// This job simply prints out its job name and the
// date and time that it is running
try {
WebApplicationContext ctx = ContextLoader
.getCurrentWebApplicationContext();
Object object = WebApplicationContextUtils
.getWebApplicationContext(ctx.getServletContext()).getBean(
getInterfaceClass());
if(getInterfaceMethod()!=null&&object!=null)
getInterfaceMethod().invoke(object);

// netManagerTaskService.getTicketMsg();
} catch (Exception e) {
e.printStackTrace();
}

}

/**
     *
     */
private void setJobParam(JobExecutionContext context) {
JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
interfaceName = jobDataMap.getString("interfaceName");
interfaceMethod = jobDataMap.getString("interfaceMethod");
}

/**
* 获取当前参数的类
*
* @return
*/
private Class getInterfaceClass() {
try {
return Class.forName(interfaceName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}

/**
*
* @return
*/
private Method getInterfaceMethod() {
try {
return getInterfaceClass().getMethod(interfaceMethod);
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
return null;
}

}

猜你喜欢

转载自yuhuiblog6338999322098842.iteye.com/blog/2108891