Channels ▼
RSS

JVM Languages

C++, Java, & Service-Oriented Architectures


There's no doubt in my mind that I became a better software developer the day I became a C++ programmer. But while I enjoy C++, there are aspects of Java that make me more productive, such as its support for strings, threads, I/O, distributed computing, and building web services. Moreover, Java has another strength that's not always taken advantage of—its ability to integrate with C++ applications.

The Java Native Interface (JNI) lets Java interface with software written in other languages, namely C++. Understanding JNI can bring the world of web services to your C++ application, without changing a line of existing C++ code. By leveraging the tools and support Java has for building web services, even a platform-specific C++ application can be exposed to the world in a platform-agnostic fashion.

The Scenario

To understand how Java can integrate with C++, and even augment it, consider the following scenario. Assume you have a securities trading system written and deployed as a C++/COM application on Windows. The application runs well, and is in use at many customer sites. However, there have been two overwhelming customer requests.

  • First, customers would like to access the trading system via the Web.
  • Second, customers would like to see you integrate a third-party portfolio manager library into your trading system.

A potential solution is to add SOAP support to your C++ trading system and integrate it with the library internally. However, some problems quickly arise. First, in this scenario, assume the library is a UNIX library, for which you do not have the source code. Second, how do you go about adding SOAP support in C++? You could port your code to .NET or find a third-party library to use. However, the downside to these choices is that they require a great deal of change to your C++ code, and probably result in two baselines of the system—one as a standalone C++/COM application (for existing customers), and the other as a C++ web service.

An alternate solution is to use Java to build a web service around your trading system and to integrate the portfolio manager within the Java code. The result would be a web service front end to your trading system, with portfolio management support, without requiring a change to your C++ code. You can leverage all that Java has to offer when building web services.

In this scenario, the C++ trading system is implemented as a COM server written in C++ as an Active Template Library (ATL) executable. This server exposes one interface—ITradeStock.

The Solution

The architecture I propose here uses JNI to provide a Java interface to the C++ application and library. The JNI code is to be exposed via a Java object that extends the java.rmi.Remote interface, making it accessible to remote Java applications via the Java Remote Method Invocation (RMI) API. Finally, a Java servlet implements the SOAP interface, and through RMI, calls the JNI layer that interfaces to the C++ code. Figure 1 illustrates the proposed system architecture.

Figure 1: System architecture.

The result is a solution that takes a C++/COM application (which must run on Windows) and a UNIX C++ library, and integrates them with Java, which can run on a variety of platforms. The integrated application is exposed via SOAP, making it platform and language independent. It is within the Java code that you solve the trading system/portfolio manager integration issue and implement the SOAP interface.

The first step in this solution is to define a Java interface that exposes methods similar to the C++ trading system and the portfolio manager, as in Listing One.

Listing One

public interface TradingSystemInterface extends Remote
{
    // From C++ Trading System (ITradeStock interface)
    public String GetQuote(String stock) 
        throws RemoteException;
    public String Buy(String stock, int shares) 
        throws RemoteException;
    public String Sell(String stock, int shares) 
        throws RemoteException;    
    // From C++ Portfolio Manager library
    public String AddHolding(String stock, int shares) 
        throws RemoteException;
    public String RemoveHolding(String stock, int shares) 
        throws RemoteException;
    public String EnumHoldings() 
        throws RemoteException;
    public String GetPosition(String stock) 
        throws RemoteException;
}

The first three methods, GetQuote, Buy, and Sell, map to the ITradeStock COM interface implemented in the C++ trading system. GetQuote returns the current quote of the given stock symbol. Buy and Sell place market orders for the stock.

The remaining methods map to those implemented in the C++ portfolio manager library. The methods AddHolding and RemoveHolding are called after you buy and sell stock, respectively, to track your positions. EnumHoldings returns a comma-delimited list of stock symbols that are in your portfolio. Finally, GetPosition returns the number of shares held for the given stock symbol. For the sake of simplicity, the portfolio manager library in this example will be implemented as a Windows DLL. This is done so that you can run all of the demo code (the COM application, the portfolio manager, and Java) on one Windows machine.

The Java Native Interface

With the interface defined, you need to generate the JNI code to integrate with the C++ trading system and the portfolio manager library. First, you define a Java class that implements the TradingSystemInterface Java interface, named TradingSystemImpl (Listing Two).

Listing Two

import java.rmi.server.*;

