001package org.eclipse.aether.named.hazelcast; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import com.hazelcast.config.Config; 023import com.hazelcast.config.cp.CPSubsystemConfig; 024import com.hazelcast.core.Hazelcast; 025import com.hazelcast.core.HazelcastInstance; 026 027import java.util.ArrayList; 028import java.util.Collections; 029import java.util.List; 030 031/** 032 * Hazelcast Client connects to remote server (or servers, in case of cluster). This class is a helper to 033 * help create and validate remote Hazelcast environment, that Hazelcast Client will connect to. 034 */ 035public final class HazelcastClientUtils { 036 037 private static final int CP_CLUSTER_NODES = 3; 038 039 private List<HazelcastInstance> servers; 040 041 /** 042 * Creates a Hazelcast server instance, that client may connect to. 043 */ 044 public HazelcastClientUtils createSingleServer() { 045 servers = Collections.singletonList(Hazelcast.newHazelcastInstance()); 046 return this; 047 } 048 049 /** 050 * Creates a Hazelcast CP cluster, that client may connect to. When this method returns, cluster is not only 051 * created but it is properly formed as well. 052 */ 053 public HazelcastClientUtils createCpCluster() { 054 ArrayList<HazelcastInstance> instances = new ArrayList<>(CP_CLUSTER_NODES); 055 for (int i = 0; i < CP_CLUSTER_NODES; i++) { 056 HazelcastInstance instance = Hazelcast.newHazelcastInstance( 057 loadCPNodeConfig().setInstanceName("node" + i) 058 ); 059 instances.add(instance); 060 } 061 servers = instances; 062 063 // make sure HZ CP Cluster is ready 064 for (HazelcastInstance instance : servers) { 065 // this call will block until CP cluster if formed 066 // important thing here is that this blocking does not happen during timeout surrounded test 067 // hence, once this method returns, the CP cluster is "ready for use" without any delay. 068 instance.getCPSubsystem().getAtomicLong(instance.getName()); 069 } 070 return this; 071 } 072 073 /** 074 * Shuts down the created server(s) 075 */ 076 public void cleanup() { 077 if (servers != null) { 078 servers.forEach(HazelcastInstance::shutdown); 079 } 080 } 081 082 private Config loadCPNodeConfig() { 083 // "cluster" for CP needs some config tweak from the test/resources/hazelcast.xml 084 Config config = Config.load().setCPSubsystemConfig(new CPSubsystemConfig().setCPMemberCount(3)); 085 config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(true); 086 return config; 087 } 088}