GRC
HR
SCM
CRM
BI


Article

 

Developing SAP-Enabled Java Applications with JCo

by Thomas G. Schuessler | SAPinsider

July 1, 2001

by Thomas G. Schuessler, ARAsoft SAPinsider - 2001 (Volume 2), July (Issue 3)
 

     While SAP had a product already (JRFC) to support the development of SAP-enabled Java components, there was still (how do I say this politely?) room for improvement. If you are interested in building add-ons to SAP in Java, SAP now offers a truly great product, the SAP Java Connector, or JCo (pronounced "jayco").

     Before we look at JCo's more prominent features, let us get two important questions out of the way: What does JCo cost? And where can I get it?

     Good news twice: JCo is free for SAP customers. It can be downloaded from http://service.sap.com/connectors.

     If you have already built applications using JRFC, I suggest that you convert them to JCo as soon as that is feasible. JCo is faster by at least an order of magnitude, has more features, has a simpler object model, and is much more stable. The JCo download contains a tutorial that shows you how to convert your JRFC applications.2

Clients? Servers? Sure!

     In most integration projects, an external client calls BAPIs in an SAP system. JCo obviously allows you to do this. But JCo also makes it extremely simple to develop a server component that can be called from an ABAP program. I see two major uses for this:

  • Provide corporate-wide services that can be used in SAP components as well as in other applications. A tax server written in Java would be a good example.
  • Subscribe to ALE in order to receive notifications about changes to certain objects. An example would be a sales application written in Java and running in a Web server. This application would clearly benefit if it had its own local database with all customers and sales products, automatically updated from SAP via ALE.3

Internet? Desktop? Whatever!

     JCo can be used to build simple, stand-alone desktop applications as well as high-end Web applications using Servlets and Java Server Pages (JSPs). The latter is made possible by the good performance characteristics and the fact that JCo has built-in, easy-to-use connection pooling. This allows you to service lots of Internet users with a much smaller number of SAP connections, preserving precious resources in your SAP systems.

     Let me give you a couple of hints to get you started in the right direction as far as building Web applications is concerned:

  • Your connection pool as well as your repository (this is where JCo keeps the metadata of BAPIs, structures, etc.) should be established in your Servlet's init method. This guarantees that this only takes place once.
  • In the Servlet's doGet and/or doPost methods you want to acquire a connection as late as possible and release it back to the pool as early as possible.4 This ensures optimum utilization of a limited number of connections.
  • You do yourself a big favor if you build GUI-free, SAP-enabled Java components first and then use those in your Web (and other) applications. This facilitates testing, reduces maintenance cost, and allows you to easily change the user interface of your applications.

     Here is a very simple Servlet that uses properly encapsulated access to SAP.5 In this example, it only takes one line of code to retrieve the collection of all countries!


A Simple Servlet
import de.arasoft.sap.jco.objectfactory.*;
import com.sap.mw.jco.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class SapServlet extends HttpServlet {
static final String POOLNAME = "TGS";
JCO.Repository repository = null;
OrderedProperties props = null;
ObjectFactory bof = null;
String s = "";
public void init(ServletConfig config) throws ServletException {
super.init(config);
try {
props = OrderedProperties.load("/logon.properties");
JCO.addClientPool(POOLNAME,
10,
props);
repository = new JCO.Repository("REPO", POOLNAME);
bof = new ObjectFactory(POOLNAME);
}
catch (Exception ex) {
s = ex.getMessage();
}
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
Countries countries = null;
Country country = null;
try {
countries = bof.getSapData().getCountries();
}
catch (Exception ex) {
s = ex.getMessage();
}
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("SAP Country List");
out.println("");
out.println(s + " out.println("" style="border-style: solid">");
if (countries != null) {
for (int i = 0; i < countries.getSize(); i++) {
country = countries.getCountry(i);
out.println("");
}
}
out.println("
");
out.println(country.getInternalCode());
out.println("
");
out.println(country.getDescription());
out.println("
");
out.close();
}
}

Synchronous? Transactional? Why Not Indeed!

     Normal client applications will use sRFC (synchronous Remote Function Call) to invoke BAPIs in SAP. In order to be part of an ALE-based distribution scenario, you also need tRFC (transactional RFC). tRFC guarantees the one-time-only delivery of a message, which is very important for an asynchronous messaging mechanism like ALE! JCo lets you use sRFC and tRFC both inbound and outbound. All the tedious details of tRFC are hidden by JCo so that you only have to write a few extra lines of code.6

Difficult to Deploy? Not at All!

     All you need is to copy a few DLLs and one JAR file. It does not get any easier than that.

Linux Anybody?

     The first shipment of JCo (in December of 2000) only supported Windows 32 platforms, but by the time you are reading this, support for Linux and other Unix variations should be either imminent or already available. Check SAP's Web site for the latest information.

Need More Info?

     First of all, the JCo download contains the Javadoc for all its classes and interfaces. In addition, there are two tutorials and several sample programs to get you started.

     If you need more in-depth information, I suggest that you attend the new, improved version of SAP's CA926 training class, which uses JCo exclusively. In this class you will learn the basics of BAPI programming, how to build desktop and browser-based client applications, as well as how to build server components that can be called from SAP. The only prerequisite for CA926 is that you are an experienced Java programmer. Please contact your SAP country organization for details about scheduling and registration.

     If you cannot wait for the next training class, contact me directly, and we might be able to arrange an on-site workshop for your Java developers.


Thomas G. Schuessler is the founder of ARAsoft (www.arasoft.de), a company offering products, consulting, custom development, and training to a worldwide base of customers. The company specializes in integration between SAP and non-SAP components and applications. ARAsoft offers various products for BAPI-enabled programs on the Windows and Java platforms. These products facilitate the development of desktop and Internet applications that communicate with R/3. Thomas is the author of SAP's CA925 "Developing BAPI-enabled Web Applications with Visual Basic" and CA926 "Developing BAPI-enabled Web Applications with Java" classes, which he teaches in Germany and in English-speaking countries. Thomas is a regularly featured speaker at SAP TechEd and SAPPHIRE conferences. Prior to founding ARAsoft in 1993, he worked with SAP AG and SAP America for seven years. Thomas can be contacted at thomas.schuessler@sap.com or at tgs@arasoft.de.


1 Take a look at Eiffel (www.eiffel.com) if you want to find out what is still missing in Java. And Eiffel's inventor, Bertrand Meyer, wrote the best book on object-oriented programming ever written: Object-Oriented Software Construction (Prentice Hall, ISBN: 0-13-629155-4).

2 The conversion will be easy if you did not use proxy classes in your application, but coded to the so-called "Common FRC Interface for Java" directly. There is currently no proxy generator for JCo, but you should not be too surprised if, by the time you are reading this, someone has released one.

3 I know that SAP Business Connector supports this king of scenario very well, but you can develop your own ALE receiver with JCo if, for whatever reason , you prefer not to use Business Connector in your project. (By the way: Business Connector itself uses JCo to access SAP.)

4 But not too early. If, for example, you call a BAPI that requires an external COMMIT WORK, you have to call both the application BAPI and BapiService. TransactionCommit before releasing the connection again.

5 The complete code for this Servlet, can be seen above.

6 You will still need to understand how to handle the TIDs (the GUIDs used to prevent duplicate processing) in your application!

An email has been sent to:






More from SAPinsider



COMMENTS

Please log in to post a comment.

No comments have been submitted on this article. Be the first to comment!


SAPinsider
FAQ