public class TradingSystemImpl 
    extends UnicastRemoteObject 
    implements TradingSystemInterface
{
    public TradeStockImpl() throws RemoteException {
    }
    // From C++ Trading System (ITradeStock)
    public String GetQuote(String stock) 
      throws RemoteException {
        return doGetQuote(stock);
    }
    public String Buy(String stock, int shares) 
      throws RemoteException {
        return doBuy(stock, shares);
    }
    public String Sell(String stock, int shares) 
      throws RemoteException {
        return doSell(stock, shares);
    }
    // From C++ Portfolio Manager
    public String AddHolding(String stock, int shares) 
      throws RemoteException {
        return doAddHolding(stock, shares);
    }
    public String RemoveHolding(String stock, int shares) 
      throws RemoteException {
        return doRemoveHolding(stock, shares);
    }
    public String EnumHoldings() 
      throws RemoteException {
        return doEnumHoldings(stock, shares);
    }
    public String GetPosition(String stock) 
      throws RemoteException {
        return doGetPosition(stock, shares);
    }
    /////////////////////////////////////////////////////
    // Native methods 
    // Calls the actual C++ Trading System (ITradeStock)
    native protected String doGetQuote(String stock);
    native protected String doBuy(String stock, int shares);
    native protected String doSell(String stock, int shares);
    // Calls the actual C++ Portfolio Manager
    native protected String doAddHolding(String stock, int shares);
    native protected String doRemoveHolding(String stock, int shares)
    native protected String doEnumHoldings()
    native protected String doGetPosition(String stock)
}

It's within the definition of the methods marked native that you add the code to call the C++ software. However, these methods are not implemented in Java, but are instead implemented in a special layer of C++ code partially generated by javah—a tool that comes with the Java Development Kit (JDK) and resides within the bin subdirectory of your JDK installation.

You compile the TradingSystemImpl class using the command:

>javac TradingSystemImpl.java

Next, use javah to generate the header files for your JNI implementation, providing the Java class file as a parameter:

>javah -jni TradingSystemImpl

If all goes well, the tool outputs the file TradingSystemImpl.h in the current directory. This file contains the function declarations for the methods that were marked "native" in the TradingSystemImpl class. An external header file, jni.h, is included at the top of the file and can be located in the include subdirectory of your JDK installation.

The final step is to implement the C++ functions. In this example, the implementation is a Windows DLL that includes the TradingSystemImpl.h file, where each function calls into the ITradeStock interface as well as the portfolio manager library. Listing Three shows a portion of the C++ JNI code. The complete implementation can be downloaded electronically, along with all of the code for this solution.

Listing Three

#include "TradingSystemImpl.h" // includes jni.h

#define INITGUIDS
#include "stdafx.h"
#include <ole2.h>
#include <ole2ver.h>
#include "stdlib.h"

// The GUIDs for using the ITradeStock interface 
const IID IID_ITradeStock = {0x326E68F3,0x6376,0x4A32,
                            {0xA0,0xA5,0xDD,0x37,0x02,0x6C,0xCC,0x3C}};
const IID LIBID_TRADINGSYSTEMLib = {0x81C64ABD,0xEDBB,0x492D,
                                   {0xB6,0x22,0xC6,0x2E,0x6F,0x68,0x0F,0xC3}};
const CLSID CLSID_TradeStock = {0x0D46CA8E,0x90A1,0x4F1F,
                                   {0xAE,0xBD,0x08,0xAB,0x32,0x10,0x93,0xF6}};
