Show TOC

Example documentationProcessing a tRFC/bgRFC Call Locate this document in the navigation structure

 

If the following ABAP statement is called up:

Syntax Syntax

  1. CALL FUNCTION 'STFC_CONNECTION'
       DESTINATION RFCDEST
          IN BACKGROUND TASK // (bgRFC: IN BACKGROUND UNIT)
    
End of the code.

fRFC or bgRFC processing takes place.

Note Note

The prerequisite for this is the implementation of a separate JCoServerTIDHandler.

End of the note.

This means that the following call sequence is triggered:

  1. boolean checkTID(String tid) //against your JCoServerTIDHandler implementation

    Note Note

    At this point, the application must be informed that the next call is to be processed with this TID. The application must return the value true in order to signal that execution is possible.

    End of the note.
  2. calls the function module.

  3. commit(String tid) or rollback(String tid) depends on the result of the function distribution. If handleRequest has not thrown any exception, onCommit is called.

  4. protected void methods onConfirmTID(String tid).

    Note Note

    At this point, the application is informed that execution has been ended by the call with the specified TID. Under certain circumstances, this call might be received in a different Listener or, if problems arise, may not be received in the ABAP backend system at all.

    End of the note.

Syntax Syntax

tRFC Call

  1. static class MyTIDHandler implements JCoServerTIDHandler
  2.     {
  3.         
  4.         Map<String, TIDState> availableTIDs = new Hashtable<String, TIDState>();
  5.         
  6.         public boolean checkTID(JCoServerContext serverCtx, String tid)
  7.         {
  8.         // This example uses a hash table to store status information.                 
  9.         // Usually you would use a database. If the DB is down,  
  10.         // throw a RuntimeException at this point. JCo will then abort  
  11.             //the tRFC and the SAP backend will try again later.
  12.            System.out.println("TID Handler: checkTID for " + tid);
  13.             TIDState state = availableTIDs.get(tid);
  14.             if(state == null)
  15.             {
  16.                 availableTIDs.put(tid, TIDState.CREATED);
  17.                 return true;
  18.             }
  19.             
  20.             if(state == TIDState.CREATED || state == TIDState.ROLLED_BACK)
  21.                 return true;
  22.             return false;
  23.             // "true" means that JCo will now execute the transaction,  
  24.             // "false" means that we have already executed this
  25.             // transaction previously, so JCo will skip the 
  26.             // handleRequest() step and will immediately return an OK  
  27.             // code to SAP.
  28.         }
  29.         
  30.         public void commit(JCoServerContext serverCtx, String tid)
  31.         {
  32.             System.out.println("TID Handler: commit for " + tid);
  33.             
  34.             // react on commit e.g. commit on the database
  35.             // if necessary throw a RuntimeException, if the commit  
  36.             // was not possible
  37.             availableTIDs.put(tid, TIDState.COMMITTED);
  38.         }
  39.         
  40.         public void rollback(JCoServerContext serverCtx, String tid)
  41.         {
  42.             System.out.println("TID Handler: rollback for " + tid);
  43.             availableTIDs.put(tid, TIDState.ROLLED_BACK);
  44.             
  45.             // react on rollback e.g. rollback on the database
  46.         }
  47.         
  48.         public void confirmTID(JCoServerContext serverCtx, String tid)
  49.         {
  50.             System.out.println("TID Handler: confirmTID for " + tid);
  51.             
  52.             try
  53.             {
  54.                 // clean up the resources
  55.             }
  56.             // catch(Throwable t) {} //partner wont react on an 
  57.             // exception at this point
  58.             finally
  59.             {
  60.                 availableTIDs.remove(tid);
  61.             }
  62.         }
  63.         
  64.         public void execute(JCoServerContext serverCtx)
  65.         {
  66.             String tid = serverCtx.getTID();
  67.             if(tid != null)
  68.             {
  69.                 System.out.println("TID Handler: execute for " + tid);
  70.                 availableTIDs.put(tid, TIDState.EXECUTED);
  71.             }
  72.         }
  73.         
  74.         private enum TIDState
  75.         {
  76.             CREATED, EXECUTED, COMMITTED, ROLLED_BACK, CONFIRMED;
  77.         }
  78.     }
  79.     
  80.     static void step3SimpleTRfcServer()
  81.     {
  82.         JCoServer server;
  83.         try
  84.         {
  85.             server = JCoServerFactory.getServer(SERVER_NAME1);
  86.         }
  87.         catch(JCoException ex)
  88.         {
  89.             throw new RuntimeException("Unable to create the server " + SERVER_NAME1 + ", because of " + ex.getMessage(), ex);
  90.         }
  91.         
  92.         JCoServerFunctionHandler stfcConnectionHandler = new StfcConnectionHandler();
  93.         DefaultServerHandlerFactory.FunctionHandlerFactory factory = new DefaultServerHandlerFactory.FunctionHandlerFactory();
  94.         factory.registerHandler("STFC_CONNECTION", stfcConnectionHandler);
  95.         server.setCallHandlerFactory(factory);
  96.         
  97.         // in addition to step 1
  98.         myTIDHandler = new MyTIDHandler();
  99.         server.setTIDHandler(myTIDHandler);
  100.         
  101.         server.start();
  102.         System.out.println("The program can be stopped using <ctrl>+<c>");
  103.     }
  104.     
  105.     public static void main(String[] a)
  106.     {
  107.         // step1SimpleServer();
  108.         step2SimpleServer();
  109.         // step3SimpleTRfcServer();
  110.     }
  111. }
End of the code.