Show TOC

Example documentationJava Program for Creating a Server Connection Locate this document in the navigation structure

 

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:

  1. Implement the JCoServerFunctionHandler and the coding to be executed when the call is received.

  2. Create an instance for your JCoServer implementation and start it with start().

Example

Definition of Server Properties

Syntax Syntax

  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.     
End of the code.

JCo Server

Syntax Syntax

  1.       static class StfcConnectionHandler implements JCoServerFunctionHandler
  2.     {
  3.         public void handleRequest(JCoServerContext serverCtx, JCoFunction function)
  4.         {
  5.             System.out.println("----------------------------------------------------------------");
  6.             System.out.println("call              : " + function.getName());
  7.             System.out.println("ConnectionId      : " + serverCtx.getConnectionID());
  8.             System.out.println("SessionId         : " + serverCtx.getSessionID());
  9.             System.out.println("TID               : " + serverCtx.getTID());
  10.             System.out.println("repository name   : " + serverCtx.getRepository().getName());
  11.             System.out.println("is in transaction : " + serverCtx.isInTransaction());
  12.             System.out.println("is stateful       : " + serverCtx.isStatefulSession());
  13.             System.out.println("----------------------------------------------------------------");
  14.             System.out.println("gwhost: " + serverCtx.getServer().getGatewayHost());
  15.             System.out.println("gwserv: " + serverCtx.getServer().getGatewayService());
  16.             System.out.println("progid: " + serverCtx.getServer().getProgramID());
  17.             System.out.println("----------------------------------------------------------------");
  18.             System.out.println("attributes  : ");
  19.             System.out.println(serverCtx.getConnectionAttributes().toString());
  20.             System.out.println("----------------------------------------------------------------");
  21.             System.out.println("req text: " + function.getImportParameterList().getString("REQUTEXT"));
  22.             function.getExportParameterList().setValue("ECHOTEXT", function.getImportParameterList().getString("REQUTEXT"));
  23.             function.getExportParameterList().setValue("RESPTEXT", "Hello World");
  24.         }
  25.     }
  26.     static void step1SimpleServer()
  27.     {
  28.         JCoServer server;
  29.         try
  30.         {
  31.             server = JCoServerFactory.getServer(SERVER_NAME1);
  32.         }
  33.         catch(JCoException ex)
  34.         {
  35.             throw new RuntimeException("Unable to create the server " + SERVER_NAME1 + ", because of " + ex.getMessage(), ex);
  36.         }
  37.         
  38.         JCoServerFunctionHandler stfcConnectionHandler = new StfcConnectionHandler();
  39.         DefaultServerHandlerFactory.FunctionHandlerFactory factory = new DefaultServerHandlerFactory.FunctionHandlerFactory();
  40.         factory.registerHandler("STFC_CONNECTION", stfcConnectionHandler);
  41.         server.setCallHandlerFactory(factory);
  42.         
  43.         server.start();
  44.         System.out.println("The program can be stopped using <ctrl>+<c>");
  45.     }
End of the code.