Show TOC

Example documentationExample Program JCo Server Locate this document in the navigation structure

 

The example below contains a complete JCo server program. It is made up of the code examples from the activities described in the previous sections:

  • Java Program for Creating a Server Connection

  • Exception Listener

  • Server State Changed Listener

  • Processing an ABAP Call

  • Processing a tRFC Call

    Syntax Syntax

    JCo Server Program

    1. import java.io.File;
    2. import java.io.FileOutputStream;
    3. import java.util.Hashtable;
    4. import java.util.Map;
    5. import java.util.Properties;
    6. import com.sap.conn.jco.JCoException;
    7. import com.sap.conn.jco.JCoFunction;
    8. import com.sap.conn.jco.ext.DestinationDataProvider;
    9. import com.sap.conn.jco.ext.ServerDataProvider;
    10. import com.sap.conn.jco.server.DefaultServerHandlerFactory;
    11. import com.sap.conn.jco.server.JCoServer;
    12. import com.sap.conn.jco.server.JCoServerContext;
    13. import com.sap.conn.jco.server.JCoServerErrorListener;
    14. import com.sap.conn.jco.server.JCoServerExceptionListener;
    15. import com.sap.conn.jco.server.JCoServerFactory;
    16. import com.sap.conn.jco.server.JCoServerFunctionHandler;
    17. import com.sap.conn.jco.server.JCoServerState;
    18. import com.sap.conn.jco.server.JCoServerStateChangedListener;
    19. import com.sap.conn.jco.server.JCoServerTIDHandler;
    20. public class StepByStepServer
    21. {
    22.     static String SERVER_NAME1 = "SERVER";
    23.     static String DESTINATION_NAME1 = "ABAP_AS_WITHOUT_POOL";
    24.     static String DESTINATION_NAME2 = "ABAP_AS_WITH_POOL";
    25.     static
    26.     {
    27.         Properties connectProperties = new Properties();
    28.         connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "ls4065");
    29.         connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR,  "85");
    30.         connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "800");
    31.         connectProperties.setProperty(DestinationDataProvider.JCO_USER,   "farber");
    32.         connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "laska");
    33.         connectProperties.setProperty(DestinationDataProvider.JCO_LANG,   "en");
    34.         createDataFile(DESTINATION_NAME1, "jcoDestination", connectProperties);
    35.         connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "3");
    36.         connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT,    "10");
    37.         createDataFile(DESTINATION_NAME2, "jcoDestination", connectProperties);
    38.         
    39.         Properties servertProperties = new Properties();
    40.         servertProperties.setProperty(ServerDataProvider.JCO_GWHOST, "binmain");
    41.         servertProperties.setProperty(ServerDataProvider.JCO_GWSERV, "sapgw53");
    42.         servertProperties.setProperty(ServerDataProvider.JCO_PROGID, "JCO_SERVER");
    43.         servertProperties.setProperty(ServerDataProvider.JCO_REP_DEST, "ABAP_AS_WITH_POOL");
    44.         servertProperties.setProperty(ServerDataProvider.JCO_CONNECTION_COUNT, "2");
    45.         createDataFile(SERVER_NAME1, "jcoServer", servertProperties);
    46.     }
    47.     
    48.     static void createDataFile(String name, String suffix, Properties properties)
    49.     {
    50.         File cfg = new File(name+"."+suffix);
    51.         if(!cfg.exists())
    52.         {
    53.             try
    54.             {
    55.                 FileOutputStream fos = new FileOutputStream(cfg, false);
    56.                 properties.store(fos, "for tests only !");
    57.                 fos.close();
    58.             }
    59.             catch (Exception e)
    60.             {
    61.                 throw new RuntimeException("Unable to create the destination file " + cfg.getName(), e);
    62.             }
    63.         }
    64.     }
    65.     
    66.     static class StfcConnectionHandler implements JCoServerFunctionHandler
    67.     {
    68.         public void handleRequest(JCoServerContext serverCtx, JCoFunction function)
    69.         {
    70.             System.out.println("----------------------------------------------------------------");
    71.             System.out.println("call              : " + function.getName());
    72.             System.out.println("ConnectionId      : " + serverCtx.getConnectionID());
    73.             System.out.println("SessionId         : " + serverCtx.getSessionID());
    74.             System.out.println("TID               : " + serverCtx.getTID());
    75.             System.out.println("repository name   : " + serverCtx.getRepository().getName());
    76.             System.out.println("is in transaction : " + serverCtx.isInTransaction());
    77.             System.out.println("is stateful       : " + serverCtx.isStatefulSession());
    78.             System.out.println("----------------------------------------------------------------");
    79.             System.out.println("gwhost: " + serverCtx.getServer().getGatewayHost());
    80.             System.out.println("gwserv: " + serverCtx.getServer().getGatewayService());
    81.             System.out.println("progid: " + serverCtx.getServer().getProgramID());
    82.             System.out.println("----------------------------------------------------------------");
    83.             System.out.println("attributes  : ");
    84.             System.out.println(serverCtx.getConnectionAttributes().toString());
    85.             System.out.println("----------------------------------------------------------------");
    86.             System.out.println("req text: " + function.getImportParameterList().getString("REQUTEXT"));
    87.             function.getExportParameterList().setValue("ECHOTEXT", function.getImportParameterList().getString("REQUTEXT"));
    88.             function.getExportParameterList().setValue("RESPTEXT", "Hello World");
    89.         }
    90.     }
    91.     
    92.     static void step1SimpleServer()
    93.     {
    94.         JCoServer server;
    95.         try
    96.         {
    97.             server = JCoServerFactory.getServer(SERVER_NAME1);
    98.         }
    99.         catch(JCoException ex)
    100.         {
    101.             throw new RuntimeException("Unable to create the server " + SERVER_NAME1 + ", because of " + ex.getMessage(), ex);
    102.         }
    103.         
    104.         JCoServerFunctionHandler stfcConnectionHandler = new StfcConnectionHandler();
    105.         DefaultServerHandlerFactory.FunctionHandlerFactory factory = new DefaultServerHandlerFactory.FunctionHandlerFactory();
    106.         factory.registerHandler("STFC_CONNECTION", stfcConnectionHandler);
    107.         server.setCallHandlerFactory(factory);
    108.         
    109.         server.start();
    110.         System.out.println("The program can be stopped using <ctrl>+<c>");
    111.     }
    112.     
    113.     static class MyThrowableListener implements JCoServerErrorListener, JCoServerExceptionListener
    114.     {
    115.         
    116.         public void serverErrorOccurred(JCoServer jcoServer, String connectionId, Error error)
    117.         {
    118.             System.out.println(">>> Error occured on " + jcoServer.getProgramID() + " connection " + connectionId);
    119.             error.printStackTrace();
    120.         }
    121.         public void serverExceptionOccurred(JCoServer jcoServer, String connectionId, Exception error)
    122.         {
    123.             System.out.println(">>> Error occured on " + jcoServer.getProgramID() + " connection " + connectionId);
    124.             error.printStackTrace();
    125.         }
    126.     }
    127.     
    128.     static class MyStateChangedListener implements JCoServerStateChangedListener
    129.     {
    130.         public void serverStateChangeOccurred(JCoServer server, JCoServerState oldState, JCoServerState newState)
    131.         {
    132.              
    133.             // Defined states are: STARTED, DEAD, ALIVE, STOPPED; 
    134.             // see JCoServerState class for details.  
    135.             // Details for connections managed by a server instance 
    136.             // are available via JCoServerMonitor
    137.             System.out.println("Server state changed from " + oldState.toString() + " to " + newState.toString() + 
    138.                     " on server with program id " + server.getProgramID());
    139.         }
    140.     }
    141.     static void step2SimpleServer()
    142.     {
    143.         JCoServer server;
    144.         try
    145.         {
    146.             server = JCoServerFactory.getServer(SERVER_NAME1);
    147.         }
    148.         catch(JCoException ex)
    149.         {
    150.             throw new RuntimeException("Unable to create the server " + SERVER_NAME1 + ", because of " + ex.getMessage(), ex);
    151.         }
    152.         
    153.         JCoServerFunctionHandler stfcConnectionHandler = new StfcConnectionHandler();
    154.         DefaultServerHandlerFactory.FunctionHandlerFactory factory = new DefaultServerHandlerFactory.FunctionHandlerFactory();
    155.         factory.registerHandler("STFC_CONNECTION", stfcConnectionHandler);
    156.         server.setCallHandlerFactory(factory);
    157.         
    158.         //additionally to step 1
    159.         MyThrowableListener eListener = new MyThrowableListener();
    160.         server.addServerErrorListener(eListener);
    161.         server.addServerExceptionListener(eListener);
    162.         
    163.         MyStateChangedListener slistener = new MyStateChangedListener();
    164.         server.addServerStateChangedListener(slistener);
    165.         server.start();
    166.         System.out.println("The program can be stopped using <ctrl>+<c>");
    167.     }
    168.     
    169.     static class MyTIDHandler implements JCoServerTIDHandler
    170.     {
    171.         
    172.         Map<String, TIDState> availableTIDs = new Hashtable<String, TIDState>();
    173.         
    174.         public boolean checkTID(JCoServerContext serverCtx, String tid)
    175.         {
    176.             System.out.println("TID Handler: checkTID for " + tid);
    177.             if(availableTIDs.containsKey(tid))
    178.                 return false;
    179.             //notify other instances about the starting of an tRFC call, if necessary
    180.             availableTIDs.put(tid, TIDState.STARTED);
    181.             return true;
    182.         }
    183.         public void commit(JCoServerContext serverCtx, String tid)
    184.         {
    185.             System.out.println("TID Handler: commit for " + tid);
    186.             //react on commit e.g. commit on the database
    187.             //if necessary throw a RuntimeException if the commit was not possible
    188.             availableTIDs.put(tid, TIDState.FINISHED);
    189.         }
    190.         public void rollback(JCoServerContext serverCtx, String tid)
    191.         {
    192.             System.out.println("TID Handler: rollback for " + tid);
    193.             //react on rollback e.g. rollback on the database
    194.             availableTIDs.put(tid, TIDState.FINISHED);
    195.         }
    196.         public void confirmTID(JCoServerContext serverCtx, String tid)
    197.         {
    198.             System.out.println("TID Handler: confirmTID for " + tid);
    199.             try 
    200.             {
    201.                 //clean up the resources
    202.             }
    203.             //catch(Throwable t) {} //partner wont react on an exception at this point
    204.             finally
    205.             {
    206.                 availableTIDs.remove(tid);
    207.             }
    208.         }
    209.         
    210.         private enum TIDState
    211.         {
    212.             STARTED, FINISHED;
    213.         }
    214.     }
    215.     
    216.     
    217.     static void step3SimpleTRfcServer()
    218.     {
    219.         JCoServer server;
    220.         try
    221.         {
    222.             server = JCoServerFactory.getServer(SERVER_NAME1);
    223.         }
    224.         catch(JCoException ex)
    225.         {
    226.             throw new RuntimeException("Unable to create the server " + SERVER_NAME1 + ", because of " + ex.getMessage(), ex);
    227.         }
    228.         
    229.         JCoServerFunctionHandler stfcConnectionHandler = new StfcConnectionHandler();
    230.         DefaultServerHandlerFactory.FunctionHandlerFactory factory = new DefaultServerHandlerFactory.FunctionHandlerFactory();
    231.         factory.registerHandler("STFC_CONNECTION", stfcConnectionHandler);
    232.         server.setCallHandlerFactory(factory);
    233.         
    234.         //additionally to step 1
    235.         MyTIDHandler myTIDHandler = new MyTIDHandler();
    236.         server.setTIDHandler(myTIDHandler);
    237.         server.start();
    238.         System.out.println("The program can be stopped using <ctrl>+<c>");
    239.     }
    240.         
    241.     public static void main(String[] a)
    242.     {
    243.         step1SimpleServer();
    244.         step2SimpleServer();
    245.         step3SimpleTRfcServer();
    246.     }
    247. }
    End of the code.