webService接口的开发实例job调度功能(工作记录以后参考和改进):
项目A中的JOB 调度项目B中的JOB(用同一个quratz用户),启动两个项目的JOB
项目A(客服端):CommonJob.class
package com.x.x.common.job;
import java.net.MalformedURLException;
import java.net.URL;
import org.codehaus.xfire.client.Client;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import com.x.x.common.util.StaticUtils;
public class CommonJob implements Job {
private Client client = null;
public Client getClient(){
if( client == null ) {
try {
System.out.println("客服端中对smct_job 调度开始建立会话之前查询调度地址URl!");
//String smctJobInvokeUrl ="http://10.109.4.230:8080/smct/services/jobInterfaces?wsdl";
//String smctJobInvokeUrl ="http://10.108.226.193:8080/smct/services/jobInterfaces?wsdl";
String smctJobInvokeUrl = StaticUtils.getSmctJobInvokeWsdlURL();//可以配置在perporties文件中/数据库中
System.out.println("smctJobInvokeUrl:"+smctJobInvokeUrl);
System.out.println("客服端中对smct_job 调度开始建立会话之前查询调度地址为:!"+smctJobInvokeUrl);
client = new Client( new URL(smctJobInvokeUrl) );
client.setTimeout( 5000 );
System.out.println("客服端中对smct_job 调度成功建立会话!");
} catch (MalformedURLException e) {
e.printStackTrace();
System.out.println("客服端中对smct_job 调度建立会话失败!");
} catch (Exception e) {
e.printStackTrace();
System.out.println("客服端中对smct_job 调度建立会话失败!");
}
}
return client;
}
public void execute(JobExecutionContext context) throws JobExecutionException {
String result = "";
try {
JobDataMap jobDataMap = context.getMergedJobDataMap();
String clazz = jobDataMap.getString("clazz");
System.out.println("客服端对 SMCT JOB 使用类 方法"+clazz+": invokeMethodAutoExcute 调度结束:"+result);
Object[] objs = getClient().invoke("invokeMethodAutoExcute", new String[]{clazz});
if( objs != null && objs.length == 1 ){
result = objs[0].toString();
}
System.out.println("客服端对 SMCT JOB 使用类 方法: invokeMethodAutoExcute 调度结束:"+result);
} catch (Exception e) {
System.out.println("客服端对 SMCT JOB 使用类: invokeMethodAutoExcute 调度失败:"+result);
e.printStackTrace();
}
}
}
项目A中配置job类Action method:
public void addAutoJob(HttpServletRequest request, HttpServletResponse response) throws RemoteException,Exception{
response.setContentType("text/json;charset=GBK");
String jobname = request.getParameter("jobName");
String jobgroup = request.getParameter("jobGroup");
String trigname = request.getParameter("triggerName");
String triggroup = request.getParameter("triggerGroup");
String clazz = request.getParameter("className");
String expression = request.getParameter("CronExpression");
JSONObject json = new JSONObject();
try {
ServiceManager.getSession().startTransaction();
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();
JobDetail jobDetail =
new JobDetail (jobName,jobGroup,com.x.x.common.job.CommonJob.class);
jobDetail.getJobDataMap().put("clazz", clazz);
CronTrigger cronTrigger =
new CronTrigger(trigName, trigGroup, jobName, jobGroup, expression,TimeZone.getTimeZone( "Asia/Shanghai" ));
sched.addJob(jobDetail, true);
sched.scheduleJob(cronTrigger);
sched.start();
ServiceManager.getSession().commitTransaction();
json.put("retVal", "Y");
json.put("retMsg", "设置调度成功");
} catch (Exception e) {
e.printStackTrace();
try {
ServiceManager.getSession().rollbackTransaction();
} catch (Exception e2) {
} finally {
json.put("retVal", "N");
json.put("retMsg", "设置调度失败");
}
}
response.getWriter().print(json.toString());
}
项目B接口函数(服务器端):
public class ParseWebserviceSVImpl implements IParseWebserviceSV {
public String invokeMethodAutoExcute(String clazz) {
String flag = "failure";
try {
System.out.println("============smct_JOB webService Start=================");
System.out.println(" 进入接口类ParseWebserviceSVImpl对 smct_JOB 调度类反射,创建job实例开始.");
System.out.println(" 进入接口类ParseWebserviceSVImpl对 smct_JOB 调度类反射,创建job实例开始.");
Job job = (Job)Class.forName(clazz).newInstance();
System.out.println(" 进入接口类ParseWebserviceSVImpl对 smct_JOB 调度类反射,创建job实例结束.");
System.out.println(" 进入接口类ParseWebserviceSVImpl对 smct_JOB 调度开始执行开始!");
job.execute( null );
flag = "success";
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("接口类ParseWebserviceSVImpl对 smct_JOB 调度开始执行结果标示符('failure'或者'success')为:"+flag);
System.out.println("=========smct_JOB webService end=====================");
return flag;
}
}
项目B配置文件:1、引入xfire插件
2、services.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>
<name>jobInterfaces</name>
<namespace>com.asiainfo.bomc.smct</namespace>
<serviceClass>
com.asiainfo.smct.connector.webservices.interfaces.IParseWebserviceSV
</serviceClass>
<implementationClass>
com.asiainfo.smct.connector.webservices.impl.ParseWebserviceSVImpl
</implementationClass>
</service>
</beans>
项目B中被调度的job,按照普通的job写。