Ce contenu n'est pas disponible dans la langue sélectionnée.
4.3. Grouping API Configuration
To configure JBoss Data Grid using the programmatic API, call the following:
Configuration c = new ConfigurationBuilder().clustering().hash().groups().enabled().build();
To configure JBoss Data Grid using XML, use the following:
<clustering>
<hash>
<groups enabled="true" />
</hash>
</clustering>
@Group annotation within the method to specify the intrinsic group. For example:
class User {
...
String office;
...
int hashCode() {
// Defines the hash for the key, normally used to determine location
...
}
// Override the location by specifying a group, all keys in the same
// group end up with the same owner
@Group
String getOffice() {
return office;
}
}
Note
String.
Grouper interface. The computeGroup method within the Grouper interface returns the group.
Grouper operates as an interceptor and passes previously computed values to the computeGroup() method. If defined, @Group determines which group is passed to the first Grouper, providing improved group control when using intrinsic groups.
grouper to determine a key's group, its keyType must be assignable from the target key.
Grouper:
public class KXGrouper implements Grouper<String> {
// A pattern that can extract from a "kX" (e.g. k1, k2) style key
// The pattern requires a String key, of length 2, where the first character is
// "k" and the second character is a digit. We take that digit, and perform
// modular arithmetic on it to assign it to group "1" or group "2".
private static Pattern kPattern = Pattern.compile("(^k)(<a>\\d</a>)$");
public String computeGroup(String key, String group) {
Matcher matcher = kPattern.matcher(key);
if (matcher.matches()) {
String g = Integer.parseInt(matcher.group(2)) % 2 + "";
return g;
} else
return null;
}
public Class<String> getKeyType() {
return String.class;
}
}
grouper uses the key class to extract the group from a key using a pattern. Information specified on the key class is ignored. Each grouper must be registered to be used.
When configuring JBoss Data Grid programmatically:
Configuration c = new ConfigurationBuilder().clustering().hash().groups().addGrouper(new KXGrouper()).build();
Or when configuring JBoss Data Grid using XML:
<clustering>
<hash>
<groups enabled="true">
<grouper class="com.acme.KXGrouper" />
</groups>
</hash>
</clustering>