Create MATLAB
Production Server
Java Client Using MWHttpClient
Class
This example shows how to write a MATLAB®
Production Server™ client using the MWHttpClient
class from the Java® client API. For information on obtaining the Java client library, see Obtain and Configure Client Library. In your Java code, you will:
Define a Java interface that represents the deployed MATLAB function.
Instantiate a static proxy object to communicate with the server.
Call the deployed function in your Java code.
To create a Java MATLAB Production Server client application:
Create a new file, for example,
MPSClientExample.java
.Using a text editor, open
MPSClientExample.java
.Add the following import statements to the file:
import java.net.URL; import java.io.IOException; import com.mathworks.mps.client.MWClient; import com.mathworks.mps.client.MWHttpClient; import com.mathworks.mps.client.MATLABException;
Add a Java interface that represents the deployed MATLAB function.
For example, consider the following
addmatrix
function deployed to the server. For information on writing and compiling the function for deployment, see Create Deployable Archive for MATLAB Production Server. For deploying the function to the server, see Deploy Archive to MATLAB Production Server.function a = addmatrix(a1,a2) a = a1 + a2;
The interface for the
addmatrix
function follows.interface MATLABAddMatrix { double[][] addmatrix(double[][] a1, double[][] a2) throws MATLABException, IOException; }
When creating the interface, note the following:
You can give the interface any valid Java name.
You must give the method defined by this interface the same name as the deployed MATLAB function.
The Java method must support the same inputs and outputs supported by the MATLAB function, in both type and number. For more information about data type conversions and how to handle more complex MATLAB function signatures, see Data Conversion with Java and MATLAB Types and Conversion of Java Types to MATLAB Types.
The Java method must handle MATLAB exceptions and I/O exceptions.
Add the following class definition:
public class MPSClientExample { }
This class now has a single
main
method that calls the generated class.Add the
main()
method to the application.public static void main(String[] args) { }
Add the following code to the top of the
main()
method to initialize the variables used by the application:double[][] a1={{1,2,3},{3,2,1}}; double[][] a2={{4,5,6},{6,5,4}};
Instantiate a client object using the
MWHttpClient
constructor.MWClient client = new MWHttpClient();
This class establishes an HTTP connection between the application and the server instance.
Call the
createProxy
method of the client object to create a dynamic proxy.You must specify the URL of the deployable archive and the name of your interface
class
as arguments:MATLABAddMatrix m = client.createProxy(new URL("http://localhost:9910/addmatrix"), MATLABAddMatrix.class);
The URL value (
"http://localhost:9910/addmatrix"
) used to create the proxy contains three parts:the server address (
localhost
).the port number (
9910
).the archive name (
addmatrix
)
For more information about the
createProxy
method, see the Javadoc included in the
folder, where$MPS_INSTALL
/client$MPS_INSTALL
is the name of your MATLAB Production Server installation folder.Call the deployed MATLAB function in your Java application by calling the public method of the interface.
double[][] result = m.addmatrix(a1,a2);
Call the
close()
method of the client object to free system resources.client.close();
Save the Java file.
The completed Java file should resemble the following:
import java.net.URL; import java.io.IOException; import com.mathworks.mps.client.MWClient; import com.mathworks.mps.client.MWHttpClient; import com.mathworks.mps.client.MATLABException; interface MATLABAddMatrix { double[][] addmatrix(double[][] a1, double[][] a2) throws MATLABException, IOException; } public class MPSClientExample { public static void main(String[] args){ double[][] a1={{1,2,3},{3,2,1}}; double[][] a2={{4,5,6},{6,5,4}}; MWClient client = new MWHttpClient(); try{ MATLABAddMatrix m = client.createProxy(new URL("http://localhost:9910/addmatrix"), MATLABAddMatrix.class); double[][] result = m.addmatrix(a1,a2); // Print the resulting matrix printResult(result); }catch(MATLABException ex){ // This exception represents errors in MATLAB System.out.println(ex); }catch(IOException ex){ // This exception represents network issues. System.out.println(ex); }finally{ client.close(); } } private static void printResult(double[][] result){ for(double[] row : result){ for(double element : row){ System.out.print(element + " "); } System.out.println(); } } }
Compile the Java application, using the
javac
command or use the build capability of your Java IDE.For example, enter the following at the Windows® command prompt:
javac -classpath "
MPS_INSTALL_ROOT
\client\java\mps_client.jar" MPSClientExample.javaRun the application using the
java
command or your IDE.For example, enter the following at the Windows command prompt:
java -classpath .;"
MPS_INSTALL_ROOT
\client\java\mps_client.jar" MPSClientExampleTo run the application on Linux® and macOS systems, use a colon (
:
) to separate multiple paths.The application returns the following at the console:
5.0 7.0 9.0 9.0 7.0 5.0