You are on page 1of 10

SAP JCo Server Programming

The following section explains how you can write your own JCo programs for servers if you use the standalone version of the SAP JCo. A JCo server program implements functions that are called up by an R C client. The JCo server program is registered via the SAP !ateway and waits for inbound R C calls. An R C server program registers itself under a program "# to an SAP gateway $not for a specific SAP system%. "f an R C call is passed on from any SAP system to this SAP gateway with the option &Connection with a registered program' $with the same program "#%( the connection ta)es place with the corresponding JCo server program. *nce an R C function has been executed( the JCo Server waits for further R C calls from the same or other SAP systems. "f an R C connection is interrupted or terminated( the JCo server automatically registers itself again on the same SAP gateway under the same program "#.
Prerequisites

+ou are using the standalone version of the SAP JCo.

,sing transaction S-./( you have defined a destination with connection type T $TCP0"P connection% in the SAP system. +ou have chosen the registration mode $&Registered server program' option under the &Technical settings' tab page% for this destination. The destination contains the re1uired information about the SAP gateway and the registered R C server program.

Additional Information
The following sections provide examples of how to program various JCo server functions2 3stablishing an R#C Connection $"nbound Call from the SAP System% Java Program for 3stablishing a Server Connection 3xception 4istener Server State Change 4istener

Processing an A6AP Call

Inbound RFC Connection (from the SAP System)


This section provides an example of how you can establish a server7side R C connection that originates from the SAP system. To send a call from an A6AP system( the A6AP program uses the option #3ST"8AT"*8 98A-39 for the command CA44 ,8CT"*8.

"n transaction S !"# create a re$ort %ith the follo%ing coding2

&A'A( R )*' +' ,I- S./,IS ,# R SP' +' ,I- S./,IS ,# C01' +' ,I- S./,IS ,2

&A'A( RFC& S' li)e rfcdes7rfcdest 3A,* 4515 42 &A'A( RFC67 SS(89")2

R )*' +' : 40 ,,1 ;1R,&42 RFC& S' : 4JC1S R3 R<842 9corresponds to the destination name defined in the S-./

CA,, F*5C'I15 4S'FC6C155 C'I154 & S'I5A'I15 RFC& S' +P1R'I5= R )*' +' : R )*' +' I7P1R'I5= R SP' +' : R SP' +' C01' +' : C01' +' +C P'I15S S.S' 76FAI,*R : 8 7 SSA= RFC67 SS

C177*5ICA'I156FAI,*R : 9 7 SSA= RFC67 SS2