BOOL APIENTRY DllMain( HANDLE hModule, DWORD  ul_reason_for_call, 
                                                       LPVOID lpReserved)
{
    if ( FAILED ( CoInitialize(NULL) ) )
        return FALSE;
    return TRUE;
}
ITradeStock* GetTradeStockRef() 
{
    ITradeStock* pITradeStock = NULL;
    CoCreateInstance(CLSID_TradeStock, NULL, CLSCTX_LOCAL_SERVER, 
                     IID_ITradeStock, (VOID**)&pITradeStock);
    return pITradeStock; 
}
void ReleaseTradeStockRef(ITradeStock* pITradeStock) 
{
    if (pITradeStock ) pITradeStock->Release();
}
/* Class:     TradeStockImpl
 * Method:    doBuy
 * Signature: (Ljava/lang/String;I)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL 
    Java_TradeStockImpl_doBuy(JNIEnv * env, jobject me, jstring Stock, 
                              jint Shares) 
{
    // ...
}
 ...

JNI provides a C++ utility class, JNIEnv, which contains methods to help deal with conversions between C++ types and Java types. This class is defined in the header file jni.h. As an example of how to use this class, examine the JNI method Java_TradingSystemImpl_doGetQuote, in Listing Four.

Listing Four

JNIEXPORT jstring JNICALL 
   Java_TradingSystemImpl_doGetQuote(JNIEnv * env, jobject me, jstring Stock)
{
    double quote = 0.0000;
    ITradeStock* pITradeStock = GetTradeStockRef();
    if ( pITradeStock ) 
    {
        // Convert the Java String, Stock, to a C++ char*
        const char* stockStr = (*env).GetStringUTFChars(Stock, 0);

        // Calling a COM interface, convert to a BSTR
        wchar_t stockWStr[32];
        mbstowcs(stockWStr, stockStr, strlen(stockStr));
        BSTR stock = SysAllocString(stockWStr);

        // Call the actual C++ implementation of this method
        pITradeStock->GetQuote(stock, &quote);
        SysFreeString(stock);
    }
    ReleaseTradeStockRef(pITradeStock);
    // Convert the quote returned from a double to a string 
    int  decimal, sign;
    char* pszQuote = _ecvt( quote, 4, &decimal, &sign );
    char szQuote[12];
    // We need to add the decimal point since _ecvt doen't
    strncpy( szQuote, pszQuote, decimal );
    szQuote[decimal] = NULL;
    strcat( szQuote, "." );
    strcat( szQuote, &pszQuote[decimal] );
    // Since Java is expecting a Java String object as the return to this 
    // method, we use the JNIEnv class to convert char* to 
    // a jstring object and return it
    return (*env).NewStringUTF(szQuote);
}

This method calls the ITradeStock::GetQuote method within the C++ trading system, using the parameters passed in from the Java code. First, the parameter, Stock, is converted from a Java string to a C++ char* by calling JNIEnv::GetStringUTFChars. The resulting C++ string is converted to a COM BSTR before calling into the COM interface. The reverse occurs when the returned quote value is converted from a C++ double to a char*, and then to a Java string by calling JNIEnv::NewStringUTF.

In addition to the conversion routines, the JNIEnv class contains methods to throw Java exceptions, create Java objects, and even use Java reflection.

Java Remote Method Invocation

The JNI code needs to be accessible to other Java applications, potentially remote ones (those running on other computers). This lets you run the C++ trading system and its JNI code on a Windows PC, the portfolio manager and its JNI code on a UNIX machine, and the Java web service on a separate machine running either Windows or UNIX. Adding support for Java Remote Method Invocation (RMI) accomplishes this.

To support RMI, the interface TradingSystemInterface extends the java.rmi.Remote interface, and all of the methods throw java.rmi.RemoteExceptions. Also, the implementation class, TradingSystemImpl, extends the java.rmi.UnicastRemoteObject class. This makes the JNI code accessible to remote Java applications. The TradingSystemImpl class is then used to generate a client stub and a server skeleton for RMI. Java's stub/skeleton compiler, rmic, is called with the class name for which it is to generate the stub and skeleton classes:

>rmic TradingSystemImpl

After running successfully, you will find two new class files in the current directory, TradingSystemImpl_Stub.class and TradingSystemImpl_Skel.class. The stub class is loaded by Java for each client process that requests a reference to the TradingSystemImpl remote class. The skeleton class is loaded by Java when the RMI server process (which creates the TradingSystemImpl object) is started. The TradingSystemImpl object is only available to clients when the server process is running.

In this example, the TradingSystemImpl RMI server is defined as a Java application that performs the following tasks. First, the JNI Windows DLL is loaded via a call to System.loadLibrary. Second, an object of the TradingSystemImpl class is created; and third, a reference to this object is registered with the RMI registry running on the host machine. The code for this application, implemented in the Java class RMITradingSystem, is in Listing Five.

Listing Five

import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;

// This class runs as server process for the remotable  // TradingSystemImpl class

public class RMITradingSystem
{
    protected TradingSystemImpl tradeStock = null;
    public static void main(String [] args) {
        try {
            // Load the JNI C++ native code
            System.loadLibrary("JNITradingSystem"); 
            // Remaining work done in the constructor below
            RMITradingSystem rmiTS = new RMITradingSystem();
        }
        catch ( Exception e ) { 
            e.printStackTrace();
        }
    }    
    public RMITradingSystem() {
        try {
            // Create a reference the Remotable class
            tradeStock = new TradingSystemImpl();
            // Register this class with the local RMI registry
            Registry localRegistry = 
                LocateRegistry.getRegistry();
            try {
                localRegistry.bind( "TradingSystem", tradeStock );
            }
            catch ( Exception e ) {
                e.printStackTrace();
            }
        }
        catch ( Exception e ) { 
            e.printStackTrace();
        }
    }
}

Prior to running the TradingSystemImpl server process, the RMI registry must be started. Executing the rmiregistry command in the background and specifying the port as an optional parameter does this. The default RMI port is 1099.

When the RMI registry is started, the TradingSystemImpl server process should be started via the command:

> java TradingSystemImpl

Now, the JNI code that integrates with the C++ trading system and portfolio manager will be available to other Java applications via RMI.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video