日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Jco服务配置以及程序编写

發布時間:2025/3/16 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Jco服务配置以及程序编写 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Jco服務配置以及程序編寫

SAP 與第三方面軟件進行數據通信時,運用到的中間件JCO,以下是JCO的服務配置.以及程序的編寫,希望能給大家帶來點幫助

SAP與java進行通信的流程:


操作步驟

ABAP(作為Clint端),調用JAVA(作為服務器端)。

首先,JCo服務器程序在網管中的注冊

下面給出該程序的JCo服務器程序在SAP網關中的注冊步驟。

(1) 在SM59中,定義一個連接類型為T的遠程目標

單擊創建按鈕,如下圖所示,要步驟一步一步填入所需要的數據
RFC目標系統,是RFC調用時候使用的。 Program ID,是JAVA程序中使用的,此處為LDKJCO,

這里的Geteway Host就是下圖的應用程序服務器地址。 TCP服務sapgw是固定的,后面的00就是下圖的系統編號。

特別要注意的是,下面圖要默認NON-Unicode,因為這個標示影響Java代碼中的Unicode設置。其它的默認就可以

上述配置完成即可。

測試連接,當然在做這測試連接之前先要將sap程序和JAVA程序的參數設置好,具體步驟看 sap程序和JAVA程序的設置,如下


sap程序編寫
創建一個測試程序,程序名zldkjco(自定義)
代碼如下:

REPORT ZLDKJCO.
DATA:?REQUTEXT LIKE SY-LISEL,
RESPTEXT LIKE SY-LISEL,
ECHOTEXT LIKE SY-LISEL.

DATA:?RFCDEST like rfcdes-rfcdest VALUE ‘NONE’.
DATA:?RFC_MESS(128).

REQUTEXT?= ‘HELLO?WORLD’.
RFCDEST?= ‘LDKJCO’. “corresponds?to?the?destination?name?defined?in?the?SM59

CALL FUNCTION ‘STFC_CONNECTION’ DESTINATION?RFCDEST
EXPORTING
REQUTEXT?=?REQUTEXT
IMPORTING
RESPTEXT?=?RESPTEXT
ECHOTEXT?=?ECHOTEXT
EXCEPTIONS
SYSTEM_FAILURE????????= 1 MESSAGE RFC_MESS
COMMUNICATION_FAILURE?= 2 MESSAGE RFC_MESS.

IF SY-SUBRC NE 0.
WRITE:?/ ‘Call?STFC_CONNECTION?????????SY-SUBRC?=?’,?SY-SUBRC.
WRITE:?/?RFC_MESS.
ENDIF.

接下來JAVA開發代碼,代碼的具體的操作可以在ECLIPSE中完成(要完成JCO的布置和JDK路徑的設置)
public class
StepByStepServer

{

static String SERVER_NAME1 = “SERVER”;

static String DESTINATION_NAME1 = “ABAP_AS_WITHOUT_POOL”;

static String DESTINATION_NAME2 = “ABAP_AS_WITH_POOL”;

static MyTIDHandler myTIDHandler = null;

static

{

Properties connectProperties = new Properties();

connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, “10.0.0.6″);

connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, “00″);

connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, “800″);

connectProperties.setProperty(DestinationDataProvider.JCO_USER, “XIAOFR”);

connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, “123456789″);

connectProperties.setProperty(DestinationDataProvider.JCO_LANG, “en”);

createDataFile(DESTINATION_NAME1, “jcoDestination”, connectProperties);

connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, “3″);

connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, “10″);

createDataFile(DESTINATION_NAME2, “jcoDestination”, connectProperties);

Properties servertProperties = new Properties();

servertProperties.setProperty(ServerDataProvider.JCO_GWHOST, “10.0.0.6″);

servertProperties.setProperty(ServerDataProvider.JCO_GWSERV, “sapgw00″);

servertProperties.setProperty(ServerDataProvider.JCO_PROGID, “LDKJCO”);

servertProperties.setProperty(ServerDataProvider.JCO_REP_DEST, “ABAP_AS_WITH_POOL”);

servertProperties.setProperty(ServerDataProvider.JCO_CONNECTION_COUNT, “2″);

createDataFile(SERVER_NAME1, “jcoServer”, servertProperties);

}

static void createDataFile(String name, String suffix, Properties properties)

