Client-Server Communication

Table of contents

When JMap Pro opens, a connection to the associated JMap Server is established. This connection is used to direct the various system requests, including requests to load the project configuration and data as well as extension requests.

Communication between a JMap Pro (desktop) application and JMap Server is done by exchanging requests and responses. The requests and the responses are in fact serialized objects that must normally be programmed, and these objects will contain the properties required to execute the request and return the information to the client. For example, an application managing citizen requests might have a request and response with the following properties:

Request

Response

Name of requesterType of requestDescriptionx and y coordinates of the request’s location

Status of the saved request in the database Unique identifier generated upon save

For more information, refer to the Programming Extension Requests section.

Response status

Every response has a status that indicates if the request was executed successfully or if a problem occurred. The getStatus() method of the JMapExtensionResponse class allows you to get the status of the response. For the list of possible statuses and their description, refer to the documentation of the JMapExtensionResponse class.

Your extension should always check the status of a response before using it. If the status is not equal to JMapSRV_STS_SUCCESS, a special treatment should be done in order to manage the error situation. In this case, the getMessage() method allows you to generate a message explaining the cause of the error.

Sending requests to JMap Server

The JMapSrvConnection class is responsible for all communication between JMap client and JMap Server. The connection instance can be accessed using the JMap application context, as shown in the following example.

JMapSrvConnection jmapConn = JMapApplicationContext.getInstance().getConnection();

The following methods (inherited from the JMapNetworkConnection class) are used to send requests to JMap Server and to receive the responses.

Method

Description

Sends the request passed as a parameter to JMap Server and returns the response generated by JMap Server or by a server extension. This is a blocking method. It is well suited to situations where one must wait for the server’s response before continuing.

Sends the request passed as a parameter to JMap Server. This method is non-blocking. When the response is received, the callback() method of the client of the request is called. This method is well suited to situations where one must quickly give back control to the user and when one can process the response in an asynchronous manner.

The following code example shows how to use the executeRequest method.

MyRequest request = new MyRequest("This is a test", 555);

JMapSrvConnection jmapConn = JMapApplicationContext.getInstance().getConnection();

MyResponse response = (MyResponse)jmapConn.executeRequest(request); // Execution WILL block here

if (response.getStatus() == MyResponse.JMAPSRV_STS_SUCCESS)

{

// Do something useful with the response

}

else

{

// Handle error here

}

The following code example shows how to use the pushRequest method.

MyRequest request = new MyRequest("This is a test", 555);

request.setClient(new JMapRequestClient()

{

// Will be called when the response is received from JMapServer

@Override

public void callback(JMapRequest request, JMapResponse response)

{

if (response.getStatus() == MyResponse.JMAPSRV_STS_SUCCESS)

{

// Do something useful with the response

}

else

{

// Handle error here

}

}

});

JMapSrvConnection jmapConn = JMapApplicationContext.getInstance().getConnection(); jmapConn.pushRequest(request); // Execution WILL NOT block here

Dernière mise à jour

K2 Geospatial 2022