Search in sources :

Example 1 with Hazelcast

use of com.hazelcast.core.Hazelcast in project hazelcast by hazelcast.

the class TestBeansApplicationContext method testApplicationContext.

@Test
public void testApplicationContext() {
    assertTrue(HazelcastClient.getAllHazelcastClients().isEmpty());
    context.getBean("map2");
    assertEquals(1, Hazelcast.getAllHazelcastInstances().size());
    assertEquals(1, HazelcastClient.getAllHazelcastClients().size());
    HazelcastInstance hazelcast = Hazelcast.getAllHazelcastInstances().iterator().next();
    long distributedObjectsSize = hazelcast.getDistributedObjects().stream().filter(distributedObject -> !distributedObject.getName().startsWith(INTERNAL_JET_OBJECTS_PREFIX)).count();
    assertEquals(2, distributedObjectsSize);
    context.getBean("client");
    context.getBean("client");
    assertEquals(3, HazelcastClient.getAllHazelcastClients().size());
    HazelcastClientProxy client = (HazelcastClientProxy) HazelcastClient.getAllHazelcastClients().iterator().next();
    assertNull(client.getClientConfig().getManagedContext());
    int batchSize = client.getClientConfig().getQueryCacheConfigs().get("").get("cache1").getBatchSize();
    assertEquals(12, batchSize);
    assertFalse(client.getClientConfig().getMetricsConfig().isEnabled());
    HazelcastInstance instance = (HazelcastInstance) context.getBean("instance");
    assertEquals(1, Hazelcast.getAllHazelcastInstances().size());
    assertEquals(instance, Hazelcast.getAllHazelcastInstances().iterator().next());
    assertNull(instance.getConfig().getManagedContext());
}
Also used : RANDOM(com.hazelcast.config.EvictionPolicy.RANDOM) BeforeClass(org.junit.BeforeClass) HazelcastClient(com.hazelcast.client.HazelcastClient) QuickTest(com.hazelcast.test.annotation.QuickTest) QueueConfig(com.hazelcast.config.QueueConfig) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) Autowired(org.springframework.beans.factory.annotation.Autowired) MapConfig(com.hazelcast.config.MapConfig) EvictionConfig(com.hazelcast.config.EvictionConfig) Config(com.hazelcast.config.Config) HazelcastInstance(com.hazelcast.core.HazelcastInstance) AfterClass(org.junit.AfterClass) HazelcastTestSupport(com.hazelcast.test.HazelcastTestSupport) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Category(org.junit.experimental.categories.Category) ApplicationContext(org.springframework.context.ApplicationContext) PER_PARTITION(com.hazelcast.config.MaxSizePolicy.PER_PARTITION) Assert.assertNull(org.junit.Assert.assertNull) Hazelcast(com.hazelcast.core.Hazelcast) Assert.assertFalse(org.junit.Assert.assertFalse) ContextConfiguration(org.springframework.test.context.ContextConfiguration) HazelcastClientProxy(com.hazelcast.client.impl.clientside.HazelcastClientProxy) Assert.assertEquals(org.junit.Assert.assertEquals) HazelcastInstance(com.hazelcast.core.HazelcastInstance) HazelcastClientProxy(com.hazelcast.client.impl.clientside.HazelcastClientProxy) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 2 with Hazelcast

use of com.hazelcast.core.Hazelcast in project hazelcast by hazelcast.

the class HazelcastStarter method createInstanceViaHazelcast.

@SuppressWarnings("unchecked")
private static Object createInstanceViaHazelcast(HazelcastAPIDelegatingClassloader classloader, Config configTemplate) {
    try {
        Class<?> configClass = classloader.loadClass("com.hazelcast.config.Config");
        Object config = getConfig(classloader, configClass, configTemplate);
        Class<Hazelcast> hazelcastClass = (Class<Hazelcast>) classloader.loadClass("com.hazelcast.core.Hazelcast");
        System.out.println(hazelcastClass + " loaded by " + hazelcastClass.getClassLoader());
        Method newHazelcastInstanceMethod = hazelcastClass.getMethod("newHazelcastInstance", configClass);
        return newHazelcastInstanceMethod.invoke(null, config);
    } catch (ClassNotFoundException e) {
        throw rethrowGuardianException(e);
    } catch (NoSuchMethodException e) {
        throw rethrowGuardianException(e);
    } catch (IllegalAccessException e) {
        throw rethrowGuardianException(e);
    } catch (InvocationTargetException e) {
        throw rethrowGuardianException(e);
    } catch (InstantiationException e) {
        throw rethrowGuardianException(e);
    }
}
Also used : Hazelcast(com.hazelcast.core.Hazelcast) Proxy.isProxyClass(java.lang.reflect.Proxy.isProxyClass) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 3 with Hazelcast

use of com.hazelcast.core.Hazelcast in project jnosql-diana-driver by eclipse.

the class HazelcastKeyValueConfiguration method get.

/**
 * Creates a {@link HazelcastBucketManagerFactory} from configuration map
 * @param configurations the configuration map
 * @return the HazelCastBucketManagerFactory instance
 * @throws NullPointerException when configurations is null
 */
public HazelcastBucketManagerFactory get(Map<String, String> configurations) throws NullPointerException {
    List<String> servers = configurations.keySet().stream().filter(s -> s.startsWith("hazelcast-hoster-")).collect(Collectors.toList());
    Config config = new Config(configurations.getOrDefault("hazelcast-instanceName", "hazelcast-instanceName"));
    HazelcastInstance hazelcastInstance = Hazelcast.getOrCreateHazelcastInstance(config);
    return new DefaultHazelcastBucketManagerFactory(hazelcastInstance);
}
Also used : Config(com.hazelcast.config.Config) HazelcastInstance(com.hazelcast.core.HazelcastInstance) List(java.util.List) ConfigurationReader(org.jnosql.diana.driver.ConfigurationReader) Hazelcast(com.hazelcast.core.Hazelcast) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) HashMap(java.util.HashMap) KeyValueConfiguration(org.jnosql.diana.api.key.KeyValueConfiguration) Settings(org.jnosql.diana.api.Settings) Collectors(java.util.stream.Collectors) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Config(com.hazelcast.config.Config)