{

File cfg = new File(name + “.” + suffix);

if(!cfg.exists())

{

try

{

FileOutputStream fos = new FileOutputStream(cfg, false);

properties.store(fos, “for tests only !”);

fos.close();

}

catch(Exception e)

{

throw new RuntimeException(“Unable to create the destination file “ + cfg.getName(), e);

}

}

}

static class StfcConnectionHandler implements JCoServerFunctionHandler

{

public void handleRequest(JCoServerContext serverCtx, JCoFunction function)

{

System.out.println(“—————————————————————-”);

System.out.println(“call : “ + function.getName());

System.out.println(“ConnectionId : “ + serverCtx.getConnectionID());

System.out.println(“SessionId : “ + serverCtx.getSessionID());

System.out.println(“TID : “ + serverCtx.getTID());

System.out.println(“repository name : “ + serverCtx.getRepository().getName());

System.out.println(“is in transaction : “ + serverCtx.isInTransaction());

System.out.println(“is stateful : “ + serverCtx.isStatefulSession());

System.out.println(“—————————————————————-”);

System.out.println(“gwhost: “ + serverCtx.getServer().getGatewayHost());

System.out.println(“gwserv: “ + serverCtx.getServer().getGatewayService());

System.out.println(“progid: “ + serverCtx.getServer().getProgramID());

System.out.println(“—————————————————————-”);

System.out.println(“attributes : “);

System.out.println(serverCtx.getConnectionAttributes().toString());

System.out.println(“—————————————————————-”);

System.out.println(“CPIC conversation ID: “ + serverCtx.getConnectionAttributes().getCPICConversationID());

System.out.println(“—————————————————————-”);

System.out.println(“req text: “ + function.getImportParameterList().getString(“REQUTEXT”));

function.getExportParameterList().setValue(“ECHOTEXT”, function.getImportParameterList().getString(“REQUTEXT”));

function.getExportParameterList().setValue(“RESPTEXT”, “Hello World”);

// In sample 3 (tRFC Server) we also set the status to executed:

if(myTIDHandler != null)

myTIDHandler.execute(serverCtx);

}

}

static void step1SimpleServer()

{

JCoServer server;

try

{

server = JCoServerFactory.getServer(SERVER_NAME1);

}

catch(JCoException ex)

{

throw new RuntimeException(“Unable to create the server “ + SERVER_NAME1 + “, because of “ + ex.getMessage(), ex);

}

JCoServerFunctionHandler stfcConnectionHandler = new StfcConnectionHandler();

DefaultServerHandlerFactory.FunctionHandlerFactory factory = new DefaultServerHandlerFactory.FunctionHandlerFactory();

factory.registerHandler(“STFC_CONNECTION”, stfcConnectionHandler);

server.setCallHandlerFactory(factory);

server.start();

System.out.println(“The program can be stoped using <ctrl>+<c>”);

}

static class MyThrowableListener implements JCoServerErrorListener, JCoServerExceptionListener

{

public void serverErrorOccurred(JCoServer jcoServer, String connectionId, JCoServerContextInfo serverCtx, Error error)

{

System.out.println(“>>> Error occured on “ + jcoServer.getProgramID() + ” connection “ + connectionId);

error.printStackTrace();

}

public void serverExceptionOccurred(JCoServer jcoServer, String connectionId, JCoServerContextInfo serverCtx, Exception error)

{

System.out.println(“>>> Error occured on “ + jcoServer.getProgramID() + ” connection “ + connectionId);

error.printStackTrace();

}

}

static class MyStateChangedListener implements JCoServerStateChangedListener

{

public void serverStateChangeOccurred(JCoServer server, JCoServerState oldState, JCoServerState newState)

{

// Defined states are: STARTED, DEAD, ALIVE, STOPPED;

// see JCoServerState class for details.

// Details for connections managed by a server instance

// are available via JCoServerMonitor

System.out.println(“Server state changed from “ + oldState.toString() + ” to “ + newState.toString() + ” on server with program id “

+ server.getProgramID());

}

}

static void step2SimpleServer()

