If the following ABAP statement is called up:
Syntax
CALL FUNCTION 'STFC_CONNECTION' DESTINATION RFCDEST IN BACKGROUND TASK // (bgRFC: IN BACKGROUND UNIT)
fRFC or bgRFC processing takes place.
Note
The prerequisite for this is the implementation of a separate JCoServerTIDHandler.
This means that the following call sequence is triggered:
boolean checkTID(String tid) //against your JCoServerTIDHandler implementation
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.
calls the function module.
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.
protected void methods onConfirmTID(String tid).
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.
Syntax
tRFC Call
static class MyTIDHandler implements JCoServerTIDHandler
{
Map<String, TIDState> availableTIDs = new Hashtable<String, TIDState>();
public boolean checkTID(JCoServerContext serverCtx, String tid)
{
// This example uses a hash table to store status information.
// Usually you would use a database. If the DB is down,
// throw a RuntimeException at this point. JCo will then abort
//the tRFC and the SAP backend will try again later.
System.out.println("TID Handler: checkTID for " + tid);
TIDState state = availableTIDs.get(tid);
if(state == null)
{
availableTIDs.put(tid, TIDState.CREATED);
return true;
}
if(state == TIDState.CREATED || state == TIDState.ROLLED_BACK)
return true;
return false;
// "true" means that JCo will now execute the transaction,
// "false" means that we have already executed this
// transaction previously, so JCo will skip the
// handleRequest() step and will immediately return an OK
// code to SAP.
}
public void commit(JCoServerContext serverCtx, String tid)
{
System.out.println("TID Handler: commit for " + tid);
// react on commit e.g. commit on the database
// if necessary throw a RuntimeException, if the commit
// was not possible
availableTIDs.put(tid, TIDState.COMMITTED);
}
public void rollback(JCoServerContext serverCtx, String tid)
{
System.out.println("TID Handler: rollback for " + tid);
availableTIDs.put(tid, TIDState.ROLLED_BACK);
// react on rollback e.g. rollback on the database
}
public void confirmTID(JCoServerContext serverCtx, String tid)
{
System.out.println("TID Handler: confirmTID for " + tid);
try
{
// clean up the resources
}
// catch(Throwable t) {} //partner wont react on an
// exception at this point
finally
{
availableTIDs.remove(tid);
}
}
public void execute(JCoServerContext serverCtx)
{
String tid = serverCtx.getTID();
if(tid != null)
{
System.out.println("TID Handler: execute for " + tid);
availableTIDs.put(tid, TIDState.EXECUTED);
}
}
private enum TIDState
{
CREATED, EXECUTED, COMMITTED, ROLLED_BACK, CONFIRMED;
}
}
static void step3SimpleTRfcServer()
{
JCoServer server;
try
{
server = JCoServerFactory.getServer(SERVER_NAME1);
}
catch(JCoException ex)
{
throw new RuntimeException("Unable to create the server " + SERVER_NAME1 + ", because of " + ex.getMessage(), ex);
}
JCoServerFunctionHandler stfcConnectionHandler = new StfcConnectionHandler();
DefaultServerHandlerFactory.FunctionHandlerFactory factory = new DefaultServerHandlerFactory.FunctionHandlerFactory();
factory.registerHandler("STFC_CONNECTION", stfcConnectionHandler);
server.setCallHandlerFactory(factory);
// in addition to step 1
myTIDHandler = new MyTIDHandler();
server.setTIDHandler(myTIDHandler);
server.start();
System.out.println("The program can be stopped using <ctrl>+<c>");
}
public static void main(String[] a)
{
// step1SimpleServer();
step2SimpleServer();
// step3SimpleTRfcServer();
}
}