Example 4 with Hazelcast

use of com.hazelcast.core.Hazelcast in project sonarqube by SonarSource.

the class HazelcastMemberBuilder method build.

public HazelcastMember build() {
    Config config = new Config();
    // do not use the value defined by property sonar.cluster.name.
    // Hazelcast does not fail when joining a cluster with different name.
    // Apparently this behavior exists since Hazelcast 3.8.2 (see note
    // at http://docs.hazelcast.org/docs/3.8.6/manual/html-single/index.html#creating-cluster-groups)
    config.setClusterName("SonarQube");
    // Configure network
    NetworkConfig netConfig = config.getNetworkConfig();
    netConfig.setPort(port).setPortAutoIncrement(false).setReuseAddress(true);
    netConfig.getInterfaces().setEnabled(true).setInterfaces(singletonList(requireNonNull(networkInterface, "Network interface is missing")));
    JoinConfig joinConfig = netConfig.getJoin();
    joinConfig.getAwsConfig().setEnabled(false);
    joinConfig.getMulticastConfig().setEnabled(false);
    if (KUBERNETES.equals(type)) {
        joinConfig.getKubernetesConfig().setEnabled(true).setProperty("service-dns", requireNonNull(members, "Service DNS is missing")).setProperty("service-port", CLUSTER_NODE_HZ_PORT.getDefaultValue());
    } else {
        List<String> addressesWithDefaultPorts = Stream.of(this.members.split(",")).filter(host -> !host.isBlank()).map(String::trim).map(HazelcastMemberBuilder::applyDefaultPortToHost).collect(Collectors.toList());
        joinConfig.getTcpIpConfig().setEnabled(true);
        joinConfig.getTcpIpConfig().setMembers(requireNonNull(addressesWithDefaultPorts, "Members are missing"));
    }
    // We are not using the partition group of Hazelcast, so disabling it
    config.getPartitionGroupConfig().setEnabled(false);
    // Tweak HazelCast configuration
    config.setProperty("hazelcast.tcp.join.port.try.count", "10").setProperty("hazelcast.socket.bind.any", "false").setProperty("hazelcast.phone.home.enabled", "false").setProperty("hazelcast.logging.type", "slf4j");
    MemberAttributeConfig attributes = config.getMemberAttributeConfig();
    attributes.setAttribute(Attribute.NODE_NAME.getKey(), requireNonNull(nodeName, "Node name is missing"));
    attributes.setAttribute(Attribute.PROCESS_KEY.getKey(), requireNonNull(processId, "Process key is missing").getKey());
    return new HazelcastMemberImpl(Hazelcast.newHazelcastInstance(config));
}
Also used : ProcessId(org.sonar.process.ProcessId) Config(com.hazelcast.config.Config) MemberAttributeConfig(com.hazelcast.config.MemberAttributeConfig) KUBERNETES(org.sonar.process.cluster.hz.JoinConfigurationType.KUBERNETES) Collectors(java.util.stream.Collectors) Attribute(org.sonar.process.cluster.hz.HazelcastMember.Attribute) String.format(java.lang.String.format) Collections.singletonList(java.util.Collections.singletonList) NetworkConfig(com.hazelcast.config.NetworkConfig) List(java.util.List) Stream(java.util.stream.Stream) CLUSTER_NODE_HZ_PORT(org.sonar.process.ProcessProperties.Property.CLUSTER_NODE_HZ_PORT) JoinConfig(com.hazelcast.config.JoinConfig) Hazelcast(com.hazelcast.core.Hazelcast) Objects.requireNonNull(java.util.Objects.requireNonNull) Config(com.hazelcast.config.Config) MemberAttributeConfig(com.hazelcast.config.MemberAttributeConfig) NetworkConfig(com.hazelcast.config.NetworkConfig) JoinConfig(com.hazelcast.config.JoinConfig) NetworkConfig(com.hazelcast.config.NetworkConfig) JoinConfig(com.hazelcast.config.JoinConfig) MemberAttributeConfig(com.hazelcast.config.MemberAttributeConfig)

Aggregations

Hazelcast (com.hazelcast.core.Hazelcast)4 Config (com.hazelcast.config.Config)3 HazelcastInstance (com.hazelcast.core.HazelcastInstance)2 List (java.util.List)2 Objects.requireNonNull (java.util.Objects.requireNonNull)2 Collectors (java.util.stream.Collectors)2 HazelcastClient (com.hazelcast.client.HazelcastClient)1 HazelcastClientProxy (com.hazelcast.client.impl.clientside.HazelcastClientProxy)1 EvictionConfig (com.hazelcast.config.EvictionConfig)1 RANDOM (com.hazelcast.config.EvictionPolicy.RANDOM)1 JoinConfig (com.hazelcast.config.JoinConfig)1 MapConfig (com.hazelcast.config.MapConfig)1 PER_PARTITION (com.hazelcast.config.MaxSizePolicy.PER_PARTITION)1 MemberAttributeConfig (com.hazelcast.config.MemberAttributeConfig)1 NetworkConfig (com.hazelcast.config.NetworkConfig)1 QueueConfig (com.hazelcast.config.QueueConfig)1 HazelcastTestSupport (com.hazelcast.test.HazelcastTestSupport)1 QuickTest (com.hazelcast.test.annotation.QuickTest)1 String.format (java.lang.String.format)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1