Programming an Oracle Tuxedo Application Using Java

     Previous  Next    Open TOC in new window    View as PDF - New Window  Get Adobe Reader - New Window
Content starts here

Implementing Services in Oracle Tuxedo Java Server

This topic includes the following sections:

 


Typical Procedures

Typical steps of implementing the services in Oracle Tuxedo Java server are as follows.

  1. Define a class that inherits from TuxedoJavaServer
  2. Provide a default constructor
  3. Implement the tpsvrinit() and tpsvrdone() method
  4. Implement the service method which should use TPSVCINFO as its only argument parameter, as follows:
    1. Get the TuxAppContext object using getTuxAppContext() method
    2. Get the client request data using TPSVCINFO.getServiceData() method from TPSVCINFO object
    3. If you have configured a DataSource, get a connection to the DataSource using TuxAppContext.getConnection() method
    4. Do the business logic, such as call some other services using TuxAppContext.tpcall(), manipulate the database, etc.
    5. Allocate a new TypedBuffer and put a reply data in the TypedBuffer
    6. Call TuxAppContext.tpreturn() to return the reply data to client

 


Example: Implementing Java Service without Transaction

Following is a simple example that implements the TOUPPER service. It includes three steps:

  1. Defining Java Classes: Listing 4-1
  2. Creating Java Server Configuration File: Listing 4-2
  3. Updating UBB Configuration File: Listing 4-3

Defining Java Classes

Listing 4-1 Java Class Definition
import weblogic.wtc.jatmi.TypedBuffer;
import weblogic.wtc.jatmi.TypedString;
import com.oracle.tuxedo.tjatmi.*;
public class MyTuxedoJavaServer extends TuxedoJavaServer {
    public MyTuxedoJavaServer()
    {
        return;
    }
    public int tpsvrinit() throws TuxException
    {
        System.out.println("MyTuxedoJavaServer.tpsvrinit()");
        return 0;
    }
    public void tpsvrdone()
    {
        System.out.println("MyTuxedoJavaServer.tpsvrdone()");
        return;
    }
    public void JAVATOUPPER(TPSVCINFO rqst) throws TuxException {
        TypedBuffer svcData;
        TuxAppContext   myAppCtxt = null;
        TuxATMIReply    myTuxReply = null;
        TypedBuffer     replyTb = null;
        /* Get TuxAppContext first */
        myAppCtxt = getTuxAppContext();
        svcData = rqst.getServiceData();
        TypedString TbString = (TypedString)svcData;
        myAppCtxt.userlog("Handling in JAVATOUPPER()");
        myAppCtxt.userlog("Received string is:" + TbString.toString());
        String newStr = TbString.toString();
        newStr = newStr.toUpperCase();
        TypedString replyTbString = new TypedString(newStr);
        /* Return new string to client */
        myAppCtxt.tpreturn(TPSUCCESS, 0, replyTbString, 0);
    }
    public void JAVATOUPPERFORWARD(TPSVCINFO rqst) throws TuxException {
        TypedBuffer svcData;
        TuxAppContext   myAppCtxt = null;
        TuxATMIReply  myTuxReply = null;
        TypedBuffer replyTb = null;
        long flags = TPSIGRSTRT;
        /* Get TuxAppContext first */
        myAppCtxt = getTuxAppContext();
        svcData = rqst.getServiceData();
        TypedString TbString = (TypedString)svcData;
        myAppCtxt.userlog("Handling in JAVATOUPPERFORWARD()");
        myAppCtxt.userlog("Received string is:" + TbString.toString());
        /* Call another service "TOUPPER" which may be implemented by another Tuxedo Server */
        try {
            myTuxReply = myAppCtxt.tpcall("TOUPPER", svcData, flags);
            /* If success, get reply buffer */
            replyTb = myTuxReply.getReplyBuffer();
            TypedString replyTbStr = (TypedString)replyTb;
            myAppCtxt.userlog("Replied string from TOUPPER:" + replyTbStr.toString());
            /* Return the replied buffer to client */
            myAppCtxt.tpreturn(TPSUCCESS, 0, replyTb, 0);
        } catch (TuxATMITPReplyException    tre) {
            myAppCtxt.userlog("TuxATMITPReplyException:" + tre);
            myAppCtxt.tpreturn(TPFAIL, 0, null, 0);
        } catch (TuxATMITPException    te) {
            myAppCtxt.userlog("TuxATMITPException:" + te);
            myAppCtxt.tpreturn(TPFAIL, 0, null, 0);
        }
    }
}

