Handling Multiple Connections 

Use

You can use multiple connections and call different RFC functions within the context of these different connections. The different connections can be to different R/3 systems or to the same system but using different user accounts, for example.

Procedure

To use multiple connections, you create one or more IRfcConnection object(s), one for each additional connection you wish to use. You create an IRfcConnection object from the IRfcConnectionFactory object, which you obtain from FactoryManager.

You then create and call the different IRfcModules within the context of the different connections.

  1. Obtain the FactoryManager instance and set it up with MiddlewareInfo.
  2. Obtain an IRfcConnectionFactory with the getIRfcConnectionFactory method of the FactoryManager.
  3. For every connection you wish to work with (represented by IRfcConnection):
  4.  

    1. Set the UserInfo and ConnectInfo objects with the parameters of the connection you wish to establish.
    2. Use the IRfcConnectionFactory object to create an IRfcConnection object for this connection.
    3. When creating the IRfcConnection object, specify the UserInfo and ConnectInfo as parameters.

      You can create an empty connection by not specifying UserInfo and ConnectInfo as parameters, but then you will need to set these before opening the connection.

    4. Start the connection with the open method of the IRfcConnection object.

      (Note that you start a single connection with the open method of the SessionManager object.)

     

  5. Create the IRfcModule object within the context of the relevant connection:
  6.  

    1. You need to specify the connection as a parameter when creating the IRfcModule within a connection. This is true regardless of whether you create the IRfcModule manually or with an autoCreate.
    2. If you are automatically creating any of the structure or table parameters of the IRfcModule object, you must also specify the connection object as a parameter to the autoCreate method (this is because AutoCreate connects to R/3 to obtain the parameter metadata).

Make sure you use the same connection as the one you have used for creating the IRfcModule object whose parameter you are creating.

When you call the RFC function module, the IRfcModule uses the connection you have specified when creating the module.

Example

The following example uses two connections to call the RFC function GET_SYSTEM_NAME twice: once in each connection. Since it connects to two different systems, the value of the resulting SYSTEM_NAME export parameter is different after the two calls. The example uses a Properties file for both connections.

/**
 * Run the function in multiple connections.
 * This example connects to two different R/3 systems and calls
 * a function module on both.
 */
public void run()
{
    // Activate the Factory Manager instance
    FactoryManager factoryMgr  = FactoryManager.getSingleInstance();
    SessionInfo sessionInfo = getSessionInfo(PROPS_FILE);
    factoryMgr.setMiddlewareInfo(sessionInfo.getMiddlewareInfo());
        
    // Obtain the necessary factories
    IRfcModuleFactory moduleFac = factoryMgr.getRfcModuleFactory();
    IRfcConnectionFactory connectionFac = factoryMgr.getRfcConnectionFactory();
            
    // Run a function module using the first connection
    try
    {
        // PROPS_FILE1 is a String constant representing the properties file
        SessionInfo    sessionInfo1 = getSessionInfo(PROPS_FILE1);                
        IRfcConnection connection1  = 
            connectionFac.createRfcConnection(sessionInfo1.getConnectInfo(),
                                               sessionInfo1.getUserInfo());
                
        connection1.open();
            
        // Specify the connection when creating the module
        IRfcModule module1  = 
            moduleFac.autoCreateRfcModule(connection1, "GET_SYSTEM_NAME");
            
        int    retCode1 = module1.callReceive();
        String sysName1 = module1.getSimpleExportParam("SYSTEM_NAME").getString();
                
        System.out.println("System name = " + sysName1);
            
        connection1.close();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
            
    // Run a function module using the second connection
    try
    {
        // PROPS_FILE2 is a String constant representing the properties file
        SessionInfo    sessionInfo2 = getSessionInfo(PROPS_FILE2);                
        IRfcConnection connection2  = 
            connectionFac.createRfcConnection(sessionInfo2.getConnectInfo(),
                                               sessionInfo2.getUserInfo());
                
        connection2.open();
            
        // Specify the connection when creating the module
        IRfcModule module2  = 
            moduleFac.autoCreateRfcModule(connection2, "GET_SYSTEM_NAME");
            
        int    retCode2 = module2.callReceive();
        String sysName2 = module2.getSimpleExportParam("SYSTEM_NAME").getString();
                
        System.out.println("System name = " + sysName2);
            
        connection2.close();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}
    
/**
 * Obtain a SessionInfo object initialized to the values 
 * in a desired properties file.
 * @param  propertiesFile name of the desired properties file
 *
 */
private SessionInfo getSessionInfo(String propertiesFile)
{
    SessionInfo sessionInfo = new SessionInfo();
        
    try
    {
        Properties properties  = new Properties();
            
        properties.load(
            new java.io.BufferedInputStream(
                new java.io.FileInputStream(propertiesFile)));
        sessionInfo.setProperties(properties);
    }
    catch (java.io.IOException e)
    {
        // You could initialize the sessionInfo to some safe defaults.
        e.printStackTrace();
    }
        
    return sessionInfo;
}

See Also

If you use a single connection, you can use the SessionManager object to handle the connection. See all the subtopics of Using the Client Interface to Make an RFC Call.