private Config createConfig() { Config config; try { config = new XmlConfigBuilder(Application.class.getClassLoader().getResource(CONFIG_NAME)).build(); } catch (IOException e) { logger.error(e.getMessage(), e); throw new Error(e); } config.getSerializationConfig().addDataSerializableFactory(SerializableFactory.ID, new SerializableFactory()); config.getSerializationConfig().getSerializerConfigs().add(new SerializerConfig().setTypeClass(JsonNode.class) .setImplementation(JsonSerializer.makePlain(JsonNode.class))); return config; }
@Test public void testTypedCustomSerializer_configuredBySubclassing() throws Exception { String mapName = randomMapName(); Config config = new Config(); SerializerConfig serializerConfig = new SerializerConfig(); serializerConfig.setClass(MySerializer.class); serializerConfig.setTypeClass(AnotherNonSerializableObject.class); config.getSerializationConfig().getSerializerConfigs().add(serializerConfig); HazelcastInstance member = hazelcastFactory.newHazelcastInstance(config); IMap<Integer, AnotherNonSerializableObject> myMap = member.getMap(mapName); myMap.put(0, new AnotherNonSerializableObject()); AnotherNonSerializableObject fromCache = myMap.get(0); assertEquals("deserialized", fromCache.name); }
/** * Add configuration to the supplied {@link Config} to support the use of a {@link HazelcastCommandExecutor}. * @param config The {@link Config} to configure. * @return The updated {@link Config}. */ public Config addCommandExecutorConfiguration(Config config) { SerializerConfig serializerConfig = new SerializerConfig() .setImplementation(RemoteCommandSerialiser.using( objectMapper, CommandTypeMatcher.matchingAgainst(typeInfoMap))) .setTypeClass(RemoteCommand.class); ManagedContext managedContext = CommandProcessingManagedContext .processingCommandsWith(dispatchingCommandProcessor); config.getSerializationConfig().addSerializerConfig(serializerConfig); config.setManagedContext(config.getManagedContext() == null ? managedContext : CompositeManagedContext.of(managedContext, config.getManagedContext())); config.addExecutorConfig(new ExecutorConfig(executorName, threadsPerNode)); return config; }
private void setupSerializers() { SerializerConfig scBNode = new SerializerConfig().setImplementation(new BNodeSerializer()).setTypeClass(KiWiAnonResource.class); hcConfiguration.getSerializationConfig().addSerializerConfig(scBNode); SerializerConfig scBoolean = new SerializerConfig().setImplementation(new BooleanLiteralSerializer()).setTypeClass(KiWiBooleanLiteral.class); hcConfiguration.getSerializationConfig().addSerializerConfig(scBoolean); SerializerConfig scDate = new SerializerConfig().setImplementation(new DateLiteralSerializer()).setTypeClass(KiWiDateLiteral.class); hcConfiguration.getSerializationConfig().addSerializerConfig(scDate); SerializerConfig scDouble = new SerializerConfig().setImplementation(new DoubleLiteralSerializer()).setTypeClass(KiWiDoubleLiteral.class); hcConfiguration.getSerializationConfig().addSerializerConfig(scDouble); SerializerConfig scInt = new SerializerConfig().setImplementation(new IntLiteralSerializer()).setTypeClass(KiWiIntLiteral.class); hcConfiguration.getSerializationConfig().addSerializerConfig(scInt); SerializerConfig scString = new SerializerConfig().setImplementation(new StringLiteralSerializer()).setTypeClass(KiWiStringLiteral.class); hcConfiguration.getSerializationConfig().addSerializerConfig(scString); SerializerConfig scTriple = new SerializerConfig().setImplementation(new TripleSerializer()).setTypeClass(KiWiTriple.class); hcConfiguration.getSerializationConfig().addSerializerConfig(scTriple); SerializerConfig scUri = new SerializerConfig().setImplementation(new UriSerializer()).setTypeClass(KiWiUriResource.class); hcConfiguration.getSerializationConfig().addSerializerConfig(scUri); }
private void init() { checkConfig(); if (usingHazelcast) { ClientConfig clientConfig = new ClientConfig(); clientConfig.getGroupConfig().setName(hazelcastUser).setPassword(hazelcastPassword); for (int i = 0; i < hazelcastServers.length; i++) { clientConfig.getNetworkConfig().addAddress(hazelcastServers[i]); } clientConfig.getSerializationConfig().getSerializerConfigs().add(new SerializerConfig().setTypeClass(MutableRepositoryItemExt.class).setImplementation(new MutableRepositoryItemExtHazelcastSerializer())); HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig); itemCache = client.getMap("itemCacheMap"); //itemCache = new HashMap<String, MutableRepositoryItemExt>(); } else if (usingMapDB) { MutableRepositoryItemExtMapDBSerializer MutableRepositoryItemExtMapDBSerializer = new MutableRepositoryItemExtMapDBSerializer(); MutableRepositoryItemExtArrayMapDBSerializer MutableRepositoryItemExtArrayMapDBSerializer = new MutableRepositoryItemExtArrayMapDBSerializer(); mapDB = DBMaker.newTempFileDB().transactionDisable().asyncWriteEnable().make(); itemCache = mapDB.createHashMap("itemCacheMap").keySerializer(org.mapdb.Serializer.STRING).valueSerializer(MutableRepositoryItemExtMapDBSerializer).make(); MutableRepositoryItemExtArrayMapDBSerializer.setItemCache(itemCache); queryCache = mapDB.createHashMap("queryCacheMap").keySerializer(org.mapdb.Serializer.STRING).valueSerializer(MutableRepositoryItemExtArrayMapDBSerializer).make(); } else { itemCache = new ConcurrentHashMap<String, MutableRepositoryItemExt>(); queryCache = new ConcurrentHashMap<String, MutableRepositoryItemExt>(); } }
/** * Use SubZero as a serializer for selected classes only. * * @param config Hazelcast configuration to inject SubZero into * @param classes classes Hazelcast should serialize via SubZero * @return Hazelcast configuration */ public static Config useForClasses(Config config, Class<?>...classes) { SerializationConfig serializationConfig = config.getSerializationConfig(); for (Class<?> clazz : classes) { SerializerConfig serializerConfig = new SerializerConfig(); Serializer<?> serializer = new Serializer(clazz); serializerConfig.setImplementation(serializer); serializerConfig.setTypeClass(clazz); serializationConfig.addSerializerConfig(serializerConfig); } return config; }
@Test public void useForClasses() { Config config = new Config(); SubZero.useForClasses(config, String.class); Collection<SerializerConfig> serializerConfigs = config.getSerializationConfig().getSerializerConfigs(); assertThat(serializerConfigs) .extracting("typeClass") .contains(String.class); }
private Config createTypedConfig() { Config config = new Config(); SerializerConfig serializerConfig = new SerializerConfig() .setTypeClassName("info.jerrinot.subzero.it.Person") .setClassName("info.jerrinot.subzero.Serializer"); SerializationConfig serializationConfig = config.getSerializationConfig(); serializationConfig.addSerializerConfig(serializerConfig); return config; }
/** * @param name Hazelcast instance name * @param config TypeSafe typeSafeConfig * @param serializerConfigs can be empty */ public TypesafeHazelcastConfig(@NonNull final String name, @NonNull final com.typesafe.config.Config config, @NonNull final Set<SerializerConfig> serializerConfigs) { this.hazelcastConfig = createHazelcastConfig( name, config, serializerConfigs.isEmpty() ? Optional.empty() : Optional.of(ImmutableSet.copyOf(serializerConfigs)) ); }
/** * Test of hazelcastConfigFactory method, of class HazelcastConfigFactory. */ @Test public void testHazelcastConfigFactory_withSerializers() { LOG.info("hazelcastConfigFactory"); String hazelcastInstanceName = "testHazelcastConfigFactory_withSerializers"; final Set<SerializerConfig> serializerConfigs = ImmutableSet.of( new SerializerConfig().setImplementation(new JsonObjectSerializer()).setTypeClass(JsonObject.class) ); final HazelcastConfigFactory factory1 = HazelcastConfigFactory.hazelcastConfigFactory(hazelcastInstanceName + 1, serializerConfigs); final com.hazelcast.config.Config hazelcastConfig1 = factory1.apply(config.getConfig(ConfigUtil.joinPath("Hazelcast", "application-1"))); final HazelcastInstance hazelcast1 = Hazelcast.newHazelcastInstance(hazelcastConfig1); final HazelcastConfigFactory factory2 = HazelcastConfigFactory.hazelcastConfigFactory(hazelcastInstanceName + 2, serializerConfigs); final com.hazelcast.config.Config hazelcastConfig2 = factory2.apply(config.getConfig(ConfigUtil.joinPath("Hazelcast", "application-2"))); final HazelcastInstance hazelcast2 = Hazelcast.newHazelcastInstance(hazelcastConfig2); test(hazelcast1, hazelcast2); final JsonObject json = Json.createObjectBuilder().add("a", 1).build(); final String mapName = "testHazelcastConfigFactory_withSerializers_map"; final Map<String, Object> map1 = hazelcast1.getMap(mapName); map1.put("json", json); final Map<String, Object> map2 = hazelcast2.getMap(mapName); final JsonObject json2 = (JsonObject) map2.get("json"); assertThat(json2.getInt("a"), is(1)); }
private HazelcastInstance createHazelcastInstance() { ClientConfig config = new ClientConfig(); config.getNetworkConfig().addAddress("localhost" + ":" + 5701); config.getGroupConfig().setName("distribution-app"); final SerializerConfig dolphinEventSerializerConfig = new SerializerConfig(). setImplementation(new CustomStreamSerializer()).setTypeClass(CustomEventFormat.class); config.getSerializationConfig().getSerializerConfigs().add(dolphinEventSerializerConfig); return HazelcastClient.newHazelcastClient(config); }
public synchronized HazelcastInstance getHazelcastInstance(HazelcastConfig configuration) { if (hazelcastInstance == null) { final String serverName = configuration.getServerName(); final String serverPort = configuration.getServerPort(); final String groupName = configuration.getGroupName(); LOG.debug("Hazelcast server name: {}", serverName); LOG.debug("Hazelcast server port: {}", serverPort); LOG.debug("Hazelcast group name: {}", groupName); final ClientConfig clientConfig = new ClientConfig(); clientConfig.getNetworkConfig().setConnectionAttemptLimit(configuration.getConnectionAttemptLimit()); clientConfig.getNetworkConfig().setConnectionAttemptPeriod(configuration.getConnectionAttemptPeriod()); clientConfig.getNetworkConfig().setConnectionTimeout(configuration.getConnectionTimeout()); clientConfig.getNetworkConfig().addAddress(serverName + ":" + serverPort); clientConfig.getGroupConfig().setName(groupName); clientConfig.setProperty(LOGGER_PROPERTY_NAME, LOGGER_PROPERTY_SLF4J_TYPE); final SerializerConfig dolphinEventSerializerConfig = new SerializerConfig(). setImplementation(new EventStreamSerializer()).setTypeClass(DolphinEvent.class); clientConfig.getSerializationConfig().getSerializerConfigs().add(dolphinEventSerializerConfig); hazelcastInstance = HazelcastClient.newHazelcastClient(clientConfig); } return hazelcastInstance; }
@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); }
public Set<SerializerConfig> create() { Set<SerializerConfig> set = new HashSet<>(); int id = 0; for (Class<?> c : classes) { id = id + 1; set.add(new SerializerConfig().setImplementation(new HzGsonSerializer(gson, id, c)).setTypeClass(c)); } setupCustom(set, id); return set; }
/** * Initializes the {@link HazelcastInstance} for this global runtime instance. * * @param runtime * the global runtime instance * @param master * member to connect to or null * @param localhost * the preferred ip address of this host or null * @param compact * reduce thread creation if set * @param kryo * use kryo serialization if set */ protected Transport(GlobalRuntimeImpl runtime, String master, String localhost, boolean compact, boolean kryo) { this.runtime = runtime; // config final Config config = new Config(); config.setProperty("hazelcast.logging.type", "none"); config.setProperty("hazelcast.wait.seconds.before.join", "0"); config.setProperty("hazelcast.socket.connect.timeout.seconds", "1"); config.setProperty("hazelcast.connection.monitor.max.faults", "0"); if (compact) { config.setProperty("hazelcast.operation.thread.count", "2"); config.setProperty("hazelcast.operation.generic.thread.count", "2"); config.setProperty("hazelcast.io.thread.count", "2"); config.setProperty("hazelcast.event.thread.count", "2"); config.addExecutorConfig( new ExecutorConfig(ExecutionService.ASYNC_EXECUTOR, 2)); config.addExecutorConfig( new ExecutorConfig(ExecutionService.SYSTEM_EXECUTOR, 2)); config.addExecutorConfig( new ExecutorConfig(ExecutionService.SCHEDULED_EXECUTOR, 2)); } // kryo if (kryo) { config.getSerializationConfig().addSerializerConfig( new SerializerConfig().setTypeClass(SerializableRunnable.class) .setImplementation(new KryoSerializer())); } config.addMapConfig( new MapConfig(APGAS_FINISH).setInMemoryFormat(InMemoryFormat.OBJECT)); // join config final JoinConfig join = config.getNetworkConfig().getJoin(); join.getMulticastConfig().setEnabled(false); join.getTcpIpConfig().setEnabled(true); if (localhost != null) { System.setProperty("hazelcast.local.localAddress", localhost); } if (master != null) { join.getTcpIpConfig().addMember(master); } config.setInstanceName(APGAS); hazelcast = Hazelcast.newHazelcastInstance(config); me = hazelcast.getCluster().getLocalMember(); allMembers = hazelcast.getList(APGAS_PLACES); allMembers.add(me); int id = 0; for (final Member member : allMembers) { if (member.getUuid().equals(me.getUuid())) { break; } ++id; } here = id; executor = hazelcast.getExecutorService(APGAS_EXECUTOR); }
private Config createHazelcastConfig(@NonNull final String name, @NonNull final com.typesafe.config.Config typeSafeConfig, @NonNull final Optional<Set<SerializerConfig>> serializerConfigs) { checkArgument(StringUtils.isNotBlank(name)); final Config _hazelcastConfig = new Config(); _hazelcastConfig.setInstanceName(name); _hazelcastConfig.setGroupConfig(ConfigUtils.getConfig(typeSafeConfig, "group-config") .map(this::groupConfig) .orElseThrow(new ConfigurationExceptionSupplier("group-config is required")) ); _hazelcastConfig.setNetworkConfig(ConfigUtils.getConfig(typeSafeConfig, "network-config") .map(c -> this.networkConfig(c, typeSafeConfig)) .orElseThrow(new ConfigurationExceptionSupplier("network-config is required")) ); ConfigUtils.getConfigList(typeSafeConfig, "map-configs").ifPresent(mapConfigs -> { mapConfigs.stream().map(this::mapConfig).forEach(_hazelcastConfig::addMapConfig); }); ConfigUtils.getConfigList(typeSafeConfig, "multi-map-configs").ifPresent(mapConfigs -> { mapConfigs.stream().map(this::multiMapConfig).forEach(_hazelcastConfig::addMultiMapConfig); }); ConfigUtils.getConfigList(typeSafeConfig, "queue-configs").ifPresent(queueConfigs -> { queueConfigs.stream().map(this::queueConfig).forEach(_hazelcastConfig::addQueueConfig); }); ConfigUtils.getConfigList(typeSafeConfig, "topic-configs").ifPresent(queueConfigs -> { queueConfigs.stream().map(this::topicConfig).forEach(_hazelcastConfig::addTopicConfig); }); ConfigUtils.getConfigList(typeSafeConfig, "list-configs").ifPresent(listConfigs -> { listConfigs.stream().map(this::listConfig).forEach(_hazelcastConfig::addListConfig); }); ConfigUtils.getConfigList(typeSafeConfig, "set-configs").ifPresent(listConfigs -> { listConfigs.stream().map(this::getSetConfig).forEach(_hazelcastConfig::addSetConfig); }); ConfigUtils.getConfigList(typeSafeConfig, "semaphore-configs").ifPresent(semaphoreConfigs -> { semaphoreConfigs.stream().map(this::semaphoreConfig).forEach(_hazelcastConfig::addSemaphoreConfig); }); ConfigUtils.getConfigList(typeSafeConfig, "executor-configs").ifPresent(executorConfigs -> { executorConfigs.stream().map(this::executorConfig).forEach(_hazelcastConfig::addExecutorConfig); }); _hazelcastConfig.setSerializationConfig(new SerializationConfig()); serializerConfigs.ifPresent(configs -> configs.stream().forEach(serializerConfig -> _hazelcastConfig.getSerializationConfig().addSerializerConfig(serializerConfig))); ConfigUtils.getConfigList(typeSafeConfig, "partition-group-config").ifPresent(partitionGroupConfig -> { partitionGroupConfig.stream().map(this::partitionConfig).forEach(_hazelcastConfig::setPartitionGroupConfig); }); // Application manages the lifecycle and registers a shutdown hook - we want to ensure this is the last service that is stopped _hazelcastConfig.setProperty("hazelcast.shutdownhook.enabled", "false"); // mapping hazelcast.jmx.enabled to hazelcast.jmx because using Typesafe typeSafeConfig, hazelcast.jmx is an object and cannot be set to a boolean ConfigUtils.getBoolean(typeSafeConfig, "properties", "hazelcast", "jmx", "enabled").ifPresent(jmxEnabled -> _hazelcastConfig.setProperty("hazelcast.jmx", Boolean.toString(jmxEnabled))); ConfigUtils.getConfig(typeSafeConfig, "properties").ifPresent(properties -> { _hazelcastConfig.setProperties(ConfigUtils.toProperties(properties)); }); ConfigUtils.getConfig(typeSafeConfig, "member-attribute-config") .map(this::memberAttributeConfig) .ifPresent(_hazelcastConfig::setMemberAttributeConfig); applyAdditionalConfiguration(_hazelcastConfig); return _hazelcastConfig; }
static HazelcastConfigFactory hazelcastConfigFactory(final String hazelcastInstanceName, final Set<SerializerConfig> serializerConfigs) { return config -> new TypesafeHazelcastConfig(hazelcastInstanceName, config, serializerConfigs).getHazelcastConfig(); }
private void setupCustom(Set<SerializerConfig> set, int avaliableIdIndex) { Type snapshotInventoryItemType = new TypeToken<Snapshot<InventoryItemAggregateRoot>>() {}.getType(); // so just one Snapshot serializer cfg in hazelcast ? TODO set.add(new SerializerConfig().setImplementation(new HzGsonTypedSerializer(gson, avaliableIdIndex+=1, snapshotInventoryItemType)).setTypeClass(Snapshot.class)); }
public HazelcastConfigFactory(MapConfig inventoryMapConfig, Set<SerializerConfig> serializers) { this.config = new Config(); config.addMapConfig(inventoryMapConfig); config.setProperty("hazelcast.shutdownhook.enabled", "false"); // to avoid conflit with camel // config.setUserContext(userContext); for (SerializerConfig sc : serializers) { config.getSerializationConfig().addSerializerConfig(sc); } }
@BeforeClass public static void setup() { simpleService = new SerializationServiceBuilder().build(); SerializationConfig config = new SerializationConfig(); SerializerConfig scBNode = new SerializerConfig().setImplementation(new BNodeSerializer()).setTypeClass(KiWiAnonResource.class); config.addSerializerConfig(scBNode); SerializerConfig scBoolean = new SerializerConfig().setImplementation(new BooleanLiteralSerializer()).setTypeClass(KiWiBooleanLiteral.class); config.addSerializerConfig(scBoolean); SerializerConfig scDate = new SerializerConfig().setImplementation(new DateLiteralSerializer()).setTypeClass(KiWiDateLiteral.class); config.addSerializerConfig(scDate); SerializerConfig scDouble = new SerializerConfig().setImplementation(new DoubleLiteralSerializer()).setTypeClass(KiWiDoubleLiteral.class); config.addSerializerConfig(scDouble); SerializerConfig scInt = new SerializerConfig().setImplementation(new IntLiteralSerializer()).setTypeClass(KiWiIntLiteral.class); config.addSerializerConfig(scInt); SerializerConfig scString = new SerializerConfig().setImplementation(new StringLiteralSerializer()).setTypeClass(KiWiStringLiteral.class); config.addSerializerConfig(scString); SerializerConfig scTriple = new SerializerConfig().setImplementation(new TripleSerializer()).setTypeClass(KiWiTriple.class); config.addSerializerConfig(scTriple); SerializerConfig scUri = new SerializerConfig().setImplementation(new UriSerializer()).setTypeClass(KiWiUriResource.class); config.addSerializerConfig(scUri); fullService = new SerializationServiceBuilder().setConfig(config).build(); }