使用Java connector消费ABAP系统的函数

797 篇文章 33 订阅

我的ABAP系统有个函数名叫ZDIS_GET_UPSELL_MATERIALS,输入一个customer ID和product ID,会输出为这对客户和product组合维护的一组Upsell product ID和描述信息。


测试如下:
下面是使用Java消费该函数的代码:

package jco;

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.JCoException;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoParameterList;
import com.sap.conn.jco.JCoRepository;
import com.sap.conn.jco.JCoTable;
import com.sap.conn.jco.ext.DestinationDataProvider;

/**
 * basic examples for Java to ABAP communication  
 * See help: https://help.sap.com/saphelp_nwpi711/helpdata/en/48/70792c872c1b5ae10000000a42189c/frameset.htm
 */
public class StepByStepClient
{
	static String DESTINATION_NAME = "ABAP_AS_WITHOUT_POOL";
	static public final String ABAP_DURATION = "abapLayerDuration";
	static public final String UPSELL_PRODUCT = "upsellProducts";
	static public final String PRODUCT_ID = "productID";
	static public final String PRODUCT_TEXT = "productText";
	
    static private Properties prepareProperty(){
        Properties connectProperties = new Properties();
        connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "your abap system host name");
        connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR,  "00");
        connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "111");
        connectProperties.setProperty(DestinationDataProvider.JCO_USER,   "WANGJER");
        connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "your password");
        connectProperties.setProperty(DestinationDataProvider.JCO_LANG,   "en");
        createDestinationDataFile(DESTINATION_NAME, connectProperties);
        connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "3");
        connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT,    "10");
        createDestinationDataFile(DESTINATION_NAME, connectProperties);
        return connectProperties;
    }
    
    static public void main(String[] arg) {
    	createDestinationDataFile(DESTINATION_NAME, prepareProperty());
    	
    	JCoDestination destination = null;
		try {
			destination = JCoDestinationManager.getDestination(DESTINATION_NAME);
			JCoRepository repo = destination.getRepository();
    		JCoFunction stfcConnection = repo.getFunction("ZDIS_GET_UPSELL_MATERIALS");

    		JCoParameterList imports = stfcConnection.getImportParameterList();
        
    		String customerID = "1000040";
    		String materialID = "11";

    		imports.setValue("IV_CUSTOMER_ID", customerID);
    		imports.setValue("IV_MATERIAL_ID", materialID);

    		stfcConnection.execute(destination);
        
    		JCoParameterList exports = stfcConnection.getExportParameterList();
    		
    		// int result = exports.getInt("EV_RESULT");
    	    int abapDuration = exports.getInt("EV_DURATION");
    	    
    	    StringBuilder sb = new StringBuilder();
    	    sb.append("{ \"" + ABAP_DURATION + "\": " + abapDuration + ",");
    	    
    	    sb.append("\"" + UPSELL_PRODUCT + "\":[");
    	    
    	    JCoTable codes = exports.getTable("ET_MATERIALS");
    	    
    	    int row = codes.getNumRows();
    	    System.out.println("Total rows: " + row);
    	    
    	    System.out.println("ABAP duration: " + abapDuration);
    	    
    	    for( int i = 0; i < row; i++){
    	    	codes.setRow(i);
                System.out.println(codes.getString("MATERIAL_ID") + '\t' + codes.getString("MATERIAL_TEXT"));
                sb.append("{\"" + PRODUCT_ID + "\":" + codes.getString("MATERIAL_ID") + ","
                		+ "\"" + PRODUCT_TEXT + "\":\"" + codes.getString("MATERIAL_TEXT") + "\"");
                if( i < row - 1){
                	sb.append("},");
                }
                else{
                	sb.append("}");
                }
    	    }
    	    sb.append("]}");
    	    
    	    System.out.println("Final json: " + sb.toString());
    	    
		} catch (JCoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
    }
    
    static private 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 the destination files", e);
        }
    }
}

为简单起见没有使用Google的gson库进行Json的序列化。
执行结果:


要获取更多Jerry的原创文章,请关注公众号"汪子熙":

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汪子熙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值