/** * Establishes Hazelcast cluster and once required number of clients connect, execute multiple document insertion. */ @Override public void perform(DefaultProperties properties) throws IOException { Config config = new ClasspathXmlConfig("hazelcast/hazelcast-master.xml"); String address = properties.getProperty(ClusterConstants.CLUSTER_MASTER_PROP); config.getNetworkConfig().setPublicAddress(address); HazelcastInstance hz = Hazelcast.newHazelcastInstance(config); try { InsertProperties insProperties = new InsertProperties(properties); init(hz, properties, insProperties); InetSocketAddress establishedAddress = hz.getCluster().getLocalMember().getSocketAddress(); LOGGER.info("Cluster established at {}", establishedAddress); executeSend(properties, insProperties); LOGGER.info("Sending on master node finished"); waitForAllClientShutdown(hz.getClientService()); } catch (InterruptedException ex) { LOGGER.error("Thread interrupted: ", ex); } finally { hz.shutdown(); } }
@BeforeClass public static void createGrids() { /* Suppress Hazelcast log output to standard error which does not appear to be suppressible via Agent Server's * log4j.xml. */ // ConsoleOutputSuppressor.suppressStandardError(); final Config standardConfig1 = new ClasspathXmlConfig("hazelcast1.xml"), standardConfig2 = new ClasspathXmlConfig("hazelcast2.xml"); standardConfig1.setProperty("hazelcast.operation.call.timeout.millis", "3000"); standardConfig2.setProperty("hazelcast.operation.call.timeout.millis", "3000"); grid1 = Hazelcast.newHazelcastInstance(standardConfig1); grid2 = Hazelcast.newHazelcastInstance(standardConfig2); dataStructureFactory1 = HazelcastDataStructureFactory.getInstance(grid1); dataStructureFactory2 = HazelcastDataStructureFactory.getInstance(grid2); lockService1 = new DistributedLockUtils().new PublicDistributedLockService(dataStructureFactory1); lockService2 = new DistributedLockUtils().new PublicDistributedLockService(dataStructureFactory2); }
@Test public void testNativeClient() throws Exception { HazelcastInstance main = Hazelcast.newHazelcastInstance(new ClasspathXmlConfig("hazelcast-custom.xml")); Properties props = getDefaultProperties(); props.setProperty(CacheEnvironment.USE_NATIVE_CLIENT, "true"); props.setProperty(CacheEnvironment.NATIVE_CLIENT_GROUP, "dev-custom"); props.setProperty(CacheEnvironment.NATIVE_CLIENT_PASSWORD, "dev-pass"); props.setProperty(CacheEnvironment.NATIVE_CLIENT_ADDRESS, "localhost"); SessionFactory sf = createSessionFactory(props); HazelcastInstance hz = HazelcastAccessor.getHazelcastInstance(sf); assertTrue(hz instanceof HazelcastClient); assertEquals(1, main.getCluster().getMembers().size()); HazelcastClient client = (HazelcastClient) hz; ClientConfig clientConfig = client.getClientConfig(); assertEquals("dev-custom", clientConfig.getGroupConfig().getName()); assertEquals("dev-pass", clientConfig.getGroupConfig().getPassword()); Hazelcast.newHazelcastInstance(new ClasspathXmlConfig("hazelcast-custom.xml")); assertEquals(2, hz.getCluster().getMembers().size()); main.getLifecycleService().shutdown(); Thread.sleep(1000 * 2); // let client to reconnect assertEquals(1, hz.getCluster().getMembers().size()); sf.close(); Hazelcast.shutdownAll(); }
private void configureCluster(IConfig config) throws FileNotFoundException { LOG.info("Configuring embedded Hazelcast instance"); String interceptHandlerClassname = config.getProperty(BrokerConstants.INTERCEPT_HANDLER_PROPERTY_NAME); if (interceptHandlerClassname == null || !HZ_INTERCEPT_HANDLER.equals(interceptHandlerClassname)) { LOG.info("There are no Hazelcast intercept handlers. The server won't start a Hazelcast instance."); return; } String hzConfigPath = config.getProperty(BrokerConstants.HAZELCAST_CONFIGURATION); if (hzConfigPath != null) { boolean isHzConfigOnClasspath = this.getClass().getClassLoader().getResource(hzConfigPath) != null; Config hzconfig = isHzConfigOnClasspath ? new ClasspathXmlConfig(hzConfigPath) : new FileSystemXmlConfig(hzConfigPath); LOG.info("Starting Hazelcast instance. ConfigurationFile={}", hzconfig); hazelcastInstance = Hazelcast.newHazelcastInstance(hzconfig); } else { LOG.info("Starting Hazelcast instance with default configuration"); hazelcastInstance = Hazelcast.newHazelcastInstance(); } listenOnHazelCastMsg(); }
protected void withHazelcast(int numInstances, Consumer<HazelcastInstance> f) { int initialPort = 5701; List<HazelcastInstance> instances = IntStream .rangeClosed(1, numInstances) .mapToObj(i -> { ClasspathXmlConfig config = new ClasspathXmlConfig("hazelcast.xml"); config.setInstanceName("MyHazelcastInstance-" + (initialPort + i - 1)); return Hazelcast.newHazelcastInstance(config); }) .collect(Collectors.toList()); try { f.accept(instances.get(0)); } finally { instances .stream() .forEach(h -> h.getLifecycleService().shutdown()); Hazelcast.shutdownAll(); } }
public synchronized HazelcastInstance getHazelcast() { if (hazelcast == null) { Config config = new ClasspathXmlConfig("org/j2server/j2cache/cache/hazelcast/hazelcast-cache-config.xml"); config.setInstanceName("j2cache"); hazelcast = Hazelcast.newHazelcastInstance(config); } return hazelcast; }
public static void main(String[] args) throws FileNotFoundException { //加载配置 Config config = new ClasspathXmlConfig("org/palm/hazelcast/map/store/mapStoreConfig.xml"); //创建Hazelcast实力 HazelcastInstance ins = Hazelcast.newHazelcastInstance(config); //获取Map Map<Integer, String> map = ins.getMap("demo"); println(map.get(1));//输出第一条数据 map.put(11, "Moonbrook");//添加一条数据 println(map.get(11));//输出第一条数据 map.remove(11);//移除添加的数据 println(map.get(11));//输出被移除的数据 }
public static void main(String[] args) { // 从classpath加载配置文件 Config config = new ClasspathXmlConfig("xmlconfig/simple-config.xml"); // 获取网络配置 NetworkConfig netConfig = config.getNetworkConfig(); // 获取用户定义的map配置 MapConfig mapConfigXml = config.getMapConfig("demo.config"); // 获取系统默认的map配置 MapConfig mapConfigDefault = config.getMapConfig("default"); // 输出集群监听的起始端口号 System.out.println("Current port:" + netConfig.getPort()); // 输出监听端口的累加号 System.out.println("Current port count:" + netConfig.getPortCount()); // 输出自定义map的备份副本个数 System.out.println("Config map backup count:" + mapConfigXml.getBackupCount()); // 输出默认map的备份副本个数 System.out.println("Default map backup count:" + mapConfigDefault.getBackupCount()); // 测试创建Hazelcast实例并读写测试数据 HazelcastInstance instance1 = Hazelcast.newHazelcastInstance(config); HazelcastInstance instance2 = Hazelcast.newHazelcastInstance(config); Map<Integer, String> defaultMap1 = instance1.getMap("defaultMap"); defaultMap1.put(1, "testMap"); Map<Integer, String> configMap1 = instance1.getMap("configMap"); configMap1.put(1, "configMap"); Map<Integer, String> testMap2 = instance2.getMap("defaultMap"); System.out.println("Default map value:" + testMap2.get(1)); Map<Integer, String> configMap2 = instance2.getMap("configMap"); System.out.println("Config map value:" + configMap2.get(1)); }
@Bean public HazelcastInstance embeddedHazelcast() { Config hazelcastConfig = new ClasspathXmlConfig( "org/springframework/session/hazelcast/config/annotation/web/http/hazelcast-custom-map-name.xml"); NetworkConfig netConfig = new NetworkConfig(); netConfig.setPort(SocketUtils.findAvailableTcpPort()); hazelcastConfig.setNetworkConfig(netConfig); return Hazelcast.newHazelcastInstance(hazelcastConfig); }
@Bean public HazelcastInstance embeddedHazelcast() { Config hazelcastConfig = new ClasspathXmlConfig( "org/springframework/session/hazelcast/config/annotation/web/http/hazelcast-custom-idle-time-map-name.xml"); NetworkConfig netConfig = new NetworkConfig(); netConfig.setPort(SocketUtils.findAvailableTcpPort()); hazelcastConfig.setNetworkConfig(netConfig); return Hazelcast.newHazelcastInstance(hazelcastConfig); }
@Test public void testLiteMember() throws Exception { HazelcastInstance main = Hazelcast.newHazelcastInstance(new ClasspathXmlConfig("hazelcast-custom.xml")); Properties props = getDefaultProperties(); props.setProperty(CacheEnvironment.USE_LITE_MEMBER, "true"); SessionFactory sf = createSessionFactory(props); HazelcastInstance hz = HazelcastAccessor.getHazelcastInstance(sf); assertTrue(hz.getCluster().getLocalMember().isLiteMember()); assertEquals(2, main.getCluster().getMembers().size()); sf.close(); main.getLifecycleService().shutdown(); }
@Test public void testXmlPartitionGroupConfig() { Config config = new ClasspathXmlConfig("hazelcast-fullconfig.xml"); PartitionGroupConfig partitionGroupConfig = config.getPartitionGroupConfig(); Assert.assertFalse(partitionGroupConfig.isEnabled()); Assert.assertEquals(MemberGroupType.CUSTOM, partitionGroupConfig.getGroupType()); Assert.assertEquals(2, partitionGroupConfig.getMemberGroupConfigs().size()); }
public void doMain(final String[] args) throws UnknownHostException, SocketException { final Enumeration e = NetworkInterface.getNetworkInterfaces(); while (e.hasMoreElements()) { final Enumeration ee = ((NetworkInterface) e.nextElement()).getInetAddresses(); while (ee.hasMoreElements()) { final InetAddress inetAddress = (InetAddress) ee.nextElement(); if (inetAddress.isLinkLocalAddress()) { continue; } if (inetAddress.isLoopbackAddress()) { continue; } m_inetAddress = inetAddress; } } if (args.length > 0) { final HazelcastInstance hazelcastInstance = HazelcastClient.newHazelcastClient(); try { final String hostAddress = m_inetAddress == null ? "0.0.0.0" : m_inetAddress.getHostAddress(); hazelcastInstance.<String>getTopic(args[0]).publish(hostAddress); } finally { hazelcastInstance.shutdown(); } } else if (args.length == 0) { final Config config = new ClasspathXmlConfig("hazelcast.xml"); m_hazelcastInstance = Hazelcast.newHazelcastInstance(config); m_hazelcastInstance.<String>getTopic("stop").addMessageListener(this); m_hazelcastInstance.<String>getTopic("shutdown").addMessageListener(new MessageListener<String>() { @Override public void onMessage(final Message<String> message) { m_hazelcastInstance.shutdown(); } }); } }
protected void withHazelcast(int numInstances, Consumer<HazelcastInstance> f) { List<HazelcastInstance> hazelcastInstances = IntStream .rangeClosed(1, numInstances) .mapToObj(i -> Hazelcast.newHazelcastInstance(new ClasspathXmlConfig("hazelcast.xml"))) .collect(Collectors.toList()); try { f.accept(hazelcastInstances.get(0)); } finally { hazelcastInstances.forEach(h -> h.getLifecycleService().shutdown()); Hazelcast.shutdownAll(); } }
protected void withHazelcast(String configFilePath, int numInstances, Consumer<HazelcastInstance> consumer) { List<HazelcastInstance> hazelcastInstances = IntStream .rangeClosed(1, numInstances) .mapToObj(i -> { ClasspathXmlConfig config = new ClasspathXmlConfig(configFilePath); return Hazelcast.newHazelcastInstance(config); }) .collect(Collectors.toList()); hazelcastInstances.forEach(h -> h.getLifecycleService().shutdown()); }
protected void withHazelcast(int numInstances, Consumer<HazelcastInstance> consumer) { List<HazelcastInstance> hazelcastInstances = IntStream .rangeClosed(1, numInstances) .mapToObj(i -> Hazelcast.newHazelcastInstance(new ClasspathXmlConfig("hazelcast.xml"))) .collect(Collectors.toList()); try { consumer.accept(hazelcastInstances.get(0)); } finally { hazelcastInstances.forEach(h -> h.getLifecycleService().shutdown()); Hazelcast.shutdownAll(); } }
public static void main(String... args) { HazelcastInstance hazelcast = Hazelcast.newHazelcastInstance(new ClasspathXmlConfig("hazelcast.xml")); try { System .console() .readLine("[%s] Hazelcast Server Startup.", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); } finally { hazelcast.getLifecycleService().shutdown(); Hazelcast.shutdownAll(); } }
@Test public void testNativeClient() throws Exception { HazelcastInstance main = Hazelcast.newHazelcastInstance(new ClasspathXmlConfig("hazelcast-custom.xml")); Properties props = getDefaultProperties(); props.setProperty(Environment.CACHE_REGION_FACTORY, HazelcastCacheRegionFactory.class.getName()); props.setProperty(CacheEnvironment.USE_NATIVE_CLIENT, "true"); props.setProperty(CacheEnvironment.NATIVE_CLIENT_GROUP, "dev-custom"); props.setProperty(CacheEnvironment.NATIVE_CLIENT_PASSWORD, "dev-pass"); props.setProperty(CacheEnvironment.NATIVE_CLIENT_ADDRESS, "localhost"); SessionFactory sf = createSessionFactory(props); HazelcastInstance hz = HazelcastAccessor.getHazelcastInstance(sf); assertTrue(hz instanceof HazelcastClient); assertEquals(1, main.getCluster().getMembers().size()); HazelcastClient client = (HazelcastClient) hz; ClientConfig clientConfig = client.getClientConfig(); assertEquals("dev-custom", clientConfig.getGroupConfig().getName()); assertEquals("dev-pass", clientConfig.getGroupConfig().getPassword()); Hazelcast.newHazelcastInstance(new ClasspathXmlConfig("hazelcast-custom.xml")); assertEquals(2, hz.getCluster().getMembers().size()); main.getLifecycleService().shutdown(); Thread.sleep(1000 * 2); // let client to reconnect assertEquals(1, hz.getCluster().getMembers().size()); Session session = sf.openSession(); Transaction tx = session.beginTransaction(); session.save(new DummyEntity(1L, "dummy", 0, new Date())); tx.commit(); session.close(); sf.close(); Hazelcast.shutdownAll(); }
@Test public void testNativeClient() throws Exception { TestHazelcastFactory factory = new TestHazelcastFactory(); Config config = new ClasspathXmlConfig("hazelcast-custom.xml"); HazelcastInstance main = factory.newHazelcastInstance(config); Properties props = getDefaultProperties(); props.remove(CacheEnvironment.CONFIG_FILE_PATH_LEGACY); props.setProperty(Environment.CACHE_REGION_FACTORY, HazelcastCacheRegionFactory.class.getName()); props.setProperty(CacheEnvironment.USE_NATIVE_CLIENT, "true"); props.setProperty(CacheEnvironment.NATIVE_CLIENT_GROUP, "dev-custom"); props.setProperty(CacheEnvironment.NATIVE_CLIENT_PASSWORD, "dev-pass"); props.setProperty(CacheEnvironment.CONFIG_FILE_PATH,"hazelcast-client-custom.xml"); HazelcastMockInstanceLoader loader = new HazelcastMockInstanceLoader(); loader.configure(props); loader.setInstanceFactory(factory); SessionFactory sf = createSessionFactory(props, loader); final HazelcastInstance hz = HazelcastAccessor.getHazelcastInstance(sf); assertTrue(hz instanceof HazelcastClientProxy); assertEquals(1, main.getCluster().getMembers().size()); HazelcastClientProxy client = (HazelcastClientProxy) hz; ClientConfig clientConfig = client.getClientConfig(); assertEquals("dev-custom", clientConfig.getGroupConfig().getName()); assertEquals("dev-pass", clientConfig.getGroupConfig().getPassword()); assertTrue(clientConfig.getNetworkConfig().isSmartRouting()); assertTrue(clientConfig.getNetworkConfig().isRedoOperation()); factory.newHazelcastInstance(config); assertEquals(2, hz.getCluster().getMembers().size()); main.shutdown(); assertTrueEventually(new AssertTask() { @Override public void run() throws Exception { assertEquals(1, hz.getCluster().getMembers().size()); } }); assertEquals(1, hz.getCluster().getMembers().size()); Session session = sf.openSession(); Transaction tx = session.beginTransaction(); session.save(new DummyEntity(1L, "dummy", 0, new Date())); tx.commit(); session.close(); sf.close(); factory.shutdownAll(); }
@Test public void testNativeClient() throws Exception { TestHazelcastFactory factory = new TestHazelcastFactory(); Config config = new ClasspathXmlConfig("hazelcast-custom.xml"); HazelcastInstance main = factory.newHazelcastInstance(config); Properties props = getDefaultProperties(); props.remove(CacheEnvironment.CONFIG_FILE_PATH_LEGACY); props.setProperty(Environment.CACHE_REGION_FACTORY, HazelcastCacheRegionFactory.class.getName()); props.setProperty(CacheEnvironment.USE_NATIVE_CLIENT, "true"); props.setProperty(CacheEnvironment.NATIVE_CLIENT_GROUP, "dev-custom"); props.setProperty(CacheEnvironment.NATIVE_CLIENT_PASSWORD, "dev-pass"); props.setProperty(CacheEnvironment.NATIVE_CLIENT_ADDRESS, "localhost"); props.setProperty(CacheEnvironment.CONFIG_FILE_PATH,"hazelcast-client-custom.xml"); HazelcastMockInstanceLoader loader = new HazelcastMockInstanceLoader(); loader.configure(props); loader.setInstanceFactory(factory); SessionFactory sf = createSessionFactory(props, loader); final HazelcastInstance hz = HazelcastAccessor.getHazelcastInstance(sf); assertTrue(hz instanceof HazelcastClientProxy); assertEquals(1, main.getCluster().getMembers().size()); HazelcastClientProxy client = (HazelcastClientProxy) hz; ClientConfig clientConfig = client.getClientConfig(); assertEquals("dev-custom", clientConfig.getGroupConfig().getName()); assertEquals("dev-pass", clientConfig.getGroupConfig().getPassword()); assertTrue(clientConfig.getNetworkConfig().isSmartRouting()); assertTrue(clientConfig.getNetworkConfig().isRedoOperation()); factory.newHazelcastInstance(config); assertEquals(2, hz.getCluster().getMembers().size()); main.shutdown(); assertTrueEventually(new AssertTask() { @Override public void run() throws Exception { assertEquals(1, hz.getCluster().getMembers().size()); } }); assertEquals(1, hz.getCluster().getMembers().size()); Session session = sf.openSession(); Transaction tx = session.beginTransaction(); session.save(new DummyEntity(1L, "dummy", 0, new Date())); tx.commit(); session.close(); sf.close(); factory.shutdownAll(); }
@Test public void testNativeClient() throws Exception { TestHazelcastFactory factory = new TestHazelcastFactory(); Config config = new ClasspathXmlConfig("hazelcast-custom.xml"); HazelcastInstance main = factory.newHazelcastInstance(config); Properties props = getDefaultProperties(); props.remove(CacheEnvironment.CONFIG_FILE_PATH_LEGACY); props.setProperty(Environment.CACHE_REGION_FACTORY, HazelcastCacheRegionFactory.class.getName()); props.setProperty(CacheEnvironment.USE_NATIVE_CLIENT, "true"); props.setProperty(CacheEnvironment.NATIVE_CLIENT_GROUP, "dev-custom"); props.setProperty(CacheEnvironment.NATIVE_CLIENT_PASSWORD, "dev-pass"); props.setProperty(CacheEnvironment.NATIVE_CLIENT_ADDRESS, "localhost"); props.setProperty(CacheEnvironment.CONFIG_FILE_PATH,"hazelcast-client-custom.xml"); HazelcastMockInstanceLoader loader = new HazelcastMockInstanceLoader(); loader.configure(props); loader.setInstanceFactory(factory); SessionFactory sf = createSessionFactory(props, loader); final HazelcastInstance hz = HazelcastAccessor.getHazelcastInstance(sf); assertTrue(hz instanceof HazelcastClientProxy); assertEquals(1, main.getCluster().getMembers().size()); HazelcastClientProxy client = (HazelcastClientProxy) hz; ClientConfig clientConfig = client.getClientConfig(); assertEquals("dev-custom", clientConfig.getGroupConfig().getName()); assertEquals("dev-pass", clientConfig.getGroupConfig().getPassword()); assertTrue(clientConfig.getNetworkConfig().isSmartRouting()); assertTrue(clientConfig.getNetworkConfig().isRedoOperation()); factory.newHazelcastInstance(config); assertEquals(2, hz.getCluster().getMembers().size()); main.shutdown(); assertTrueEventually(new AssertTask() { @Override public void run() throws Exception { assertEquals(1, hz.getCluster().getMembers().size()); } }); Session session = sf.openSession(); Transaction tx = session.beginTransaction(); session.save(new DummyEntity(1L, "dummy", 0, new Date())); tx.commit(); session.close(); sf.close(); factory.shutdownAll(); }
public HazelcastInstanceMgr(String hazelcastConfigFile) { this.conf =new ClasspathXmlConfig(hazelcastConfigFile); }
public boolean startCluster() { state = State.starting; // Set the serialization strategy to use for transmitting objects between node clusters serializationStrategy = ExternalizableUtil.getInstance().getStrategy(); ExternalizableUtil.getInstance().setStrategy(new ClusterExternalizableUtil()); // Set session locator to use when in a cluster XMPPServer.getInstance().setRemoteSessionLocator(new RemoteSessionLocator()); // Set packet router to use to deliver packets to remote cluster nodes XMPPServer.getInstance().getRoutingTable().setRemotePacketRouter(new ClusterPacketRouter()); ClassLoader oldLoader = null; // Store previous class loader (in case we change it) oldLoader = Thread.currentThread().getContextClassLoader(); ClassLoader loader = new ClusterClassLoader(); Thread.currentThread().setContextClassLoader(loader); int retry = 0; do { try { Config config = new ClasspathXmlConfig(HAZELCAST_CONFIG_FILE); config.setInstanceName("openfire"); config.setClassLoader(loader); if (JMXManager.isEnabled() && HAZELCAST_JMX_ENABLED) { config.setProperty("hazelcast.jmx", "true"); config.setProperty("hazelcast.jmx.detailed", "true"); } hazelcast = Hazelcast.newHazelcastInstance(config); cluster = hazelcast.getCluster(); // Update the running state of the cluster state = cluster != null ? State.started : State.stopped; // Set the ID of this cluster node XMPPServer.getInstance().setNodeID(NodeID.getInstance(getClusterMemberID())); // CacheFactory is now using clustered caches. We can add our listeners. clusterListener = new ClusterListener(cluster); lifecycleListener = hazelcast.getLifecycleService().addLifecycleListener(clusterListener); membershipListener = cluster.addMembershipListener(clusterListener); break; } catch (Exception e) { if (retry < CLUSTER_STARTUP_RETRY_COUNT) { logger.warn("Failed to start clustering (" + e.getMessage() + "); " + "will retry in " + CLUSTER_STARTUP_RETRY_TIME + " seconds"); try { Thread.sleep(CLUSTER_STARTUP_RETRY_TIME*1000); } catch (InterruptedException ie) { /* ignore */ } } else { logger.error("Unable to start clustering - continuing in local mode", e); state = State.stopped; } } } while (retry++ < CLUSTER_STARTUP_RETRY_COUNT); if (oldLoader != null) { // Restore previous class loader Thread.currentThread().setContextClassLoader(oldLoader); } return cluster != null; }
public static void main(String[] args) { ClasspathXmlConfig xmlConfig = new ClasspathXmlConfig(HAZELCAST_CONFIGURATION_FILE); Hazelcast.newHazelcastInstance(xmlConfig); }
/** * Default constructor. */ private Holder() { this.hazelcastInstance = Hazelcast.newHazelcastInstance(new ClasspathXmlConfig("hazelcast.xml")); logger.info("Hazelcast instance has been launched, member ID: {}", this.getMemberId()); }
@Test public void testMR() throws IOException, ExecutionException, InterruptedException { final Config config = new ClasspathXmlConfig("hazelcast.xml"); final HazelcastInstance instance = Hazelcast.newHazelcastInstance(config); try { final IMap<Integer, SpatialPolygon> hexmap = instance.getMap("hexmap"); final SpatialPolygon spatialPolygon = new SpatialPolygon(); spatialPolygon.id = 0; spatialPolygon.polygon.startPath(0, 0); spatialPolygon.polygon.lineTo(10, 0); spatialPolygon.polygon.lineTo(10, 10); spatialPolygon.polygon.lineTo(0, 10); spatialPolygon.polygon.lineTo(0, 0); spatialPolygon.polygon.closeAllPaths(); spatialPolygon.polygon.queryEnvelope2D(spatialPolygon.envelope2D); hexmap.put(spatialPolygon.id, spatialPolygon); final IMap<Integer, SpatialPoint> geomap = instance.getMap("geomap"); final SpatialPoint spatialPoint = new SpatialPoint(); spatialPoint.id = 0; spatialPoint.point.setXY(5, 5); spatialPoint.point.queryEnvelope2D(spatialPoint.envelope2D); geomap.put(spatialPoint.id, spatialPoint); final JobTracker tracker = instance.getJobTracker("default"); final Job<Integer, SpatialPoint> job = tracker.newJob(KeyValueSource.fromMap(geomap)); final ICompletableFuture<Map<Integer, Integer>> future = job. mapper(new FeatureMapper("hexmap")). reducer(new FeatureReducerFactory()). submit(); final Map<Integer, Integer> result = future.get(); Assert.assertEquals(1, result.size()); final Map.Entry<Integer, Integer> next = result.entrySet().iterator().next(); Assert.assertEquals(0, next.getKey().intValue()); Assert.assertEquals(1, next.getValue().intValue()); } finally { instance.shutdown(); } }
private static HazelcastInstance createInstance() { final Config config = new ClasspathXmlConfig("hazelcast.xml"); return Hazelcast.newHazelcastInstance(config); }
@Override public void setUpClass() { log.info("Starting Hazelcast server side cache provider."); Config config = new ClasspathXmlConfig(CONFIG); hazelcastInstance = Hazelcast.newHazelcastInstance(config); }
public boolean startCluster() { state = State.starting; // Set the serialization strategy to use for transmitting objects between node clusters serializationStrategy = ExternalizableUtil.getInstance().getStrategy(); ExternalizableUtil.getInstance().setStrategy(new ClusterExternalizableUtil()); // Set session locator to use when in a cluster XMPPServer.getInstance().setRemoteSessionLocator(new RemoteSessionLocator()); // Set packet router to use to deliver packets to remote cluster nodes XMPPServer.getInstance().getRoutingTable().setRemotePacketRouter(new ClusterPacketRouter()); ClassLoader oldLoader = null; // Store previous class loader (in case we change it) oldLoader = Thread.currentThread().getContextClassLoader(); ClassLoader loader = new ClusterClassLoader(); Thread.currentThread().setContextClassLoader(loader); int retry = 0; do { try { Config config = new ClasspathXmlConfig(HAZELCAST_CONFIG_FILE); config.setInstanceName("openfire"); config.setClassLoader(loader); if (JMXManager.isEnabled() && HAZELCAST_JMX_ENABLED) { config.setProperty("hazelcast.jmx", "true"); config.setProperty("hazelcast.jmx.detailed", "true"); } hazelcast = Hazelcast.newHazelcastInstance(config); cluster = hazelcast.getCluster(); // Update the running state of the cluster state = cluster != null ? State.started : State.stopped; // Set the ID of this cluster node XMPPServer.getInstance().setNodeID(NodeID.getInstance(getClusterMemberID())); // CacheFactory is now using clustered caches. We can add our listeners. clusterListener = new ClusterListener(cluster); hazelcast.getLifecycleService().addLifecycleListener(clusterListener); cluster.addMembershipListener(clusterListener); break; } catch (Exception e) { if (retry < CLUSTER_STARTUP_RETRY_COUNT) { logger.warn("Failed to start clustering (" + e.getMessage() + "); " + "will retry in " + CLUSTER_STARTUP_RETRY_TIME + " seconds"); try { Thread.sleep(CLUSTER_STARTUP_RETRY_TIME*1000); } catch (InterruptedException ie) { /* ignore */ } } else { logger.error("Unable to start clustering - continuing in local mode", e); state = State.stopped; } } } while (retry++ < CLUSTER_STARTUP_RETRY_COUNT); if (oldLoader != null) { // Restore previous class loader Thread.currentThread().setContextClassLoader(oldLoader); } return cluster != null; }
default <R> R withHazelcast(int numInstances, String xmlConfigFileName, Function<HazelcastInstance, R> fun) { return withHazelcast(numInstances, new ClasspathXmlConfig(xmlConfigFileName), fun); }
public boolean startCluster() { state = State.starting; // Set the serialization strategy to use for transmitting objects between node clusters serializationStrategy = ExternalizableUtil.getInstance().getStrategy(); ExternalizableUtil.getInstance().setStrategy(new ClusterExternalizableUtil()); // Set session locator to use when in a cluster XMPPServer.getInstance().setRemoteSessionLocator(new RemoteSessionLocator()); // Set packet router to use to deliver packets to remote cluster nodes XMPPServer.getInstance().getRoutingTable().setRemotePacketRouter(new ClusterPacketRouter()); ClassLoader oldLoader = null; // Store previous class loader (in case we change it) oldLoader = Thread.currentThread().getContextClassLoader(); ClassLoader loader = new ClusterClassLoader(); Thread.currentThread().setContextClassLoader(loader); int retry = 0; do { try { Config config = new ClasspathXmlConfig(HAZELCAST_CONFIG_FILE); config.setInstanceName("openfire"); hazelcast = Hazelcast.newHazelcastInstance(config); cluster = hazelcast.getCluster(); // Update the running state of the cluster state = cluster != null ? State.started : State.stopped; // Set the ID of this cluster node XMPPServer.getInstance().setNodeID(NodeID.getInstance(getClusterMemberID())); // CacheFactory is now using clustered caches. We can add our listeners. clusterListener = new ClusterListener(cluster); hazelcast.getLifecycleService().addLifecycleListener(clusterListener); cluster.addMembershipListener(clusterListener); break; } catch (Exception e) { if (retry < CLUSTER_STARTUP_RETRY_COUNT) { logger.warn("Failed to start clustering (" + e.getMessage() + "); " + "will retry in " + CLUSTER_STARTUP_RETRY_TIME + " seconds"); try { Thread.sleep(CLUSTER_STARTUP_RETRY_TIME*1000); } catch (InterruptedException ie) { /* ignore */ } } else { logger.error("Unable to start clustering - continuing in local mode", e); state = State.stopped; } } } while (retry++ < CLUSTER_STARTUP_RETRY_COUNT); if (oldLoader != null) { // Restore previous class loader Thread.currentThread().setContextClassLoader(oldLoader); } return cluster != null; }
public static void main(String[] args) throws Exception { Config conf =new ClasspathXmlConfig("hazelcast-consul-discovery-spi-example.xml"); HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance(conf); Thread.currentThread().sleep(300000); System.exit(0); }
public static void main(String[] args) throws Exception { Config conf =new ClasspathXmlConfig("hazelcast-etcd-discovery-spi-example.xml"); HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance(conf); Thread.currentThread().sleep(30000); System.exit(0); }