Main Content

Run Client and Server Using RMI

This example shows how to implement RMI to run two separate processes that initialize MATLAB® struct arrays. The client and the server run on the same machine.

To implement RMI with a client on one machine and a server on another, use the procedure in this example and:

  1. Change how the server is bound to the system registry.

  2. Redefine how the client accesses the server.

RMI Prerequisites

To run this example, your environment must meet the following prerequisites:

  • Install MATLAB Compiler SDK™ on the development machine.

  • Install a supported version of the Java® Development Kit (JDK™) on the development machine. For more information, see Configure Your Java Environment for Generating Packages.

  • Install MATLAB Runtime on the web server. For details, see Install and Configure MATLAB Runtime.

  • Ensure that your web server is capable of running accepted Java frameworks like J2EE.

  • Install the javabuilder.jar library (matlabroot/toolbox/javabuilder/jar/javabuilder.jar) into your web server’s common library folder.

    If your implementation uses separate client machines, they also need javabuilder.jar, since it contains the com.mathworks.extern.java package.

Note

You do not need MATLAB Runtime installed on the client side. Return values from MATLAB Runtime can be automatically converted using the boolean marshalOutputs in the RemoteProxy class. For details, see the Javadoc API documentation in matlabroot/help/toolbox/javabuilder/MWArrayAPI.

Files

MATLAB Function Locationmatlabroot\toolbox\javabuilder\Examples\RMIExamples\DataTypes\DataTypesDemoComp
Java Code Locationmatlabroot\toolbox\javabuilder\Examples\RMIExamples\DataTypes\DataTypesDemoJavaApp

Procedure

  1. Copy the DataTypes folder from MATLAB to your work folder:

    copyfile(fullfile(matlabroot,'toolbox','javabuilder','Examples','RMIExamples','DataTypes'))

    At the MATLAB command prompt, navigate to the new DataTypes\DataTypesDemoComp subfolder in your work folder.

  2. Examine the MATLAB functions createEmptyStruct.m and updateField.m.

     createEmptyStruct.m

     updateField.m

  3. Generate the Java package using compiler.build.javaPackage by issuing the following command at the MATLAB command prompt:

    compiler.build.javaPackage({'createEmptyStruct.m','updateField.m'}, ...
        'PackageName','dataTypesComp', ...
        'ClassName','dataTypesClass', ...
        'Verbose','on');

    For more details, see the instructions in Generate Java Package and Build Java Application.

  4. At your system command prompt, navigate to the DataTypes\DataTypesDemoJavaApp folder.

    Compile the server Java code by issuing one of the following javac commands at your system command prompt.

    • On Windows®, type:

      javac -classpath
       "matlabroot\toolbox\javabuilder\jar\javabuilder.jar;path\to\dataTypesComp.jar"
       DataTypesServer.java 
    • On UNIX®, type:

      javac -classpath
       "matlabroot/toolbox/javabuilder/jar/javabuilder.jar:path/to/dataTypesComp.jar"
       DataTypesServer.java

    Note

    Replace matlabroot with the path to your MATLAB or MATLAB Runtime installation folder.

  5. Compile the client Java code by issuing one of the following javac commands at your system command prompt.

    • On Windows, type:

      javac -classpath
       "matlabroot\toolbox\javabuilder\jar\javabuilder.jar;path\to\dataTypesComp.jar
       DataTypesClient.java
    • On UNIX, type:

      javac -classpath
       "matlabroot/toolbox/javabuilder/jar/javabuilder.jar:path/to/dataTypesComp.jar"
       DataTypesClient.java

Run Client and Server

Run the client and server as follows:

  1. Open two command windows—one for the server and one for the client.

  2. In each window, navigate to the folder that contains DataTypesServer.java or DataTypesClient.java, respectively.

  3. Run the server by issuing one of the following java commands in a single line at the system command prompt.

    • On Windows, type:

      java -classpath
       .;"path\to\dataTypesComp.jar;matlabroot\toolbox\javabuilder\jar\javabuilder.jar"
       -Djava.rmi.server.codebase="file:///matlabroot\toolbox\javabuilder\jar\javabuilder.jar
       file:///path\to\dataTypesComp.jar" DataTypesServer
    • On UNIX, type:

      java -classpath
       .:"path/to/dataTypesComp.jar;matlabroot/toolbox/javabuilder/jar/javabuilder.jar"
       -Djava.rmi.server.codebase="file:///matlabroot/toolbox/javabuilder/jar/javabuilder.jar
       file:///path/to/dataTypesComp.jar" DataTypesServer

  4. In the second command window, run the client by issuing one of the following java commands in a single line.

    • On Windows, type:

      java -classpath
       .;"path/to/\dataTypesComp.jar;matlabroot\toolbox\javabuilder\jar\javabuilder.jar"
       DataTypesClient
    • On UNIX, type:

      java -classpath
       .:"path/to/dataTypesComp.jar;matlabroot/toolbox/javabuilder/jar/javabuilder.jar"
       -Djava.rmi.server.codebase="file:///matlabroot/toolbox/javabuilder/jar/javabuilder.jar
       file:///path/to/dataTypesComp.jar" DataTypesClient

If the commands are successful, the following output appears in the command window running the server:

Please wait for the server registration notification.            
            Server registered and running successfully!!

            EVENT 1: Initializing the structure on server
                     and sending it to client:
                     Initialized empty structure:

                   Name: []
                Address: []

            ##################################

            EVENT 3: Partially initialized structure as received by server:

                   Name: []
                Address: [1x1 struct]

            Address field as initialized from the client:

                Street: '3, Apple Hill Drive'
                  City: 'Natick'
                 State: 'MA'
                   Zip: '01760'

            ##################################

            EVENT 4: Updating 'Name' field before 
                     sending the structure back to the client:

                  Name: 'The MathWorks'
               Address: [1x1 struct]

            ##################################
The following output appears in the command window running the client:
 Running the client application!!

            EVENT 2: Initialized structure as received in client applications:

                   Name: []
                Address: []

            Updating the 'Address' field to :

                Street: '3, Apple Hill Drive'
                  City: 'Natick'
                 State: 'MA'
                   Zip: '01760'

            #################################


            EVENT 5: Final structure as received by client:

                   Name: 'The MathWorks'
                Address: [1x1 struct]

            Address field:

                Street: '3, Apple Hill Drive'
                  City: 'Natick'
                 State: 'MA'
                   Zip: '01760'

            #################################

Note

For more examples of RMI implementation, see the files in matlabroot/toolbox/javabuilder/Examples/RMIExamples.

Related Topics