Main Content

MATLABProduction ServerJavaClient Basics

Themps_client.jarJava®client library lets you evaluate MATLAB®functions deployed on remote servers using native Java data.

Obtainmps_client.jarClient Library

There are several options to obtain themps_client.jarclient library based on your project set up:

  • In aMATLAB Production Server™installation,mps_client.jaris located in$MPS_INSTALL/client/java.

  • The library is available for download atMATLAB Production Server Client Libraries. Select your release to download the folder, then unzip it.mps_client.jaris located in/java.

  • 图书馆也驻留在Maven™库athttps://mvnrepository.com/artifact/com.mathworks.prodserver/mps_java_client. To use the jar in your Maven project, include the following coordinates in thepom.xmlfile:

    < !——https://mvnrepository.com/artifact/com.mathworks.prodserver/mps_java_client -->  com.mathworks.prodserver mps_java_client release_number 

Configure your development environment to usemps_client.jarby adding it to your Java class path.

Choose Workflow for Client-Server Communication

The Java client API offers two workflows for client-server communication:

UseMWHttpClientClass

This workflow uses theMWHttpClientclass and hides the implementation details of request creation and data serialization when evaluating MATLAB functions deployed on servers. This workflow provides the following two options to evaluate a deployed MATLAB function.

Based on your requirements, decide if the client uses a static proxy or a dynamic proxy.

  • A static proxy uses an object implementing an interface that mirrors the deployed MATLAB functions. You provide the interface for the static proxy. This is a type-safe API that enforces passing the proper data types to the function at compile time.

    SeeStatic Proxy Interface Guidelines.

  • A dynamic proxy creates server requests based on the MATLAB function name provided to theinvoke()method. You pass the function name as a parameter to the proxy along with the function arguments. You provide the function name, the number of output arguments, and all of the input arguments required to evaluate the functions. Doing so defers type checking until runtime.

    SeeInvoke MATLAB Functions Dynamically.

Write Java code to instantiate a proxy to aMATLAB Production Serverinstance and call the MATLAB functions.

  1. Create anMWClientobject for communicating with the service hosted by aMATLAB Production Serverinstance.

  2. Create MATLAB data structures to hold the data passed between the client and server.

  3. Invoke MATLAB functions.

  4. Free system resources using theclosemethod of theMWClientobject.

Use RESTful API and protobuf

This workflow uses theMATLAB Production ServerRESTful API for MATLAB Function Executionfor MATLAB function execution for request creation andprotocol buffers(protobuf) for data serialization. Protocol buffers are a language-neutral and platform-neutral method of serializing structured data.

To use protobuf when making a request to the server, set the HTTPContent-Typeheader toapplication/x-google-protobufin the client code. The Java client library provides helper classes to internally create protobuf messages based on a proto format and returns the corresponding byte array. Use this byte array in the HTTP request body. The Java client library provides methods and classes to deserialize the protobuf responses.

For examples, seeAsynchronous RESTful Requests Using Protocol Buffers in the Java ClientandSynchronous RESTful Requests Using Protocol Buffers in the Java Client.

Logging

You can record details such as HTTP request statuses, server URLs, and output data in your Java client application using the logging capability available in theMATLAB Production ServerJava client library. To offer logging options, the Java client library,mps_client.jar, packages the SLF4J API module (version 1.7.25) as part of the client library. You can use any SLF4J-supported logging framework such as Log4j, Logback, or thejava.util.loggingpackage. Without a binding, SLF4J defaults to a no-operation implementation. For details about using a binding, see theBridging Legacy APIsdocumentation on the SLF4J website.

When using a logging framework or a different version of SLF4J, you must add it in front of themps_client.jaron the Java class path.

If you use thejava.util.loggingpackage for logging, you must load and use thejava.util.logging.Loggerclass in your Java application code before you load thecom.mathworks.mps.client.MWHttpClientclass. For more information aboutjava.util.logging, see the Oracle®Package java.util.loggingJavadoc.

Related Topics

External Websites