Main Content

Bond Pricing Tool for Java Client

This example shows an application that calculates a bond price from a simple formula.

You run this example by entering the following known values into a simple graphical interface:

  • Coupon payment — C

  • Number of payments — N

  • Interest rate — i

  • Value of bond or option at maturity — M

The application calculates price (P) based on the following equation:

P = C * ( (1 - (1 + i)^-N) / i ) + M * (1 + i)^-N

Objectives

The Bond Pricing Tool demonstrates the following features of MATLAB® Production Server™:

  • Deploying a simple MATLAB function with a fixed number of inputs and a single output

  • Deploying a MATLAB function with a simple GUI front-end for data input

  • Using dispose() to free system resources

Step 1: Write MATLAB Code

Implement the Bond Pricing Tool in MATLAB, by writing the following code. Name the code pricecalc.m.

Sample code is available in MPS_INSTALL\client\java\examples\BondPricingTool\MATLAB.

function price = pricecalc(value_at_maturity, coupon_payment,...
                           interest_rate, num_payments)

    C = coupon_payment;
    N = num_payments;
    i = interest_rate;
    M = value_at_maturity;
    
    price = C * ( (1 - (1 + i)^-N) / i ) + M * (1 + i)^-N;

end

Step 2: Create a Deployable Archive with the Production Server Compiler App

To create the deployable archive for this example:

  1. From MATLAB, select the Production Server Compiler App.

  2. In the Application Type list, select Deployable Archive.

  3. In the Exported Functions field, add pricecalc.m.

    pricecalc.m is located in MPS_INSTALL\client\java\examples\BondPricingTool\MATLAB.

  4. Under Application Information, change pricecalc to BondTools.

  5. Click Package.

The generated deployable archive, BondTools.ctf is located in the for_redistribution_files_only of the project’s folder.

Step 3: Share the Deployable Archive on a Server

  1. Download the MATLAB Runtime, if needed, at https://www.mathworks.com/products/compiler/mcr. See Supported MATLAB Runtime Versions for more information.

  2. Create a server using mps-new. See Create a Server for more information.

  3. If you have not already done so, specify the location of the MATLAB Runtime to the server by editing the server configuration file, main_config and specifying a path for --mcr-root. See Edit the Configuration File for details.

  4. Start a Server Instance and Verify Server Status.

  5. Copy the BondTools.ctf file to the auto_deploy folder on the server for hosting.

Step 4: Create the Java Client Code

Create a compatible client interface and define methods in Java® to match MATLAB function pricecalc.m, hosted by the server as BondTools.ctf, using the guidelines in this section.

Additional Java files are also included that are typical of a standalone application. You can find the example files in MPS_INSTALL\client\java\examples\BondPricingTool\Java.

This Java code...Provides this functionality...
BondPricingTool.javaRuns the calculator application. The variable values of the pricing function are declared in this class.
BondTools.javaDefines pricecalc method interface, which is later used to connect to a server to invoke pricecalc.m
BondToolsFactory.javaFactory that creates new instances of BondTools
BondToolsStub.java

Java class that implements a dummy pricecalc Java method. Creating a stub method is a technique that allows for calculations and processing to be added to the application at a later time.

BondToolsStubFactory.javaFactory that returns new instances of BondToolsStub
RequestSpeedMeter.javaDisplays a GUI interface and accepts inputs using Java Swing classes
ServerBondToolsFactory.javaFactory that creates new instances of MWHttpClient and creates a proxy that provides an implementation of the BondTools interface and allows access to pricecalc.m, hosted by the server

When developing your Java code, note the following essential tasks, described in the sections that follow. For more information about clients coding basics and best practices, see Java Client Coding Best Practices.

This documentation references specific portions of the client code. You can find the complete Java client code in MPS_INSTALL\client\java\examples\BondPricingTool\Java.

Declare Java Method Signatures Compatible with MATLAB Functions You Deploy

To use the MATLAB functions you defined in Step 1: Write MATLAB Code, declare the corresponding Java method signature in the interface BondTools.java:

interface BondTools {
    double pricecalc (double faceValue,
                      double couponYield,
                      double interestRate,
                      double numPayments)
            throws IOException, MATLABException;
}

This interface creates an array of primitive double types, corresponding to the MATLAB primitive types (Double, in MATLAB, unless explicitly declared) in pricecalc.m. A one to one mapping exists between the input arguments in both the MATLAB function and the Java interface The interface specifies compatible type double. This compliance between the MATLAB and Java signatures demonstrates the guidelines listed in Java Client Coding Best Practices.

Instantiate MWClient, Create Proxy, and Specify Deployable Archive

In the ServerBondToolsFactory class, perform a typical MATLAB Production Server client setup:

  1. Instantiate MWClient with an instance of MWHttpClient:

    ...
        private final MWClient client = new MWHttpClient();
    
    

  2. Call createProxy on the new client instance. Specify port number (9910) and the deployable archive name (BondTools) the server is hosting in the auto_deploy folder:

    ...
    public BondTools newInstance () throws Exception
    {
      mpsUrl = new URL("http://user1.dhcp.mathworks.com:9910/BondTools");
      return client.createProxy(mpsUrl, BondTools.class);
    }
    ...

Use dispose() Consistently to Free System Resources

This application makes use of the Factory pattern to encapsulate creation of several types of objects.

Any time you create objects—and therefore allocate resources—ensure you free those resources using dispose().

For example, note that in ServerBondToolsFactory.java, you dispose of the MWHttpClient instance you created in Instantiate MWClient, Create Proxy, and Specify Deployable Archive when it is no longer needed.

Additionally, note the dispose() calls to clean up the factories in BondToolsStubFactory.java and BondTools.java.

Step 5: Build the Client Code and Run the Example

Before you attempt to build and run your client code, ensure that you have done the following:

  • Added mps_client.jar ($MPS_INSTALL\client\java) to your Java CLASSPATH and Build Path.

  • Copied your deployable archive to your server’s auto_deploy folder.

  • Modified your server’s main_config file to point to where your MATLAB Runtime is installed.

  • Start a Server Instance and Verify Server Status.

When you run the calculator application, you should see the following output: