SAP JCo Server Programming.doc

14
SAP JCo Server Programming The following section explains how you can write your own JCo programs for servers if you use the standalone version of the SAP JCo. A JCo server program implements functions that are called up by an RFC client. The JCo server program is registered via the SAP Gateway and waits for inbound RFC calls. An RFC server program registers itself under a program ID to an SAP gateway (not for a specific SAP system). If an RFC call is passed on from any SAP system to this SAP gateway with the option “Connection with a registered program” (with the same program ID), the connection takes place with the corresponding JCo server program. Once an RFC function has been executed, the JCo Server waits for further RFC calls from the same or other SAP systems. If an RFC connection is interrupted or terminated, the JCo server automatically registers itself again on the same SAP gateway under the same program ID. Prerequisites You are using the standalone version of the SAP JCo. Using transaction SM59, you have defined a destination with connection type T (TCP/IP connection) in the SAP system. You have chosen the registration mode (“Registered server program” option under the “Technical settings” tab page) for this destination. The destination contains the required information about the SAP gateway and the registered RFC server program. 1

Transcript of SAP JCo Server Programming.doc

Page 1: SAP JCo Server Programming.doc

SAP JCo Server Programming 

The following section explains how you can write your own JCo programs for servers if you use the standalone version of the SAP JCo.

A JCo server program implements functions that are called up by an RFC client. The JCo server program is registered via the SAP Gateway and waits for inbound RFC calls.

        An RFC server program registers itself under a program ID to an SAP gateway (not for a specific SAP system).

        If an RFC call is passed on from any SAP system to this SAP gateway with the option “Connection with a registered program” (with the same program ID), the connection takes place with the corresponding JCo server program.

        Once an RFC function has been executed, the JCo Server waits for further RFC calls from the same or other SAP systems.

        If an RFC connection is interrupted or terminated, the JCo server automatically registers itself again on the same SAP gateway under the same program ID.

Prerequisites

        You are using the standalone version of the SAP JCo.

        Using transaction SM59, you have defined a destination with connection type T (TCP/IP connection) in the SAP system.

        You have chosen the registration mode (“Registered server program” option under the “Technical settings” tab page) for this destination.

        The destination contains the required information about the SAP gateway and the registered RFC server program.

Additional Information

The following sections provide examples of how to program various JCo server functions:

        Establishing an RDC Connection (Inbound Call from the SAP System)

        Java Program for Establishing a Server Connection

        Exception Listener

        Server State Change Listener

        Processing an ABAP Call

1

Page 2: SAP JCo Server Programming.doc

Inbound RFC Connection (from the SAP System) 

This section provides an example of how you can establish a server-side RFC connection that originates from the SAP system.

To send a call from an ABAP system, the ABAP program uses the option DESTINATION "NAME" for the command CALL FUNCTION.

 

In transaction SE38, create a report with the following coding:

2

Page 3: SAP JCo Server Programming.doc

 

DATA: REQUTEXT LIKE SY-LISEL,

      RESPTEXT LIKE SY-LISEL,

      ECHOTEXT LIKE SY-LISEL.

 

DATA: RFCDEST like rfcdes-rfcdest VALUE 'NONE'.

DATA: RFC_MESS(128).

 

REQUTEXT = 'HELLO WORLD'.

RFCDEST = 'JCOSERVER01'. "corresponds to the destination name defined in the SM59

 

CALL FUNCTION 'STFC_CONNECTION'

   DESTINATION RFCDEST

   EXPORTING

     REQUTEXT = REQUTEXT

   IMPORTING

     RESPTEXT = RESPTEXT

     ECHOTEXT = ECHOTEXT

   EXCEPTIONS

     SYSTEM_FAILURE        = 1 MESSAGE RFC_MESS

     COMMUNICATION_FAILURE = 2 MESSAGE RFC_MESS.

 

IF SY-SUBRC NE 0.

    WRITE: / 'Call STFC_CONNECTION         SY-SUBRC = ', SY-SUBRC.

    WRITE: / RFC_MESS.

ENDIF.3

Page 4: SAP JCo Server Programming.doc

Java Program for Establishing a Server Connection 

Use

In the next step, you can write a Java program that establishes a server connection to a SAP gateway.

Procedure

To do this, you need to:

        extend the JCO.Server class

        implement a constructor that passes on the required parameters to the JCO.Server superclass such as Gateway Host, Gateway Service, Program ID and Repository.

        overwrite the protected void method handleRequest(JCO.Function function) and implement the coding that is executed when the call is received.

        create various instances for your JCO.Serverimplementation and start them with start().

Example

4

Page 5: SAP JCo Server Programming.doc

public class MyFirstServer extends JCO.Server {

    /**

     *  Create an instance of my own server

     *  @param gwhost (gateway host)

     *  @param gwserv (gateway service number)

     *  @param progid (program id)

     *  @param repository (repository used by the server to lookup the

       definitions of an inc)

     */

    public MyFirstServer(String gwhost, String gwserv,

                         String progid, IRepository repository) {

               super(gwhost,gwserv,progid,repository);

    }

 

    /**

     *  Overrides the default method.

     */

