The following example describes a client programming model that you can use in J2EE applications (Servlets, EJB, etc):
1. Set a context for the Connection Factory which was configured during SAPJRA instance configuration (see previous section. Here the name could be MyConnFactory):
InitialContext initialcontext = new InitialContext();
ConnectionFactory connectionfactory =
(ConnectionFactory) initialcontext.lookup("java:comp/env/MyConnFactory");
2. Request a connection handle:
Connection connection = connectionfactory.getConnection();
3. Create a RecordFactory object to get a metadata description of the Remote Function Modules (RFM), that you will be calling:
RecordFactory recordFactory = connectionfactory.getRecordFactory();
4. Create Record objects containing all necessary information about theRFM.
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”);
5. 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();
6. Execute the call:
MappedRecord output = (MappedRecord) interaction.execute(null, input);
The input parameter in the method execute(interactionSpec, input) should be of type MappedRecord. The return object output in this method is also always of type MappedRecord.
7. Use the following method for any synchronous calls (calls that are executed immediately, and the method call either returns output object or throws an exception, if the call failed):
public Record execute(InteractionSpec interactionSpec, Record input)
Since all required information about the interaction was declared when creating the MappedRecored input, there is no need to pass anything for interactionSpec. So just pass null for interactionSpec.
8. Use the following method for any asynchronous calls (in the SAP system known as tRFC):
public boolean execute(InteractionSpec interactionSpec, Record input,
Record output)
where you need to pass null for interactionSpec and for output (since no data could be received by tRFC).
If in this case an exception occurs. The state of the tRFC call is unknown and should be analyzed separately (the TID of this call is returned in the Exception text). The new call of this API with the same input will result in repeating the call with a new TID.
9. 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>
· Transactions for the Client Programming Model
· Security for the Client Programming Model
For more information on descriptors, please see:
· http://java.sun.com/dtd/web-app_2_3.dtd and
· http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd