Tutorial Steps
Introduction- The Weather Application
- Initializing the CacheManager
- Putting stuff in the cache
- Making entries expire
- Configuring the cache
- Clustering the application
- Listen to changes in the cluster
- Listen to changes in the cache
- Grouping entries together
- Temperature averages with streams
- Declarative configuration
The Weather App
In Infinispan the distribution of entries in the cluster is done in accordance with a consistent hashing algorithm. This algorithm uses the entry's key to compute a hash, and uses that hash to determine which node will be the primary owner, and which other nodes will act as backups. It is possible to control the distribution. In particular it might be useful, for performance reasons, to co-locate related entries on the same node using some grouping algorithm. In our weather application, for example, we may want to group weather locations per-country. For this we need to create a Grouper class which can compute a group name based on a key. Here's our example:
public static class LocationGrouper implements Grouper {
@Override
public String computeGroup(String key, String group) {
return key.split(",")[1].trim();
}
@Override
public Class getKeyType() {
return String.class;
}
}
The above code is quite simple: it merely splits the key using the comma as a delimiter and uses the second half (the "country") as the group. The hashing algorithm will be using the group, instead of the key, to compute the entry's hash. We need to specify the grouper we want to use in the configuration:
config.clustering().hash().groups().enabled().addGrouper(new LocationWeather.LocationGrouper());
It's time to run the application:
git checkout -f step-8
mvn clean package exec:exec # on terminal 1
mvn exec:exec # on terminal 2
If you examine the output of each node on this run, you will see that the event logs will be "paired" for all entries belonging to the same country. In my case, the "Romania" group got "hashed" to the second node:
Coordinator Node Output
-- Entry for Bucharest, Romania modified by another node in the cluster
-- Entry for Cluj-Napoca, Romania modified by another node in the cluster
In the next step we will see how to control the serialized form of the entries as they are transferred between nodes.