Creating Java Server Configuration File

Listing 4-2 shows an configuration example that exports MyTuxedoJavaServer.JAVATOUPPER() method as Tuxedo service name JAVATOUPPER and MyTuxedoJavaServer.JAVATOUPPERFORWARD() method as Tuxedo service name JAVATOUPPERFORWARD.

Listing 4-2 Java Server Configuration File
<?xml version="1.0" encoding="UTF-8"?>
<TJSconfig>
    <TuxedoServerClasses>
        <TuxedoServerClass name="MyTuxedoJavaServer"></TuxedoServerClass>
    </TuxedoServerClasses>
</TJSconfig>

Updating UBB Configuration File

Listing 4-3 UBB Config File Configuration
*GROUPS 
TJSVRGRP  LMID=simple GRPNO=2
*SERVERS
TMJAVASVR SRVGRP= TJSVRGRP  SRVID=4	CLOPT="-- -c TJSconfig.xml"
	MINDISPATCHTHREADS=2 MAXDISPATCHTHREADS=2 

 


Example: Implementing Java Service with Transaction

Listing 4-4 shows an example that implements the WRITEDB_SVCTRN_COMMIT service which inserts the user request string into the table: TUXJ_TRAN_TEST.

It includes three steps:

  1. Defining Java Classes: Listing 4-4
  2. Creating Java Server Configuration File: Listing 4-5
  3. Updating UBB Configuration File: Listing 4-6

Defining Java Classes

