The following two methods are added through the PartitionDefition interface:
/** * This sets a string as alias name * */ void setPartitionAlias(String aliasName); /** * This returns the partition alias string for this partition * * @return String */ String getPartitionAlias();
Two partition alias samples are provided as programming examples. One demonstrates how partition alias works, and the other shows how it works with the mixed partition aliases and non-alias partitions in the same application. The two samples follow:
Partition alias sample
public PartitionDefinition[] getPartitions() { try { //1-1 between partition and alias numOfAlias=numOfPartitions; partitionAlias= new String[numOfAlias]; } catch (Exception e) { … } PartitionDefinition[] rc = new PartitionDefinition[numOfPartitions]; for (int i = 1; i <= numOfPartitions; ++i) { rc[i - 1] = ivManager.createPartitionDefinition(PartitionAlias.PARTITION_PREFIX + padZeroToString(i + "", 6)); rc[i-1].setPartitionAlias(PartitionAlias.PARTITION_ALIAS + padZeroToString(i + "", 6)); } return rc; }
public class PartitionAlias_PartitionKey { /** * return the partition string as the partition key * @param partition * @return */ public static String pingAlias(String partitionAlias) { return partitionAlias; } /** * return the partition string as the partition key * @param partition * @return */ public static String ping(String partition) { return partition; }
Some methods can use partition alias context while other methods can use partition context.
Mixed partition alias and non-alias partition sample
public PartitionDefinition[] getPartitions() { try { numOfAlias=numOfPartitions/5; partitionAlias= new String[numOfAlias]; } catch (Exception e) { …… } PartitionDefinition[] rc = new PartitionDefinition[numOfPartitions]; for (int i = 1; i <= numOfPartitions; ++i) { rc[i - 1] = ivManager.createPartitionDefinition(MixedAliasNoAliasPartition.PARTITION_PREFIX + padZeroToString(i + "", 6)); if ((i-1)%5==0){ int k= (i-1)/5 + 1; rc[i-1].setPartitionAlias(MixedAliasNoAliasPartition.PARTITION_ALIAS + padZeroToString(k + "", 6)); } } return rc; }
Routing can use either partition alias context if it exists or partition context. If partition alias does not exist for this partition and client request has partition alias context, client requests will receive a NO_IMPLEMENT exception, but this no-alias partition can be routed with partition context. In any application, some partitions may have their aliases, and other partitions may have not any aliases.