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

Java Connector(JCO)环境的搭建:Step by step to download and configure JCO in your laptop

我的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, "ldcixcd.wdf.sap.corp");
        connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR,  "00");
        connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "111");
        connectProperties.setProperty(DestinationDataProvider.JCO_USER,   "WANGJER");
        connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "Saptest1");
        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的原创技术文章,请关注公众号"汪子熙"或者扫描下面二维码:

原创声明,本文系作者授权云+社区发表,未经许可,不得转载。

如有侵权,请联系 yunjia_community@tencent.com 删除。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏一个爱瞎折腾的程序猿

sqlserver使用存储过程跟踪SQL

USE [master] GO /****** Object: StoredProcedure [dbo].[sp_perfworkload_trace_s...

2110
来自专栏我和未来有约会

Kit 3D 更新

Kit3D is a 3D graphics engine written for Microsoft Silverlight. Kit3D was inita...

2606
来自专栏落花落雨不落叶

canvas画简单电路图

64411
来自专栏一个会写诗的程序员的博客

Spring Reactor 项目核心库Reactor Core

Non-Blocking Reactive Streams Foundation for the JVM both implementing a Reactiv...

2222
来自专栏陈仁松博客

ASP.NET Core 'Microsoft.Win32.Registry' 错误修复

今天在发布Asp.net Core应用到Azure的时候出现错误InvalidOperationException: Cannot find compilati...

4878
来自专栏跟着阿笨一起玩NET

c#实现打印功能

2872
来自专栏Ceph对象存储方案

Luminous版本PG 分布调优

Luminous版本开始新增的balancer模块在PG分布优化方面效果非常明显,操作也非常简便,强烈推荐各位在集群上线之前进行这一操作,能够极大的提升整个集群...

3225
来自专栏飞扬的花生

jsencrypt参数前端加密c#解密

      写程序时一般是通过form表单或者ajax方式将参数提交到服务器进行验证,如何防止提交的请求不被抓包后串改,虽然无法说绝对安全却给非法提交提高了难度...

3919
来自专栏张善友的专栏

Silverlight + Model-View-ViewModel (MVVM)

     早在2005年,John Gossman写了一篇关于Model-View-ViewModel模式的博文,这种模式被他所在的微软的项目组用来创建Expr...

3008
来自专栏hbbliyong

WPF Trigger for IsSelected in a DataTemplate for ListBox items

<DataTemplate DataType="{x:Type vm:HeaderSlugViewModel}"> <vw:HeaderSlug...

4074

扫码关注云+社区