Show TOC

Background documentationExample Program IDoc Server Locate this document in the navigation structure

 

The following programming example shows the use of the IDoc class library for IDoc communication using SAP JCo 3.0 from the server side.

Syntax Syntax

IDoc Server

  1. package com.sap.conn.idoc.examples;
  2. import com.sap.conn.idoc.*;
  3. import java.io.*;
  4. import com.sap.conn.jco.server.*;
  5. import com.sap.conn.idoc.jco.*;
  6. public class IDocServerExample
  7. {
  8.     public static void main(String[] a)
  9.     {   
  10.         try
  11.         {
  12.         	// see examples of configuration files MYSERVER.jcoServer and  BCE.jcoDestination provided in the installation directory. 
  13.         	JCoIDocServer server = JCoIDoc.getServer("MYSERVER"); 
  14.         	server.setIDocHandlerFactory(new MyIDocHandlerFactory());
  15.         	server.setTIDHandler(new MyTidHandler());
  16.         	
  17.         	MyThrowableListener listener = new MyThrowableListener();
  18.         	server.addServerErrorListener(listener);
  19.             server.addServerExceptionListener(listener);
  20.             server.setConnectionCount(1);
  21.             server.start();
  22.         }
  23.         catch (Exception e) 
  24.         {
  25.             e.printStackTrace();
  26.         }
  27.      }
  28.     
  29.     static class MyIDocHandler implements JCoIDocHandler
  30.     {
  31.         public void handleRequest(JCoServerContext serverCtx, IDocDocumentList idocList)
  32.         {
  33.         	
  34.         	FileOutputStream fos=null;
  35.             OutputStreamWriter osw=null;
  36.     		try
  37.     		{
  38.     			IDocXMLProcessor xmlProcessor = 
  39.     				JCoIDoc.getIDocFactory().getIDocXMLProcessor();
  40.                 fos=new FileOutputStream(serverCtx.getTID()+"_idoc.xml");
  41.                 osw=new OutputStreamWriter(fos, "UTF8");
  42.     			xmlProcessor.render(idocList, osw, 
  43.     					IDocXMLProcessor.RENDER_WITH_TABS_AND_CRLF);			
  44.     			osw.flush();
  45.     		}
  46.     		catch (Throwable thr)
  47.     		{
  48.     			thr.printStackTrace();
  49.     		}
  50.             finally
  51.             {
  52.                 try
  53.                 {
  54.                     if (osw!=null)
  55.                         osw.close();
  56.                     if (fos!=null)
  57.                         fos.close();
  58.                 }
  59.                 catch (IOException e)
  60.                 {
  61.                     e.printStackTrace();
  62.                 }
  63.             }
  64.         }
  65.     }
  66.     
  67.     static class MyIDocHandlerFactory implements JCoIDocHandlerFactory
  68.     {
  69.     	private JCoIDocHandler handler = new MyIDocHandler();
  70.     	public JCoIDocHandler getIDocHandler(JCoIDocServerContext serverCtx)
  71.     	{
  72.     		return handler;
  73.     	}
  74.     }
  75.     
  76.     static class MyThrowableListener implements JCoServerErrorListener, JCoServerExceptionListener
  77.     {
  78.         
  79.         public void serverErrorOccurred(JCoServer server, String connectionId, Error error)
  80.         {
  81.             System.out.println(">>> Error occured on " + server.getProgramID() + " connection " + connectionId);
  82.             error.printStackTrace();
  83.         }
  84.         public void serverExceptionOccurred(JCoServer server, String connectionId, Exception error)
  85.         {
  86.             System.out.println(">>> Error occured on " + server.getProgramID() + " connection " + connectionId);
  87.             error.printStackTrace();
  88.         }
  89.     }
  90.     
  91.     static class MyTidHandler implements JCoServerTIDHandler
  92.     {
  93.     	public boolean checkTID(JCoServerContext serverCtx, String tid)
  94.     	{
  95.     		System.out.println("checkTID called for TID="+tid);
  96.     		return true;
  97.     	}
  98.     
  99.     	public void confirmTID(JCoServerContext serverCtx, String tid)
  100.     	{
  101.     		System.out.println("confirmTID called for TID="+tid);
  102.     	}
  103.     
  104.     	public void commit(JCoServerContext serverCtx, String tid)
  105.     	{
  106.     		System.out.println("commit called for TID="+tid);
  107.     	}
  108.     
  109.     	public void rollback(JCoServerContext serverCtx, String tid)
  110.     	{
  111.     		System.out.print("rollback called for TID="+tid);
  112.     	}
  113.     }
  114. }
End of the code.