Sunday, 19 January 2014

How to execute ODI 11g Scenario from Java

Hi all,
in this post I want to explain how to run an ODI 11g scenario from Java. To be honest, I had try only with ODI version 11g and not with previously versions.

For this implementation we need of some knowledges of Java and Eclipse develompment tool.

It's amazing how many things you can do with the interaction between Java and ODI.

Well, the steps for this development are:

  1. Create a Java project
  2. Import the necessary library for connect to ODI SDK
  3. Write the sample code for call an ODI scenario
  4. Export your Java project such as JAR file
  5. Understand what it could do the ODI project

Ok, we analize step by step.

1) 
For create a Java project, I have used the Eclipse tool that you can find hereI use the version of Eclipse IDE for Java EE Developers, but the classic version is also good.

After you downloaded and installed the IDE you must create a new project. Select "File > New > Project" and from the window wizard select "Java" folder and then "Java Project". Enter the name of java project and others information that the wizard require. Click "Finish" button when you have complete.


Create Java Project




2)
The second step require to import the necessary SDK library for connect to ODI and execute the ODI scenario that will develop further.

In Eclipse IDE, expand the project node in Package Explorer pane and create a new folder named "lib" (Right-click on project > New > Folder).




For your convenience, copy in this folder all the files with jar extensions for ODI-SDK that you can find on your ODI installation path

../oracledi.sdk/lib

For the documentation, you can also find the Oracle Data Integrator Java API Reference 11g - Release1 (11.1.1.6.0) here.

After this, you must to reference this library into our project. Right-click on "lib" folder and select "Build Path > Configure Build Path".




Select "Add External JARs" and get all jar files that you have copied in the lib folder.




3) 
Now the core of this post. You must to write the code for call an ODI scenario. See the example below and check where you must change the value for the parameters.

The project is compose from two classes:

- OdiMain (main class)
- OdiInvokeScenario (class that invoke a scenario)

In the "OdiMain" class are defined all variables that you need setup their value for call a scenario ODI. All these variables are passed to "OdiInvokeScenario" class, that actually call a scenario ODI.

/* Main class */

import oracle.odi.runtime.agent.invocation.InvocationException;
import oracle.odi.runtime.agent.invocation.StartupParams;
import odi.sdk.*;

public class OdiMain {

    public static void main(String[] args) throws InvocationException {
       
        //SET PARAMETER HERE
        String odiHost = "http://yourmachine:port/oraclediagent";
        String odiUser = "";  //INSERT ODI USER
        String odiPassword = "";  //INSERT ODI PASSWORD
        String odiScenName =  ""; //INSERT NAME OF SCENARIO ODI
        String odiScenVersion = ""; //INSERT VERSION OF SCENARIO ODI
       
        StartupParams odiStartupParams = new StartupParams();
        

        //IF YOU HAVE PARAMETERS FOR SCENARIO ODI INSERT HERE
        odiStartupParams = null;
        String odiKeywords = null;

 
       
//INSERT THE CONTEXT FOR SCENARIO ODI "GLOBAL" SUCH AS DEFAULT
        String odiContextCode = "";
        int odiLogLevel = 5; //INSERT LOG LEVEL
        String odiSessionName = null; 
        boolean odiSynchronous = true; //TRUE FOR SYNC MODE, FALSE ASYNC MODE
        String odiWorkRepName = ""; //INSERT NAME OF YOUR WORK REPOSITORY
       
        OdiInvokeScenario call = new OdiInvokeScenario();

        
        call.runScenario(odiHost,odiUser,odiPassword,odiScenName,odiScenVersion,
                         odiStartupParams,odiKeywords,odiContextCode,
                         odiLogLevel,odiSessionName,odiSynchronous,
                         odiWorkRepName);
    }
}


/*  OdiInvokeScenario class  */

import oracle.odi.runtime.agent.invocation.ExecutionInfo;
import oracle.odi.runtime.agent.invocation.InvocationException;
import oracle.odi.runtime.agent.invocation.RemoteRuntimeAgentInvoker;
import oracle.odi.runtime.agent.invocation.StartupParams;

public class OdiInvokeScenario {

    public void runScenario(String odiHost,String odiUser,String odiPassword,

                            String odiScenName,String odiScenVersion,
                            StartupParams odiStartupParams,String odiKeywords,
                            String odiContextCode,int odiLogLevel,
                            String odiSessionName,boolean odiSynchronous,
                            String odiWorkRepName) throws InvocationException {
        try{
       
            //Starting the session on the remote agent.
            RemoteRuntimeAgentInvoker remoteRuntimeAgentInvoker = 

            new RemoteRuntimeAgentInvoker(odiHost,odiUser,
                                          odiPassword.toCharArray());
           
            //Parameter: 

            //pScenName,pScenVersion,pVariables,pKeywords,
            //pContextCode, pLogLevel, pSessionName, pSynchronous, pWorkRepName
            //pVariables ==> startupParams = new StartupParams();

           
            ExecutionInfo exeInfo =
                    remoteRuntimeAgentInvoker.invokeStartScenario(                                         odiScenName,odiScenVersion,odiStartupParams,

                           odiKeywords,odiContextCode,odiLogLevel,
                           odiSessionName,odiSynchronous,odiWorkRepName);
           
           
            //Retrieve the session ID.
            System.out.println(

              "-----------------------------------" + "\n" + 
              "Scenario Started in Session : " + exeInfo.getSessionId() + "\n" +
              "Scenario Session Status : " + exeInfo.getSessionStatus() + "\n" +
              "-----------------------------------" );
        }
        catch (Exception e)    {
            e.printStackTrace();
        }
    }
}


You can copy and paste the code in your environment. If there are some errors, check and resolve it.

4) 
After you complete the java project and test it, you can export the project such as jar file. The jar file is an "Java ARchive" file that allow you execute your project calling this file. You can also to call the file from a shell prompt Windows or Unix/Linux or from a dashboard or other again.

For create your jar file select  "File > Export"


Export project for create a jar file
In the export wizard, open the Java folder and select "Runnable JAR file". Enter the name of JAR file and the path where you want to save it. 


5) 
Finally, if you have just the ODI project developed, you can try now your implementation respect to this post. Conversely, if you must again to develop your ODI project you must understand that you can potentially run your ODI project, throught ODI scenario, maybe from everywhere recalling only your jar file.

Ok guys, for this post is all. I hope that you appreciate that and now you are happy.
Enjoy and keep in touch.

No comments:

Post a Comment