Calling the kettle job in Java and building the Web service interface

Source: Internet
Author: User

The first step: (If you copy all the jar packages under Lib in Kett to the Java Project Lib directory) to create and connect to the repository, if you only use this one workspace, you can put this code into a static code block, the code is as follows:

Kettleenvironment.init ();
Creates a resource pool object, at which point the object is still an empty object
Kettledatabaserepository repository = new Kettledatabaserepository ();
Create a repository database object, like we create a repository in spoon
(Database connection name, database type, connection method, IP, database name, port, user name, password)
Databasemeta Datameta = new Databasemeta ("Database connection name", "database type (such as MSSQL)", "connection method (such as native (JDBC)", "IP", "Database name", "Port number",

"User name", "password");
Resource library meta-object, select resource Pool (ID, name, description)
Kettledatabaserepositorymeta Kettledatabasemeta =new Kettledatabaserepositorymeta ("Resource pool ID", "Repository name", "Here is description information", Datameta);
Assigning a value to a resource pool
Repository.init (Kettledatabasemeta);
Connect to a resource library
Repository.connect ("admin", "admin");

Step two: Start and run the job:

Directoryinterface = Repository.loadrepositorydirectorytree ();
Jobmeta Jobmeta = Repository.loadjob (jobname,directoryinterface,null,null);//Load a job,jobname:job name from the repository
Job Job = new Job (Repository,jobmeta);

Job.start ();//Start job, default is thread execution.
Job.waituntilfinished ();//wait for job execution to end

Step three: Get the job status or stop the job:

Job.stopall ();//Stop Job
String status=job.getstatus ();//Gets the running state.

At this point, the basic functionality of the Java call job has been implemented, if the need is now to generate a Web service interface, provide four methods for external access, respectively, to pass in the string job name and execute the job (such as "JOBA,JOBB,JOBC"), Pass in a job name get the current state of the job, pass in a job name stop running the job, share the code as follows:

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 ();
Creates a resource pool object, at which point the object is still an empty object
Kettledatabaserepository repository = new Kettledatabaserepository ();
Create a repository database object, like we create a repository in spoon
(Database connection name, database type, connection method, IP, database name, port, user name, password)
Databasemeta Datameta =new

Databasemeta ("Database connection name", "database type (such as MSSQL)", "connection method (e.g. native (JDBC)", "IP", "Database name", "Port number",

"User name", "password");

Resource library meta-object, select resource Pool (ID, name, description)
Kettledatabaserepositorymeta Kettledatabasemeta =new Kettledatabaserepositorymeta ("Resource pool ID", "Repository name", "Here is description information", Datameta);
Assigning a value to a resource pool
Repository.init (Kettledatabasemeta);
Connect to a resource library
Repository.connect ("admin", "admin");
Testoajobx.repository=repository;
} catch (Kettleexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}

public void Setjod (String jobnames) {
Resource Pool Directory
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);//Load a job from the repository
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 ("First" + (i+1) + "job:" +tmpname[i]+ "Start Execution");
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 ("First" + (i+1) + "job:" +tmpname[i]+ "execution Complete");
if (Jobs.get (i). GetErrors () >0) {
System.out.println ("+ (i+1) +" job: "+tmpname[i]+" Execution failed! ");
}else{
System.out.println ("+ (i+1) +" job: "+tmpname[i]+" executed successfully! ");
}
}
}
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) +" Item Job: "+jobname+" Stop exception! ");
}else{
System.out.println ("+ (m+1) +" job: "+jobname+" Stop successful! ");
}
}else{
System.out.println ("The job is not running");
}
}
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 ("The job is not running");
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 ();
}
}
}

And then build the interface on the line.

Now there is a problem to be solved, that is, the job's start, run, and Execute methods can perform the job, but it cannot be executed the second time after using Job.start, but run and execute can

Calling the kettle job in Java and building the Web service interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.