Test Client Data Integration Against MATLAB
This example shows how to test your RESTful API or Java® client for deployment to MATLAB® Production Server™ using the development version of MATLAB Production Server. MATLAB Compiler SDK™ includes the development version of MATLAB Production Server for testing and debugging application code and Excel® add-ins before deploying them to web applications and enterprise systems.
For testing purposes, you will create and use a MATLAB function called addmatrix
that accepts two numeric
matrices as inputs and returns their sum as an output. You access the local test server
by clicking the Test Client button in the Production Server
Compiler app.
Create a MATLAB Function
Write a MATLAB function called
addmatrix
that accepts two numeric matrices as inputs and returns their sum as an output. Save this file asaddmatrix.m
.function a = addmatrix(a1, a2) a = a1 + a2;
Test the function at the MATLAB command prompt.
a = [10 20 30; 40 50 60]; b = [100 200 300; 400 500 600]; c = addmatrix(a,b)
c = 110 220 330 440 550 660
Prepare for Testing
Open the Production Server Compiler app by typing the following at the MATLAB command prompt:
productionServerCompiler
In the Type section of the toolstrip, select Deployable Archive (.ctf) from the list.
Specify the MATLAB functions to deploy.
In the Exported Functions section of the toolstrip, click the plus button.
Using the file explorer, locate and select the
addmatrix.m
file.
In the section titled Include MATLAB function signature file, click the Create File button. This will create an editable JSON file that contains the function signatures of the functions included in the archive. By editing this file you can specify argument types and/or sizes of inputs and outputs, and also provide help information for each of the inputs. For more information, see MATLAB Function Signatures in JSON (MATLAB Production Server).
If you have an existing JSON file with function signatures, click the Add Existing File button to add that file instead of the Create File button.
By including this information in your archive, you can use the discovery service functionality on the server.
Note
Only the MATLAB Production Server RESTful API supports the discovery service. For more information, see RESTful API for MATLAB Function Execution (MATLAB Production Server).
Click the Test Client button. The app will switch to the TEST tab.
Check the value of the Port field.
It must be:
an available port
the same port number the client is using
For this example, the client will use port 9910.
Check the box to Enable CORS. This option needs to be enabled if you are using a client that uses JavaScript®. By enabling CORS the server will accept requests from different domains.
Check the box to Enable Discovery. This option needs to be enabled to use the discovery service. The discovery service returns information about deployed MATLAB functions as a JSON object.
Click Start.
Test Using RESTful API
This example uses the MATLAB Call Web Services from MATLAB Using HTTP to invoke the RESTful API and make requests to the testing interface. You can use other tools such cURL or JavaScript XHR.
The testing interface does not support asynchronous client requests. The interface processes a POST Asynchronous Request (MATLAB Production Server) like a POST Synchronous Request (MATLAB Production Server). Other asynchronous requests from the RESTful API are not supported.
Test Discovery Service
Import the MATLAB HTTP Interface packages, setup the request, and send the request to the testing interface.
% Import MATLAB HTTP Interface packages import matlab.net.* import matlab.net.http.* import matlab.net.http.fields.* % Setup request requestUri = URI('http://localhost:9910/api/discovery'); options = matlab.net.http.HTTPOptions('ConnectTimeout',20,... 'ConvertResponse',false); request = RequestMessage; request.Header = HeaderField('Content-Type','application/json'); request.Method = 'GET'; % Send request response = request.send(requestUri, options);
View the response body.
response.Body.Data
The response body has been snipped to fit the page. A formatted version of the response body can be found by expandingans = "{"discoverySchemaVersion":"1.0.0","archives":{"matfun":{"archiveSchemaVersion":"1.1.0",...
ans
.To test using JavaScript XHR you can use the following code:
Testing Data Exchange
Start a separate session of the MATLAB desktop.
Note
You must use a separate MATLAB session to make POST requests. If you make POST requests from the same MATLAB session that is running the testing interface, MATLAB does not respond.
Import the MATLAB HTTP Interface packages, setup the request, and send the request to the testing interface.
% Import HTTP interface packages import matlab.net.* import matlab.net.http.* import matlab.net.http.fields.* % Setup message body body = MessageBody; a = [10 20 30; 40 50 60]; b = [100 200 300;400 500 600]; payload = mps.json.encoderequest({a,b}); body.Payload = payload; % Setup request requestUri = URI('http://localhost:9910/matfun/addmatrix'); options = matlab.net.http.HTTPOptions('ConnectTimeout',20,... 'ConvertResponse',false); request = RequestMessage; request.Header = HeaderField('Content-Type','application/json'); request.Method = 'POST'; request.Body = body; % Send request response = request.send(requestUri, options)
View the response body.
response.Body.Data
ans = "{"lhs":[[[110,220,330],[440,550,660]]]}"
To test using JavaScript XHR you can use the following code:
Examine Data
Switch to the Production Server Compiler app.
In the testing interface, under MATLAB Execution Requests, click the completed message in the app to see the values exchanged between the client and MATLAB.
Click Input to view the arrays passed into MATLAB.
Click Output to view the array returned to the client.
Set Breakpoints
In the testing interface of the Production Server Compiler, click Breakpoints > Break on MATLAB function entry.
In the separate MATLAB session, resend a POST request to the local test server.
When the MATLAB editor opens, note that a breakpoint is set at the first line in the function and that processing has paused at the breakpoint.
You now can use all of the MATLAB debugging tools to step through your function.
Note
You can create a timeout error in the client if you take a long time stepping through the MATLAB function.
Note that variables
a1
anda2
are displayed in the MATLAB workspace.In the MATLAB editor, click Continue to complete the debug process.
The Server Requests section of the app shows that the request completed successfully.
Click Stop to shutdown the local test server.
Click Close Test.
Testing Using Java Client Application
Create a Java file
MPSClientExample.java
with following client code:At the system command prompt, compile the Java client code using the
javac
command.javac -classpath "
matlabroot
\toolbox\compiler_sdk\mps_clients\java\mps_client.jar" MPSClientExample.javaAt the system command prompt, run the Java client.
java -classpath .;"matlabroot\toolbox\compiler_sdk\mps_clients\java\mps_client.jar" MPSClientExample
Note
You cannot run the Java client from the MATLAB command prompt.
The application returns the following at the console:
110.0 220.0 330.0 440.0 550.0 660.0
You can debug the data exchanged between the client and MATLAB using the same steps listed under Test Using RESTful API.