View Javadoc
1   package org.eclipse.aether.named.hazelcast;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.UUID;
25  import java.util.stream.IntStream;
26  
27  import com.hazelcast.client.HazelcastClient;
28  import com.hazelcast.client.config.ClientConfig;
29  import com.hazelcast.config.Config;
30  import com.hazelcast.core.Hazelcast;
31  import com.hazelcast.core.HazelcastInstance;
32  
33  /**
34   * Hazelcast test utilities.
35   */
36  public final class HazelcastClientUtils
37  {
38      private final List<HazelcastInstance> servers = new ArrayList<>();
39  
40      /**
41       * Creates similar but still randomized name.
42       */
43      public String clusterName( Class<?> klazz )
44      {
45          return String.format( "%s-%s", klazz.getSimpleName(), UUID.randomUUID() );
46      }
47  
48      /**
49       * Creates single Hazelcast client instance.
50       */
51      public synchronized HazelcastInstance createClient( String clusterName )
52      {
53          ClientConfig config = ClientConfig.load();
54          config.setClusterName( clusterName );
55          return HazelcastClient.newHazelcastClient( config );
56      }
57  
58      /**
59       * Creates single Hazelcast member instance.
60       */
61      public synchronized HazelcastInstance createMember( String clusterName )
62      {
63          return createMembers( 1, clusterName ).get( 0 );
64      }
65  
66      /**
67       * Creates given count of Hazelcast member instances.
68       */
69      public synchronized List<HazelcastInstance> createMembers( int memberCount, String clusterName )
70      {
71          Config config = Config.load();
72          config.setClusterName( clusterName );
73          ArrayList<HazelcastInstance> result = new ArrayList<>( memberCount );
74          IntStream.range( 0, memberCount ).forEach( i -> {
75                      config.setInstanceName( "node-" + i );
76                      HazelcastInstance instance = Hazelcast.newHazelcastInstance( config );
77                      result.add( instance );
78                      servers.add( instance );
79                  }
80          );
81          return result;
82      }
83  
84      /**
85       * Shuts down the created instances.
86       */
87      public synchronized void cleanup()
88      {
89          servers.forEach( HazelcastInstance::shutdown );
90          servers.clear();
91      }
92  }