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 java.util.ArrayList; 023import java.util.List; 024import java.util.UUID; 025import java.util.stream.IntStream; 026 027import com.hazelcast.client.HazelcastClient; 028import com.hazelcast.client.config.ClientConfig; 029import com.hazelcast.config.Config; 030import com.hazelcast.core.Hazelcast; 031import com.hazelcast.core.HazelcastInstance; 032 033/** 034 * Hazelcast test utilities. 035 */ 036public final class HazelcastClientUtils 037{ 038 private final List<HazelcastInstance> servers = new ArrayList<>(); 039 040 /** 041 * Creates similar but still randomized name. 042 */ 043 public String clusterName( Class<?> klazz ) 044 { 045 return String.format( "%s-%s", klazz.getSimpleName(), UUID.randomUUID() ); 046 } 047 048 /** 049 * Creates single Hazelcast client instance. 050 */ 051 public synchronized HazelcastInstance createClient( String clusterName ) 052 { 053 ClientConfig config = ClientConfig.load(); 054 config.setClusterName( clusterName ); 055 return HazelcastClient.newHazelcastClient( config ); 056 } 057 058 /** 059 * Creates single Hazelcast member instance. 060 */ 061 public synchronized HazelcastInstance createMember( String clusterName ) 062 { 063 return createMembers( 1, clusterName ).get( 0 ); 064 } 065 066 /** 067 * Creates given count of Hazelcast member instances. 068 */ 069 public synchronized List<HazelcastInstance> createMembers( int memberCount, String clusterName ) 070 { 071 Config config = Config.load(); 072 config.setClusterName( clusterName ); 073 ArrayList<HazelcastInstance> result = new ArrayList<>( memberCount ); 074 IntStream.range( 0, memberCount ).forEach( i -> { 075 config.setInstanceName( "node-" + i ); 076 HazelcastInstance instance = Hazelcast.newHazelcastInstance( config ); 077 result.add( instance ); 078 servers.add( instance ); 079 } 080 ); 081 return result; 082 } 083 084 /** 085 * Shuts down the created instances. 086 */ 087 public synchronized void cleanup() 088 { 089 servers.forEach( HazelcastInstance::shutdown ); 090 servers.clear(); 091 } 092}