{

JCoServer server;

try

{

server = JCoServerFactory.getServer(SERVER_NAME1);

}

catch(JCoException ex)

{

throw new RuntimeException(“Unable to create the server “ + SERVER_NAME1 + “, because of “ + ex.getMessage(), ex);

}

JCoServerFunctionHandler stfcConnectionHandler = new StfcConnectionHandler();

DefaultServerHandlerFactory.FunctionHandlerFactory factory = new DefaultServerHandlerFactory.FunctionHandlerFactory();

factory.registerHandler(“STFC_CONNECTION”, stfcConnectionHandler);

server.setCallHandlerFactory(factory);

// additionally to step 1

MyThrowableListener eListener = new MyThrowableListener();

server.addServerErrorListener(eListener);

server.addServerExceptionListener(eListener);

MyStateChangedListener slistener = new MyStateChangedListener();

server.addServerStateChangedListener(slistener);

server.start();

System.out.println(“The program can be stoped using <ctrl>+<c>”);

}

static class MyTIDHandler implements JCoServerTIDHandler

{

Map<String, TIDState> availableTIDs = new Hashtable<String, TIDState>();

public boolean checkTID(JCoServerContext serverCtx, String tid)

{

// This example uses a Hashtable to store status information. But usually

// you would use a database. If the DB is down, throw a RuntimeException at

// this point. JCo will then abort the tRFC and the R/3 backend will try

// again later.

System.out.println(“TID Handler: checkTID for “ + tid);

TIDState state = availableTIDs.get(tid);

if(state == null)

{

availableTIDs.put(tid, TIDState.CREATED);

return true;

}

if(state == TIDState.CREATED || state == TIDState.ROLLED_BACK)

return true;

return false;

// “true” means that JCo will now execute the transaction, “false” means

// that we have already executed this transaction previously, so JCo will

// skip the handleRequest() step and will immediately return an OK code to R/3.

}

public void commit(JCoServerContext serverCtx, String tid)

{

System.out.println(“TID Handler: commit for “ + tid);

// react on commit e.g. commit on the database

// if necessary throw a RuntimeException, if the commit was not

// possible

availableTIDs.put(tid, TIDState.COMMITTED);

}

public void rollback(JCoServerContext serverCtx, String tid)

{

System.out.println(“TID Handler: rollback for “ + tid);

availableTIDs.put(tid, TIDState.ROLLED_BACK);

// react on rollback e.g. rollback on the database

}

public void confirmTID(JCoServerContext serverCtx, String tid)

{

System.out.println(“TID Handler: confirmTID for “ + tid);

try

{

// clean up the resources

}

// catch(Throwable t) {} //partner wont react on an exception at

// this point

finally

{

availableTIDs.remove(tid);

}

}

public void execute(JCoServerContext serverCtx)

{

String tid = serverCtx.getTID();

if(tid != null)

{

System.out.println(“TID Handler: execute for “ + tid);

availableTIDs.put(tid, TIDState.EXECUTED);

}

}

private enum TIDState

{

CREATED, EXECUTED, COMMITTED, ROLLED_BACK, CONFIRMED;

}

}

static void step3SimpleTRfcServer()

{

JCoServer server;

try

{

server = JCoServerFactory.getServer(SERVER_NAME1);

}

catch(JCoException ex)

{

throw new RuntimeException(“Unable to create the server “ + SERVER_NAME1 + “, because of “ + ex.getMessage(), ex);

}

JCoServerFunctionHandler stfcConnectionHandler = new StfcConnectionHandler();

DefaultServerHandlerFactory.FunctionHandlerFactory factory = new DefaultServerHandlerFactory.FunctionHandlerFactory();

factory.registerHandler(“STFC_CONNECTION”, stfcConnectionHandler);

server.setCallHandlerFactory(factory);

// additionally to step 1

myTIDHandler = new MyTIDHandler();

server.setTIDHandler(myTIDHandler);

server.start();

System.out.println(“The program can be stoped using <ctrl>+<c>”);

}

public static void main(String[] a)

{

// step1SimpleServer();

step2SimpleServer();

// step3SimpleTRfcServer();

}

}

在ECLIPSE中編寫完成后,在ECLIPSE中可以運行程序,運行成功如下圖:

運行成功后會產生一個SERVER.jcoserver,點擊此文件可以看到如果下的信息,連接成功。

最后在SAP中,運行TCODE:SM59,選中你所創建的遠程目標,此處為LDKJCO。點連接后出下圖,說明SAP可以與JCO通信,SAP可以往JCO發數據。

總結

以上是生活随笔為你收集整理的Jco服务配置以及程序编写的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。