您好,登錄后才能下訂單哦!
小編給大家分享一下如何使用Java connector消費ABAP系統的函數,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
我的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的序列化。
執行結果:
以上是“如何使用Java connector消費ABAP系統的函數”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。