第一步:(前提将kett中lib下的所有jar包拷贝到java项目lib目录)创建并连接资源库,如果只用这一个工作空间,可以将此段代码放入静态代码块,代码如下:
KettleEnvironment.init();
//创建资源库对象,此时的对象还是一个空对象
KettleDatabaseRepository repository = new KettleDatabaseRepository();
//创建资源库数据库对象,类似我们在spoon里面创建资源库
//(数据库连接名称,数据库类型,连接方式,IP,数据库名,端口,用户名,密码)
DatabaseMeta dataMeta = new DatabaseMeta("数据库连接名","数据库类型(比如MSSQL)","连接方式(例如Native(JDBC))","IP","数据库名","端口号", "用户名","密码");
//资源库元对象,选择资源库(ID,名称,描述)
KettleDatabaseRepositoryMeta kettleDatabaseMeta =new KettleDatabaseRepositoryMeta("资源库ID", "资源库名称", "这里是描述信息",dataMeta);
//给资源库赋值
repository.init(kettleDatabaseMeta);
//连接资源库
repository.connect("admin","admin");第二步:启动并运行job:
directoryInterface = repository.loadRepositoryDirectoryTree();
JobMeta jobMeta = repository.loadJob(jobName,directoryInterface,null,null);//从资源库中加载一个job,jobName:job名
Job job = new Job(repository,jobMeta);job.start();//启动job,默认是线程执行。
job.waitUntilFinished();//等待job执行结束第三步:获取job状态或者停止job:
job.stopAll();//停止job
String status=job.getStatus();//获取运行状态。至此,java调用job的基本功能已经实现,如果现在有需求是生成web service接口,提供四个方法供外界访问,分别是传入串job名并分别执行这些job(比如“jobA,jobB,JobC”)、传入一个job名获取这个job 的当前状态、传入一个job名停止运行这个job,分享代码如下:
package com.test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;import org.pentaho.di.core.KettleEnvironment;
import org.pentaho.di.core.database.DatabaseMeta;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.job.Job;
import org.pentaho.di.job.JobMeta;
import org.pentaho.di.repository.Repository;
import org.pentaho.di.repository.RepositoryDirectoryInterface;
import org.pentaho.di.repository.kdr.KettleDatabaseRepository;
import org.pentaho.di.repository.kdr.KettleDatabaseRepositoryMeta;import com.util.DBUtil;
public class TestOaJobX {
private static Connection con = null;
private static PreparedStatement pst = null;
private static ResultSet rs = null;
private static Repository repository;
private static List<Job> jobs;
private static String[] jobNameArray;
public TestOaJobX() {
// TODO Auto-generated constructor stub
}
static{
try {
KettleEnvironment.init();
//创建资源库对象,此时的对象还是一个空对象
KettleDatabaseRepository repository = new KettleDatabaseRepository();
//创建资源库数据库对象,类似我们在spoon里面创建资源库
//(数据库连接名称,数据库类型,连接方式,IP,数据库名,端口,用户名,密码)
DatabaseMeta dataMeta =newDatabaseMeta("数据库连接名","数据库类型(比如MSSQL)","连接方式(例如Native(JDBC))","IP","数据库名","端口号",
"用户名","密码");
//资源库元对象,选择资源库(ID,名称,描述)
KettleDatabaseRepositoryMeta kettleDatabaseMeta =new KettleDatabaseRepositoryMeta("资源库ID", "资源库名称", "这里是描述信息",dataMeta);
//给资源库赋值
repository.init(kettleDatabaseMeta);
//连接资源库
repository.connect("admin","admin");
TestOaJobX.repository=repository;
} catch (KettleException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void setJod(String jobNames) {
//资源库目录
RepositoryDirectoryInterface directoryInterface;
try {
if(TestOaJobX.jobs==null){
directoryInterface = repository.loadRepositoryDirectoryTree();
jobNameArray=jobNames.split(",");
List<JobMeta> jobmetalist=new ArrayList<JobMeta>();
for(int i=0;i<jobNameArray.length;i++){
JobMeta jobMeta = repository.loadJob(jobNameArray[i],directoryInterface,null,null);//从资源库中加载一个job
jobmetalist.add(jobMeta);
}
TestOaJobX.jobs=new ArrayList<Job>();
for(int i=0;i<jobmetalist.size();i++){
Job job = new Job(repository,jobmetalist.get(i));
TestOaJobX.jobs.add(job);
}
}
} catch (KettleException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void runJob(String jobNames){
setJod(jobNames);
String[] tmpname=jobNames.split(",");
for(int i=0;i<jobs.size();i++){
System.out.println("第"+(i+1)+"项作业:"+tmpname[i]+" 开始执行");
//jobs.get(i).start();
//jobs.get(i).run();
try {
jobs.get(i).execute(i, null);
} catch (KettleException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
jobs.get(i).waitUntilFinished();
System.out.println("第"+(i+1)+"项作业:"+tmpname[i]+" 执行完毕");
if(jobs.get(i).getErrors()>0){
System.out.println("第"+(i+1)+"项作业:"+tmpname[i]+" 执行失败!");
}else{
System.out.println("第"+(i+1)+"项作业:"+tmpname[i]+" 执行成功!");
}
}
}
public void stopJob(String jobName) {
if(jobNameArray!=null){
int m=0;
for(int i=0;i<jobNameArray.length;i++){
if(jobName.equals(jobNameArray[i])){
m=i;
}
}
jobs.get(m).stopAll();
if(jobs.get(m).getErrors()>0){
System.out.println("第"+(m+1)+"项作业:"+jobName+" 停止异常!");
}else{
System.out.println("第"+(m+1)+"项作业:"+jobName+" 停止成功!");
}
}else{
System.out.println("该作业未运行");
}
}
public String getStatus(String jobName) {
if(jobNameArray!=null){
int m=0;
for(int i=0;i<jobNameArray.length;i++){
if(jobName.equals(jobNameArray[i])){
m=i;
}
}
String status=jobs.get(m).getStatus();
return status;
}else{
System.out.println("该作业未运行");
return null;
}
}
public void errorView(String jobName) {
con=DBUtil.getConnection();
String sql="select * from ERROR_LOG where KJBNAME = ? order by ERROETIME desc";
try {
pst=con.prepareStatement(sql);
pst.setString(1,jobName);
rs=pst.executeQuery();
if(rs!=null){
while(rs.next()){
System.out.println(rs.getString("KJBNAME")+" "+
rs.getString("KTRNAME")+" "+
rs.getString("ERRORINFO")+" "+
rs.getString("ERROETIME"));
}
}
pst.close();
rs.close();
con.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
然后生成接口就行了。
现在有一个问题有待于解决,就是job的start、run、和execute方法都可以执行job,但是用job.start后不能第二次执行这个方法,但是run和execute可以