Listing 4-4 Class Definition
import weblogic.wtc.jatmi.TypedBuffer;
import weblogic.wtc.jatmi.TypedString;
import com.oracle.tuxedo.tjatmi.*;
import java.sql.SQLException;
/* MyTuxedoTransactionServer is user defined class */
public class MyTuxedoTransactionServer extends TuxedoJavaServer{
    public MyTuxedoTransactionServer ()
    {
        return;
    }
    public int tpsvrinit() throws TuxException
    {
        System.out.println("In MyTuxedoTransactionServer.tpsvrinit()");
        return 0;
    }
    public void tpsvrdone()
    {
        System.out.println("In MyTuxedoTransactionServer.tpsvrdone()");
	 return;
    }
public void WRITEDB_SVCTRN_COMMIT(TPSVCINFO rqst) throws TuxException {
        TuxAppContext   myAppCtxt;
        TypedBuffer     rplyBuf = null;
        String          strType = "STRING";
        String          ulogMsg;
        TypedString     rqstMsg;
        Connection      connDB = null;
        Statement       stmtDB = null;
        String          stmtSQL;
        int             trnLvl, trnStrtInSVC;
        int             trnRtn;
        int             rc = TPSUCCESS;
 
        rqstMsg = (TypedString)rqst.getServiceData();
        myAppCtxt = getTuxAppContext();
        myAppCtxt.userlog("JAVA-INFO: Request Message Is \"" + rqstMsg.toString() + "\"");
        rplyBuf = new TypedString("This Is a Simple Transaction Test from Tuxedo Java Service");
        long trnFlags = 0;
        try {
            trnStrtInSVC = 0;
            trnLvl = myAppCtxt.tpgetlev();
            if (0 == trnLvl) {
                long trnTime = 6000;
                myAppCtxt.userlog("JAVA-INFO: Start a transaction...");
                trnRtn = myAppCtxt.tpbegin(trnTime, trnFlags);
                myAppCtxt.userlog("JAVA-INFO: tpbegin return " + trnRtn);
                trnStrtInSVC = 1;
            }
            connDB = myAppCtxt.getConnection();
            if (null != connDB) {
                myAppCtxt.userlog("JAVA-INFO: Get connection: (" +
                                  connDB.toString() + ").");
            }
            stmtDB = connDB.createStatement();
            if (null != stmtDB) {
                myAppCtxt.userlog("JAVA-INFO: Create statement: (" +
                                  stmtDB.toString() + ").");
            }
            stmtSQL = "INSERT INTO TUXJ_TRAN_TEST VALUES ('" +
                      rqstMsg.toString() + "')";
            myAppCtxt.userlog("JAVA-INFO: Start to execute sql (" + stmtSQL + ")...");
            stmtDB.execute(stmtSQL);
            myAppCtxt.userlog("JAVA-INFO: End to execute sql (" + stmtSQL + ").");
            if (1 == trnStrtInSVC) {
                    myAppCtxt.userlog("JAVA-INFO: tpcommit current transaction...");
                    trnRtn = myAppCtxt.tpcommit(trnFlags);
                    myAppCtxt.userlog("JAVA-INFO: tpcommit return " + trnRtn);
                    trnStrtInSVC = 0;
                    if (-1 == trnRtn ) {
                      rc = TPFAIL;
    }
            }
        } catch (TuxATMIRMException e) {
            String errMsg = "ERROR: TuxATMIRMException: (" + e.getMessage() + ").";
            myAppCtxt.userlog("JAVA-ERROR: " + errMsg); 
    rc = TPFAIL;
        } catch (TuxATMITPException e) {
            String errMsg = "ERROR: TuxATMITPException: (" + e.getMessage() + ").";
            myAppCtxt.userlog("JAVA-ERROR: " + errMsg);            
           rc = TPFAIL;
        } catch (SQLException e) {
            String errMsg = "ERROR: SQLException: (" + e.getMessage() + ").";
            myAppCtxt.userlog("JAVA-ERROR: " + errMsg);
           rc = TPFAIL;
        } catch (Exception e) {
            String errMsg = "ERROR: Exception: (" + e.getMessage() + ").";
            myAppCtxt.userlog("JAVA-ERROR: " + errMsg);
           rc = TPFAIL;
        } catch (Throwable e) {
            String errMsg = "ERROR: Throwable: (" + e.getMessage() + ").";
            myAppCtxt.userlog("JAVA-ERROR: " + errMsg);
            rc = TPFAIL;
	} finally {
            if (null != stmtDB) {
		try {
                stmtDB.close();
		} catch (SQLException e) {}
        }
	myAppCtxt.tpreturn(rc, 0, rplyBuf, 0);
    }
}

Creating Java Server Configuration File

Listing 4-5 Java Server Configuration File
<?xml version="1.0" encoding="UTF-8"?>
<TJSconfig>
     <ClassPaths>
        <ClassPath>/home/oracle/app/oracle/product/11.2.0/dbhome_2/ucp/lib/ucp.jar</ClassPath>
        <ClassPath>/home/oracle/app/oracle/product/11.2.0/dbhome_2/jdbc/lib/ojdbc6.jar</ClassPath>
     </ClassPaths>
        
        <DataSources>
           <DataSource name="oracle">
            <DriverClass>oracle.jdbc.xa.client.OracleXADataSource</DriverClass>
            <JdbcDriverParams>
                <ConnectionUrl>jdbc:oracle:thin:@//10.182.54.144:1521/javaorcl</ConnectionUrl>
            </JdbcDriverParams>
          </DataSource>
        </DataSources>			
	<TuxedoServerClasses>
   <TuxedoServerClass name=" MyTuxedoTransactionServer">
	   </TuxedoServerClass>
	</TuxedoServerClasses>
</TJSconfig> 

Updating UBB Configuration File

Listing 4-6 UBB Conf File Configuration
*GROUPS
ORASVRGRP  LMID=simple GRPNO=1
OPENINFO="Oracle_XA:Oracle_XA+Acc=P/scott/triger+SesTm=120+MaxCur=5+LogDir=.+SqlNet=javaorcl"
TMSNAME=TMSORA TMSCOUNT=2 
*SERVERS
TMJAVASVR SRVGRP=ORASVRGRP SRVID=3	
 	CLOPT="-- -c TJSconfig.xml"
	MINDISPATCHTHREADS=2 MAXDISPATCHTHREADS=4

  Back to Top       Previous  Next