@PostConstruct public void init() { Config config = this.hazelcastInstance.getConfig(); String mapName = this.sessionProperties.getMapName(); MapConfig mapConfig = config.getMapConfigOrNull(mapName); if (mapConfig == null) { // @formatter:off MapAttributeConfig principalNameAttributeConfig = new MapAttributeConfig() .setName(HazelcastSessionRepository.PRINCIPAL_NAME_ATTRIBUTE) .setExtractor(PrincipalNameExtractor.class.getName()); // @formatter:on MapIndexConfig principalNameIndexConfig = new MapIndexConfig( HazelcastSessionRepository.PRINCIPAL_NAME_ATTRIBUTE, false); // @formatter:off mapConfig = new MapConfig(mapName) .addMapAttributeConfig(principalNameAttributeConfig) .addMapIndexConfig(principalNameIndexConfig); // @formatter:on config.addMapConfig(mapConfig); } }
/** * Creates {@link HazelcastInstance} for use in integration tests. * @param port the port for Hazelcast to bind to * @return the Hazelcast instance */ public static HazelcastInstance embeddedHazelcastServer(int port) { MapAttributeConfig attributeConfig = new MapAttributeConfig() .setName(HazelcastSessionRepository.PRINCIPAL_NAME_ATTRIBUTE) .setExtractor(PrincipalNameExtractor.class.getName()); Config config = new Config(); config.getNetworkConfig() .setPort(port); config.getMapConfig(HazelcastSessionRepository.DEFAULT_SESSION_MAP_NAME) .addMapAttributeConfig(attributeConfig) .addMapIndexConfig(new MapIndexConfig( HazelcastSessionRepository.PRINCIPAL_NAME_ATTRIBUTE, false)); return Hazelcast.newHazelcastInstance(config); }
@Test public void testMapIndexInitialization() { Config config = new Config(); MapConfig mapConfig = config.getMapConfig("testMapIndexInitialization"); mapConfig.addMapIndexConfig(new MapIndexConfig("name", false)); mapConfig.addMapIndexConfig(new MapIndexConfig("age", true)); HazelcastInstance hz = Hazelcast.newHazelcastInstance(config); IMap map = hz.getMap(mapConfig.getName()); CMap cmap = TestUtil.getCMap(hz, mapConfig.getName()); Map<Expression, Index> indexes = cmap.getMapIndexService().getIndexes(); assertEquals(2, indexes.size()); for (Entry<Expression, Index> e : indexes.entrySet()) { Index index = e.getValue(); if ("name".equals(e.getKey().toString())) { assertFalse(index.isOrdered()); } else if ("age".equals(e.getKey().toString())) { assertTrue(index.isOrdered()); } else { fail("Unknown expression: " + e.getKey() + "! Has toString() of GetExpressionImpl changed?"); } } }
public static void main(String[] args) { MapConfig mapConfig = new MapConfig(); mapConfig.setName("cacheMap")// 设置Map名称 .setInMemoryFormat(InMemoryFormat.BINARY)// 设置内存格式 .setBackupCount(1);// 设置副本个数 mapConfig.getMapStoreConfig()// .setWriteDelaySeconds(60)// .setWriteBatchSize(1000);// 设置缓存格式 mapConfig.addMapIndexConfig(new MapIndexConfig().setAttribute("id").setOrdered(true));// 增加索引 mapConfig.addMapIndexConfig(new MapIndexConfig().setAttribute("name").setOrdered(true)); }
@Bean public HazelcastInstance hazelcastInstance() { MapAttributeConfig attributeConfig = new MapAttributeConfig() .setName(HazelcastSessionRepository.PRINCIPAL_NAME_ATTRIBUTE) .setExtractor(PrincipalNameExtractor.class.getName()); Config config = new Config(); config.getMapConfig(HazelcastSessionRepository.DEFAULT_SESSION_MAP_NAME) // <2> .addMapAttributeConfig(attributeConfig) .addMapIndexConfig(new MapIndexConfig( HazelcastSessionRepository.PRINCIPAL_NAME_ATTRIBUTE, false)); return Hazelcast.newHazelcastInstance(config); // <3> }
@Bean(destroyMethod = "shutdown") public HazelcastInstance hazelcastInstance() { Config config = new Config(); int port = SocketUtils.findAvailableTcpPort(); config.getNetworkConfig() .setPort(port); System.out.println("Hazelcast port #: " + port); SerializerConfig serializer = new SerializerConfig() .setImplementation(new ObjectStreamSerializer()) .setTypeClass(Object.class); config.getSerializationConfig() .addSerializerConfig(serializer); MapAttributeConfig attributeConfig = new MapAttributeConfig() .setName(HazelcastSessionRepository.PRINCIPAL_NAME_ATTRIBUTE) .setExtractor(PrincipalNameExtractor.class.getName()); config.getMapConfig(HazelcastSessionRepository.DEFAULT_SESSION_MAP_NAME) .addMapAttributeConfig(attributeConfig) .addMapIndexConfig(new MapIndexConfig( HazelcastSessionRepository.PRINCIPAL_NAME_ATTRIBUTE, false)); return Hazelcast.newHazelcastInstance(config); }