...
The EagleML API library can execute requests in asynchronous or synchronous mode.
- The asynchronous mode receives the reply from EagleML API through a callback. The EagleML API client sends a REST request to MC2 and waits for the Task Status Reply and the extract data to be delivered asynchronously when ready via a callback.
- The synchronous mode executes the request and waits for the TSR/reply on the same connection.
...
Execute Requests in the Eagle ML APL Library
No Format |
---|
dependencies { compile 'com.eagleinvsys.eaglemlapi:eaglemlapiclient:1.2.0' } |
To use the library, add the following dependency: In the gradle.build
script add:
...
Before using the EagleML API for REST calls it should be initialized as described here:. This example should be executed only once.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
// This is the initialization and shutdown code // Create the eaglemlApi object String endpoint = "http://url.to.mcserviceeagle.web.server:port"; String login = "eagleuser"; String password = "eaglepassword"; com.eagleinvsys.eagleapi.client.EagleApi eaglemlApi = new EagleApi(endpoint, login, password, null /* by default use java.io.tmpdir temp folder, or specify the path to the temporary folder for big extracts */, true); // Optional - Add this to enable the CSV metrics reporting File csvmetricsdir = new File("/apps/eagle/csvmetrics"); eaglemlApi.enableCsvMetricReporter(csvmetricsdir, 0); // Optional - set the threshold on the extract result size, after which the data is saved to a temporary file. Default is 1MB //eaglemlApi.setTempFileThreshold(10000000); // Optional - set the starting callback port number and the number of ports to try. // If not specified the operating system will use a random port number. // You need to do this only if there is a firewall between the EaglemlApi object and Message CenterEagle THICK Application Server, preventing the callback to complete //eaglemlApi.setStartingCallbackPort(40000); // Optional - set the number of ports to try. In this example eaglemlApi will attempt to use ports in the range of 40000 to 40020 // If it can not listen to any of these ports it will fail to start. //eaglemlApi.setNumberOfPortsToTry(20); // Optional - set the published host address for callbacks. If not set the local host address (one of the interfaces) will be used. // This is needed only if there is a firewall/NAT between EaglemlApi Client and MessageCenter Eagle THICK Application Servr and Message Center needs a specific IP to execute the callback. //eaglemlApi.setPublishedHostAddress("192.168.1.104"); // Optional - set the name of the stream to which RTR will be sent. //eaglemlApi.setSendToStream("eagle_ml-2-0_default_out_extract_service"); // Start the EagleApi listener and initialize the objects required to parse the results. // Pass true as a parameter to wait for initialization to complete. // Pass false as a parameter to complete the initialization asynchronously eaglemlApi.start(true); |
The above should be executed only once. After the initial request, you can execute multiple requests concurrently on the eaglemlApi object.
...
Wait on a Future<IEagleApiResult> object
The following is an example of a wait on a Future<IEagleApiResult> object.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
// THIS IS A SAMPLE OF THE REQUEST EXECUTION. REQUESTS CAN BE EXECUTED ONLY AFTER THE eaglemlApi.start() is called. Map<String, String> parameters = new HashMap<String, String>(); // Add the parameters required to build an URI // Generate a unique correlationId // Add the attachments for the POST request Future<IEagleApiResult> futureResult = eaglemlApi.executeRestRequestAsync("GET", "/eagle/v2/manager-relationships", parameters, correlationId, null, 600 /* timeout in seconds */); try (IEagleApiResult result = futureResult.get()) { switch(result.getResultStatus()) { case EagleApiResultStatus.SUCCESS: List<IEagleApiResultDataFile> files = result.getDataFiles(); for (IEagleApiResultDataFile file : files) { // See the java doc for the IEagleApiResultDataFile. The file will represent one result. // Currently EaglemlApi will produce only one file. // You can get a stream of uncompressed data: try(InputStream stream = file.getStream()) { // Parse the extract and execute any transformations - for example to JSON } // If needed you can get the compressed data and get access to the file with the file.isInMemory(), file.getData(), file.getFile() // The temporary files (if any) created while processing the extracts will be removed when you close the IEagleApiResult instance. // If you want to preserve you'll have to move/copy them here. } break; case EagleApiResultStatus.TIMEOUT: // TIMEOUT - report an error break; case EagleApiResultStatus.ERROR: // ERROR - use result.getErrorMessage to get the error message break; case EagleApiResultStatus.STOPPED: // The request execution was stopped, when eaglemlApi.stop() is called break; case EagleApiResultStatus.NO_DATA: // The no data result retrieved. Report this back. break; } } |
Asynchronous Execution with a Callback
The following is an example of an asynchronous execution with a callback.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
• // THIS IS A SAMPLE OF THE REQUEST EXECUTION. REQUESTS CAN BE EXECUTED ONLY AFTER THE eaglemlApi.start() is called. Map<String, String> parameters = new HashMap<String, String>(); // Add the parameters required to build an URI // Generate a unique correlationId // Add the attachments for the POST request eaglemlApi.executeRestRequestAsync("GET", "/eagle/v2/manager-relationships", parameters, correlationId, businessTaskId, null, 600 /* timeout in seconds */, (result, exception) -> { if(exception != null) { // There was an exception while executing the request. Process it. } else { switch(result.getResultStatus()) { case EagleApiResultStatus.SUCCESS: List<IEagleApiResultDataFile> files = result.getDataFiles(); for (IEagleApiResultDataFile file : files) { // See the java doc for the IEagleApiResultDataFile. The file will represent one extract. // Currently EaglemlApi will produce only one file. // You can get a stream of uncompressed data: try(InputStream stream = file.getStream()) { // Parse the extract and execute any transformations - for example to JSON } // If needed you can get the compressed data and get access to the file with the file.isInMemory(), file.getData(), file.getFile() // The temporary files (if any) created while processing the extracts will be removed when you close the IEagleApiResult instance. // If you want to preserve you'll have to move/copy them here. } break; case EagleApiResultStatus.TIMEOUT: // TIMEOUT - report an error break; case EagleApiResultStatus.ERROR: // ERROR - use result.getErrorMessage to get the error message break; case EagleApiResultStatus.STOPPED: // The request execution was stopped, when eaglemlApi.stop() is called break; case EagleApiResultStatus.NO_DATA: // The extract resulted in no data retrieved. Report this back. break; } } }); |
Execute the Request Synchronously
You can execute the request synchronously by following the sample below. Requests can be executed only after the eaglemlApi.start() has been called.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
// THIS IS A SAMPLE OF THE REQUEST EXECUTION. REQUESTS CAN BE EXECUTED ONLY AFTER THE eaglemlApi.start() is called. Map<String, String> parameters = new HashMap<String, String>(); // Add the parameters required to build an URI // Generate a unique correlationId // Add the attachments for the POST request try (IEagleApiResult result = eaglemlApi.executeRequestAsync("GET", "/eagle/v2/manager-relationships", parameters, correlationId, null, 600 /* timeout in seconds */)) { switch(result.getResultStatus()) { case EagleApiResultStatus.SUCCESS: List<IEagleApiResultDataFile> files = result.getDataFiles(); for (IEagleApiResultDataFile file : files) { // See the java doc for the IEagleApiResultDataFile. The file will represent one extract. // Currently EaglemlApi will produce only one file. // You can get a stream of uncompressed data: try(InputStream stream = file.getStream()) { // Parse the extract and execute any transformations - for example to JSON } // If needed you can get the compressed data and get access to the file with the file.isInMemory(), file.getData(), file.getFile() // The temporary files (if any) created while processing the extracts will be removed when you close the IEagleApiResult instance. // If you want to preserve you'll have to move/copy them here. } break; case EagleApiResultStatus.TIMEOUT: // TIMEOUT - report an error break; case EagleApiResultStatus.ERROR: // ERROR - use result.getErrorMessage to get the error message break; case EagleApiResultStatus.STOPPED: // The request execution was stopped, when eaglemlApi.stop() is called break; case EagleApiResultStatus.NO_DATA: // The extract resulted in no data retrieved. Report this back. break; } } catch(EagleApiException e) { // Process the exception }; |
Command Line Interface for Executing REST Requests
Synchronous Mode
To enable synchronous mode, enter the following in the console:
...
To launch a single request, you can determine all necessary parameters directly in console with the "-data=" option. For example:(The contentType and pathToFile are optional.)
No Format |
---|
-data="httpMethod|httpPath|basecorrid|contentType|pathToFile" |
The contentType and pathToFile are optional.
When you want to launch several extracts or want to read all parameters from a file, use the "-file=" option:
...
No Format |
---|
-file="C:\Users\User\Documents\my_config.txt" |
In asynchronous mode, the client executes the following steps.
...
do the following:
- Create a callback. It is represented by a Jetty HTTP Servlet server which starts to listen on a random free port. You can set a timeout here. When it expires, the callback is unregistered and the timeout error is displayed. For example, a 5 min t/o:
No Format |
---|
-timeout=300 |
2. The client then executes REST Execute a REST request. The request contains x-eagle-rest-callback header with the IP address of the Eagle API client, the communication port and /eagleapicallback suffix. For example:
...