    protected void handleRequest(JCO.Function function) {

        JCO.ParameterList input  = function.getImportParameterList();

        JCO.ParameterList output = function.getExportParameterList();

        JCO.ParameterList tables = function.getTableParameterList();

       

        System.out.println("handleRequest(" + function.getName() + ")");

        if (function.getName().equals("STFC_CONNECTION")) {

5

Page 6: SAP JCo Server Programming.doc

            System.out.println(">>> request STFC_CONNECTION: " + input.getString("REQUTEXT"));

            output.setValue(input.getString("REQUTEXT"),"ECHOTEXT");

            output.setValue("This is a response from MyFirstServer","RESPTEXT");

        }

    }

}

 

public class FirstExample {

    static MyFirstServer serverConnections[] = new MyFirstServer[3];

    /**

     *  Start the server

     */

    public static void startServers()  {

      JCO.addClientPool("POOL",  3, "000", "user" ,"password" , "EN",

                                     "abap_system" ,"00");

       IRepository repository = JCO.createRepository("REP", "POOL");

       for(int i = 0; I < serverConnections.length; i++) {

           // Server listens for incoming requests from system 1

           // (Change gateway host, service, and program ID according to your

              needs)

           serverConnections [i] = new MyFirstServer

                             ("gwhost",  //gateway host, often the same as host

                              "sapgw00", //gateway service, generally sapgw+<SYSNR>

                              "JCOSERVER01", // corresponds to program ID defined in SM59

6

Page 7: SAP JCo Server Programming.doc

Additional Information

For a description of the Unicode connection, see:

        Server Connection to a Unicode Backend System

 

Exception Listener 

Whenever errors occur, the JCo throws an exception. All exceptions that occur in the JCo are passed on to the registered Exception and Error Listener.

The application must process the exceptions separately in the method handleRequest()(that is, the exceptions that it generates itself). Exceptions from the application coding are not passed on to the Listener.

To define a Listener, create a class that implements the JCO.ServerExceptionListener and JCO.ServerStateChangedListener:

7

Page 8: SAP JCo Server Programming.doc

public class MyExceptionAndErrorListener

implements JCO.ServerExceptionListener, JCO.ServerErrorListener {

 

  public void serverExceptionOccurred(JCO.Server server, Exception ex)

  {

    System.out.println("Exception in server " + server.getProgID() + ":\n" + ex);

    ex.printStackTrace();

  }

  public void serverErrorOccurred(JCO.Server server, Error er)

  {

    System.out.println("Error in server " + server.getProgID() + ":\n" + er);

    er.printStackTrace();

  }

}

Register the Listener class with JCO.addServerErrorListener and JCO.addServerExceptionListener:

public class SecondExample extends FirstExample {

    public static void main(String[] args) {

        MyExceptionAndErrorListener l = new MyExceptionAndErrorListener();

        JCO.addServerErrorListener(l);

        JCO.addServerExceptionListener(l);

        FirstExample.startServers() ;

    }

}

Server State Change Listener 

8

Page 9: SAP JCo Server Programming.doc

The JCO.ServerStateChangedListenerinterface enables you to monitor the server connection.

 public class MyServerStateChangedListener

{

  /**

   *  Simply prints server state changes

   */

  public void serverStateChangeOccurred(JCO.Server server, int old_state, int new_state)

  {

    System.out.print("Server " + server.getProgID() + " changed state from [");

    if ((old_state & JCO.STATE_STOPPED    ) != 0) System.out.print(" STOPPED ");

    if ((old_state & JCO.STATE_STARTED    ) != 0) System.out.print(" STARTED ");

    if ((old_state & JCO.STATE_LISTENING  ) != 0) System.out.print(" LISTENING ");

    if ((old_state & JCO.STATE_TRANSACTION) != 0) System.out.print(" TRANSACTION ");

    if ((old_state & JCO.STATE_BUSY       ) != 0) System.out.print(" BUSY ");

 

    System.out.print("] to [");

    if ((new_state & JCO.STATE_STOPPED    ) != 0) System.out.print(" STOPPED ");

    if ((new_state & JCO.STATE_STARTED    ) != 0) System.out.print(" STARTED ");

    if ((new_state & JCO.STATE_LISTENING  ) != 0) System.out.print(" LISTENING ");

    if ((new_state & JCO.STATE_TRANSACTION) != 0) System.out.print(" TRANSACTION ");

    if ((new_state & JCO.STATE_BUSY       ) != 0) System.out.print(" BUSY ");

    System.out.println("]");

  }

}

 

9

Page 10: SAP JCo Server Programming.doc

Register the Listener class with the API JCO.addServerStateChangedListener():

 

public class ThirdExample extends FirstExample {

    public static void main(String[] args) {

        MyExceptionAndErrorListener el = new MyExceptionAndErrorListener();

        JCO.addServerErrorListener(el);

        JCO.addServerExceptionListener(el);

        MyServerStateChangedListener sl = new MyServerStateChangedListener();

        JCO.addServerStateChangedListener(sl);

        FirstExample.startServers() ;

    }

}

 

 Processing an ABAP Call 

The class that extends the JCO.Server and is called up when an ABAP call is being processed, must overwrite the protected void method handleRequest(JCO.Function function).

The application that processes the call must quit call execution can only allow ABAP exceptions.

All other exceptions that are thrown by handleRequest are treated as system failures.

 

10

Page 11: SAP JCo Server Programming.doc

 protected void handleRequest(JCO.Function function)

    {

        try

        {

            //process the request

        }

        catch(Exception e)

        {

            throw new AbapException("a group of the exception", "text");

        }

    }

The exception and error listeners are not informed about the exception case. This takes place in handleRequest.

Additional Information

For a description of the call sequence for tRFC processing, see:

        Processing a tRFC Call

11