Client Programming Model
Definition
The following example describes a client programming model that you can use in Java EE applications (Servlets, EJB, and so on):
-
Set a context for the Connection Factory which was configured during SAPJRA instance configuration (refer to previous section. In this example, it is named MyConnFactory ):
InitialContext initialcontext = new InitialContext(); ConnectionFactory connectionfactory = (ConnectionFactory) initialcontext.lookup("java:comp/env/MyConnFactory"); -
Request a connection handle:
Connection connection = connectionfactory.getConnection(); -
Create a RecordFactoryobject to get a metadata description of the Remote Function Modules (RFM), that you will be calling:
RecordFactory recordFactory =connectionfactory.getRecordFactory(); -
Create Record objects containing all necessary information about the RFM:
MappedRecord input =recordFactory.createMappedRecord("NameOfYourRFM");This input object can be filled with business data. For example, if your input structure has a string field "MyBank", you may set it by
input.put("MyBank","BankOfWalldorf"); -
Create an interaction object to make a call to an SAP system where you can send your business data and receive data back:
Interaction interaction = connection.createInteraction(); -
Then you execute the call itself using the method
public Record execute(InteractionSpec interactionSpec, Record input) -
For any synchronous calls, you have to pass null for interactionSpec:
MappedRecord output = (MappedRecord) interaction.execute(null, input); -
For any transactional calls (tRFC calls) you need to create an InteractionSpec object.
InteractionSpec interactionSpec = ((InteractionSpecFactory) connectionFatory).createInteractionSpec();And pass this object within the method execute, like
interaction.execute(interactionSpec, input);If you use a new InteractionSpec instance in the next call, this will generate a new Transaction ID. This new call will then be considered as an independent call accordingly.
-
Add the description of this ConnectionFactory to your standard descriptor web.xml (if you access the factory from a servlet) or ejb-jar.xml (if you access the factory from EJB) to allow your application to access a specified ConnectionFactory :
<resource-ref> <res-ref-name> MyConnFactory </res-ref-name> <res-type> javax.resource.cci.ConnectionFactory </res-type> <res-auth> Application </res-auth> <res-sharing-scope> Shareable </res-sharing-scope> </resource-ref>
In this case, MyConnFactory represents the ConnectionFactory configured during the SAPJRA configuration (refer to SAP JRA Configuration on the SAP J2EE Application Server ).
More Information
For more information on descriptors, please refer to:
-
http://java.sun.com/dtd/web-app_2_3.dtd and
-
http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd