We have a cluster consisting of 4 machines, one of them for backoffice. When I change a property from local.properties, I want to register it to the database and all machines should get the new value.
To do so, I create a
CustomLocalProperty type for the new or altered property. When I change a property from backoffice machine, it is registered to the database and it is added to runtime properties via an interceptor(code piece is below) but other machines could not see the updated or new value.
How can I update this value from the other machines?
getConfiguration().addProperty(customLocalPropertyModel.getKey(),
customLocalPropertyModel.getValue());
Solved
Here I can't see any benefit of using CustomLocalProperty, because the configuration always loads from your properties files. Instead what you can think of to broadcast your changes to other nodes and also update your local.properties source so next server build/restart get the updated value.
You can take help of Cluster-Aware Events to publish your changes to other nodes. Also refer to how cache invalidation handle in hybris cluster.
When customer thinks of changing a local property someday (For instance: product.price.minThreshold) , I do not want customer to enter Hac to do this action.
Customer can change this property or add a new local property from Backoffice in my case. I used CustomLocalPropertyModel to preserve customer changes on local properties and thus when Server restarts again, I can not lose customer changes and I can get them from database together with local.properties.
Firstly, I create an interceptor for CustomLocalPropertyModel called CustomLocalPropertyInterceptor and then fire(publish) an event called CustomLocalPropertyEvent in the interceptor. This event implements ClusterAwareEvent. I can reach other nodes with ClusterAwareEvent.(see below from official documentation)
Because the SAP Hybris Commerce can run in a cluster, it is possible to send an event from one node to a specific second node or broadcast events across all nodes of the cluster.You then have to implement the method publish(int sourceNodeId, int targetNodeId) in a way to return true if you want to publish the event the from the cluster node with the ID sourceNodeId to the node with the ID targetNodeId. Basically, return the boolean value of whether you wish to publish events from cluster node sourceNodeId to cluster node targetNodeId. Sometimes you want only a special node to receive event, for example the node that hosts an index service that needs to be informed of data changes. You should then return true if targetNodeId is equal to the ID of the node that hosts that index server.
@Override
public boolean publish(int sourceNodeId, int targetNodeId) {
LOG.info("broadcast from all to all cluster nodes");
return true;
}
You can keep your data in the event. Then i create class named CustomPropertyEventListener in order to capture the event on the other nodes and thus these nodes get new or altered value and add it to the their own hac via code piece below.
configurationService.getConfiguration().addProperty(localPropertyKey,localPropertyValue);
No comments:
Post a Comment