IF S./S*>RC 5 <2 ;RI' ( ? 4Call S'FC6C155 C'I15 ;RI' ( ? RFC67 SS2 5&IF2 ; S./S*>RC : 4# S./S*>RC2

Java Program for stablishing a Server Connection


*se
"n the next step( you can write a Java program that establishes a server connection to a SAP gateway.

Procedure
To do this( you need to2 extend the JC*.Server class

implement a constructor that passes on the re1uired parameters to the JC*.Server superclass such as !ateway <ost( !ateway Service( Program "# and Repository. overwrite the protected void method handleRe1uest$JC*. unction function% and implement the coding that is executed when the call is received. create various instances for your JC*.Serverimplementation and start them with start$%.

@am$le

$ublic class -y irstServer extends JC*.Server > 0?? ? Create an instance of my own server ? @param gwhost $gateway host% ? @param gwserv $gateway service number% ? @param progid $program id% ? @param repository $repository used by the server to loo)up the definitions of an inc% ?0 $ublic -y irstServer$String gwhost( String gwserv( String progid( "Repository repository% > super$gwhost(gwserv(progid(repository%A B

0?? ? *verrides the default method. ?0 protected void handleRe1uest$JC*. unction function% > JC*.Parameter4ist input C function.get"mportParameter4ist$%A JC*.Parameter4ist output C function.get3xportParameter4ist$%A JC*.Parameter4ist tables C function.getTableParameter4ist$%A

System.out.println$9handleRe1uest$9 D function.get8ame$% D 9%9%A if $function.get8ame$%.e1uals$9ST CEC*883CT"*89%% > System.out.println$9FFF re1uest ST CEC*883CT"*82 9 D input.getString$9R3G,T3HT9%%A output.setIalue$input.getString$9R3G,T3HT9%(93C<*T3HT9%A .

Additional Information
or a description of the ,nicode connection( see2 Server Connection to a ,nicode 6ac)end System

@ce$tion ,istener
Jhenever errors occur( the JCo throws an exception. All exceptions that occur in the JCo are passed on to the registered Exception and Error Listener.

The application must process the exceptions separately in the method handleRe1uest$% $that is( the exceptions that it generates itself%. 3xceptions from the application coding are not passed on to the 4istener. To define a 4istener( create a class that implements the JC*.Server3xception4istener and JC*.ServerStateChanged4istener2

$ublic class -y3xceptionAnd3rror4istener implements JC*.Server3xception4istener( JC*.Server3rror4istener >

$ublic void server3xception*ccurred$JC*.Server server( 3xception ex% > System.out.println$93xception in server 9 D server.getProg"#$% D 92Ln9 D ex%A ex.printStac)Trace$%A B $ublic void server3rror*ccurred$JC*.Server server( 3rror er% > System.out.println$93rror in server 9 D server.getProg"#$% D 92Ln9 D er%A er.printStac)Trace$%A B B Register the 4istener class with JC*.addServer3rror4istener and JC*.addServer3xception4istener2 $ublic class Second3xample extends irst3xample > $ublic static void main$StringMN args% > -y3xceptionAnd3rror4istener l C new -y3xceptionAnd3rror4istener$%A JC*.addServer3rror4istener$l%A JC*.addServer3xception4istener$l%A irst3xample.startServers$% A B B

Server State Change ,istener


O

The JC*.ServerStateChanged4istenerinterface enables you to monitor the server connection.


$ublic class -yServerStateChanged4istener > 0?? ? Simply prints server state changes ?0 $ublic void serverStateChange*ccurred$JC*.Server server( int oldEstate( int newEstate% > System.out.print$9Server 9 D server.getProg"#$% D 9 changed state from M9%A if $$oldEstate P JC*.STAT3EST*PP3# if $$oldEstate P JC*.STAT3ESTART3# % QC R% System.out.print$9 ST*PP3# 9%A % QC R% System.out.print$9 START3# 9%A

if $$oldEstate P JC*.STAT3E4"ST38"8! % QC R% System.out.print$9 4"ST38"8! 9%A if $$oldEstate P JC*.STAT3ETRA8SACT"*8% QC R% System.out.print$9 TRA8SACT"*8 9%A if $$oldEstate P JC*.STAT3E6,S+ % QC R% System.out.print$9 6,S+ 9%A

System.out.print$9N to M9%A if $$newEstate P JC*.STAT3EST*PP3# if $$newEstate P JC*.STAT3ESTART3# % QC R% System.out.print$9 ST*PP3# 9%A % QC R% System.out.print$9 START3# 9%A

if $$newEstate P JC*.STAT3E4"ST38"8! % QC R% System.out.print$9 4"ST38"8! 9%A if $$newEstate P JC*.STAT3ETRA8SACT"*8% QC R% System.out.print$9 TRA8SACT"*8 9%A if $$newEstate P JC*.STAT3E6,S+ System.out.println$9N9%A % QC R% System.out.print$9 6,S+ 9%A

B B

Register the 4istener class with the AP" JC*.addServerStateChanged4istener$%2

$ublic class Third3xample extends irst3xample > $ublic static void main$StringMN args% > -y3xceptionAnd3rror4istener el C new -y3xceptionAnd3rror4istener$%A JC*.addServer3rror4istener$el%A JC*.addServer3xception4istener$el%A -yServerStateChanged4istener sl C new -yServerStateChanged4istener$%A JC*.addServerStateChanged4istener$sl%A irst3xample.startServers$% A B B

Processing an A>AP Call


The class that extends the JC*.Server and is called up when an A6AP call is being processed( must overwrite the protected void method handleRe1uest$JC*. unction function%. The application that processes the call must 1uit call execution can only allow A6AP exceptions. All other exceptions that are thrown by handleRe1uest are treated as system failures.

protected void handleRe1uest$JC*. unction function% > try > 00process the re1uest B catch$3xception e% > thro% new Abap3xception$9a group of the exception9( 9text9%A B B The exception and error listeners are not informed about the exception case. This ta)es place in handleRe1uest.

Additional Information
or a description of the call se1uence for tR C processing( see2 Processing a tR C Call

5R