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

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

Search

How to Configure and User Custom Routers

Contents

Introduction

In SymmetricDS, routers define which captured data from a source node should be sent to which specific nodes in a target node group.

The synchronization module includes two predefined routers:

The synchronization module allows to create customized routers, with custom logic to decide which servers should be notified when a synchronized table is modified. This wiki page will explain how to implement custom routers.

The IDataRouter Interface

All routers must implement the IDataRouter interface, or extend a class that implements it.

public interface IDataRouter extends IExtensionPoint {
    public Set<String> routeToNodes(SimpleRouterContext context, DataMetaData dataMetaData, Set<Node> nodes, boolean initialLoad, boolean initialLoadSelectUsed, TriggerRouter triggerRouter);
    public void completeBatch(SimpleRouterContext context, OutgoingBatch batch);
    public void contextCommitted(SimpleRouterContext context);
    public boolean isConfigurable();
}

The AbstractDataRouter Class

The AbstractDataRouter class provides several helper methods that make easier the implementation of routers, for instance:

protected Map<String, String> getDataMap(DataMetaData dataMetaData, 
ISymmetricDialect symmetricDialect);

The getDataMap method returns a map columnName->value that contains the values for all the columns of the synchronized table that just has been modified. Usually this values will be used to decide the list of nodes that must be notified of this change.

protected Set<String> toNodeIds(Set<Node> nodes, Set<String> nodeIds);

Given a list of nodes it returns a set that contains the node ids. If a set is provided in the second argument, the returned list will include the values of that set too.

The AbstractClientFilteredRouter Class

This abstract router is defined in the org.openbravo.replication.symmetricds module, and should be the base for all the custom router implementations. This abstract router is in charge of making sure the changes on a given record are only routed to mobile servers defined for the same client. It exposes this abstract method that should be implemented by its subclasses:

protected abstract Set<String> doRouteToNodes(SimpleRouterContext context,
      DataMetaData dataMetaData, Set<Node> nodes, boolean initialLoad,
      boolean initialLoadSelectUsed, TriggerRouter triggerRouter);

It extends the AbstractDataRouter class, so its utility methods are available for its subclasses.

Defining a Custom Router

All custom router implementations must extend the AbstractClientFilteredRouter class.

Custom routers are instantiated using dependency injection, so the class that implements them must have the annotation@ApplicationScoped.

Custom routers are defined in the Custom Router window, and can be assigned to the synchronized tables in the Synchronize Table window.

Example

This router, defined for the UOM table, will only route the changes to the target nodes if the name of the unit of measure is not "QA".

@ApplicationScoped
public class TestCustomRouter extends AbstractClientFilteredRouter implements NamedExtensionPoint {
 
  @Override
  public Set<String> doRouteToNodes(SimpleRouterContext context, DataMetaData dataMetaData,
      Set<Node> nodes, boolean initialLoad, boolean initialLoadSelectUsed,
      TriggerRouter triggerRouter) {
    Set<String> nodeIds = null;
    Map<String, String> dataMap = getDataMap(dataMetaData, null);
    String uomName = dataMap.get("name");
    nodeIds = ("QA".equals(uomName) ? new HashSet<String>() : toNodeIds(nodes, null));
    return nodeIds;
  }
 
  @Override
  public String getName() {
    return "TestCustomRouter";
  }
}
CustomRouter1.png
CustomRouter2.png

Retrieved from "http://wiki.openbravo.com/wiki/How_to_Configure_and_User_Custom_Routers"

This page has been accessed 304 times. This page was last modified on 1 August 2016, at 13:21. Content is available under Creative Commons Attribution-ShareAlike 2.5 Spain License.