View source | Discuss this page | Page history | Printable version   
Main Page
Upload file
What links here
Recent changes

PDF Books
Add page
Show collection (0 pages)
Collections help


How to Send and Receive Messages From Store To Central and ViceVersa



This howto gives insight in how to send messages from a store to the central server or to other stores. Messages can be used to sync transactional events or to trigger actions in other stores. Messages are an alternative to data syncing using Symmetric DS which works directly from the database model.

Before starting this howto make sure to read the howto setup a dev environment. It also makes sense to have a clear understanding of the states of an Openbravo Commerce server (offline, online, transitioning etc.).

Also the howto assumes that the reader is familiar with the main concepts/information provided by the retail developers guide.

Installing the how to module

The examples provided in this howto can be found in the Store Server Howto module. The howto module can be downloaded using the hg clone command in the modules directory:

 hg clone

After this execute the 'ant smartbuild -Dlocal=no' command in the main project directory.

Import Entry Table - Symmetric DS

As a transport mechanism Openbravo Commerce uses the import transaction table which is synced to other servers using Symmetric DS. So for inter-server messaging you need to install and configure Symmetric DS. See here for information on Symmetric DS setup.

Sending Messages

A message is formatted as a JSON string. So all the information which you want to send should be 'json-able'. On the receiving side it should be possible from the json to identify the message, to see what type of message it is. Most messages will probably have a type attribute in the json or something similar.

You can send messages to the central server or to other stores. Messages to a store should always be send using the organization id of the store, messages to the central server are send using the 0 organization.

The MobileServerController class provides several methods to send messages:

The method names are quite descriptive.

It is important to know that message sending is asynchronous. Messages are send and received in order, but it is possible that receiving code gets into an error and places a message in the error queue. This depends on the processing code. So message processing order on the receiving side is in principle the same as sending order but depends on the receiving processing logic.

Examples of calling the above methods:

      "{'type': 'howtoMessageFromStore', 'action': 'localQueryActionPerformed', 'organization':'"
          + OBContext.getOBContext().getCurrentOrganization().getId() + "'}");
            OBDal.getInstance().get(Organization.class, jsonObject.getString("organization")),
            "{'type': 'howtoMessageFromCentral', 'action': 'confirmed', 'organization':'"
                + OBContext.getOBContext().getCurrentOrganization().getId() + "'}");

Receiving Messages

To receive and process a message you need to write a message handler. The message handler extends the default MobileServerMessageHandler class. There are two methods in that class:

Here is an example of a message handler written for the central server:

public class HowtoCentralMessageHandler extends MobileServerMessageHandler {
  public boolean processMessage(Organization org, String json) {
    if (!MobileServerController.getInstance().isThisACentralServer()) {
      return false;
    try {
      final JSONObject jsonObject = new JSONObject(json);
      // check if we can handle the message
      if (jsonObject.has("type") && "howtoMessageFromStore".equals(jsonObject.get("type"))) {
        // we can, send a message back to the store that it was done
        System.err.println("Sending back message to the store");
        // send a message to the store server that a local action was performed
            OBDal.getInstance().get(Organization.class, jsonObject.getString("organization")),
            "{'type': 'howtoMessageFromCentral', 'action': 'confirmed', 'organization':'"
                + OBContext.getOBContext().getCurrentOrganization().getId() + "'}");
        return true;
      return false;
    } catch (Exception e) {
      throw new OBException(e);

The code starts by checking if the code itself is running in the correct server. This is needed in this example because this code is combined with code for a store server in one module. Then it checks if the type is the expected type and does something. In this case the code sends back a message to the store from which it received it.

Message or Request

Sending messages with the approach outlined here is different from sending a direct requests to other servers in the architecture.

Sending messages uses transactions to ensure that the message is only send if your transaction succeeds, in addition a message is always delivered and processed in the other system. If processing in the other system fails there is an infrastructure to record this failure and retry the message. With direct request/response the error handling and retry has to be done in custom code. Also after doing the request if the failure is in the callers system somehow the called system has to be notified of the failure.

Summarizing: use direct requests to query other systems or to obtain simple information like a transaction id. Use messages for triggering complex transactions in other systems.

Retrieved from ""

This page has been accessed 416 times. This page was last modified on 20 November 2015, at 08:42. Content is available under Creative Commons Attribution-ShareAlike 2.5 Spain License.