Wednesday, August 27, 2014

SAP JCo - An example program to establish the connection

import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.ext.DestinationDataProvider;

public class BAPICallTest {

    static String DESTINATION_NAME1 = "ABAP_AS_WITHOUT_POOL";

    static String DESTINATION_NAME2 = "ABAP_AS_WITH_POOL";

    static {
        Properties connectProperties = new Properties();
        connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "host name");
        connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "system number");
        connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "Client");
        connectProperties.setProperty(DestinationDataProvider.JCO_USER, "user name");
        connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "password");

        connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "en");
        createDestinationDataFile(DESTINATION_NAME1, connectProperties);
        connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "3");
        connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "10");
        createDestinationDataFile(DESTINATION_NAME2, connectProperties);
    }

    static void createDestinationDataFile(String destinationName, Properties connectProperties) {
        File destCfg = new File(destinationName + ".jcoDestination");
        try {
            FileOutputStream fos = new FileOutputStream(destCfg, false);
            connectProperties.store(fos, "for tests only !");
            fos.close();
        } catch (Exception e) {
            throw new RuntimeException("Unable to create thedestination files", e);
        }
    }

    public static void main(String[] args) throws Exception {
        JCoDestination destination1 = JCoDestinationManager.getDestination("DESTINATION_NAME1");
        System.out.println(destination1);
        System.out.println("Destination 1 Attributes:");
        System.out.println(destination1.getAttributes());

        JCoDestination destination2 = JCoDestinationManager.getDestination("DESTINATION_NAME2");
        System.out.println(destination2);
        System.out.println("Destination 2 Attributes:");
        System.out.println(destination2.getAttributes());

        JCoFunction function = destination2.getRepository().getFunction("STFC_CONNECTION");
        function.execute(destination2);
    }
}


The above program gets attributes of SAP server. It uses direct connection and connection pool.

No comments:

Post a Comment