1 package org.eclipse.aether.named.hazelcast;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
35
36 public final class HazelcastClientUtils
37 {
38 private final List<HazelcastInstance> servers = new ArrayList<>();
39
40
41
42
43 public String clusterName( Class<?> klazz )
44 {
45 return String.format( "%s-%s", klazz.getSimpleName(), UUID.randomUUID() );
46 }
47
48
49
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
60
61 public synchronized HazelcastInstance createMember( String clusterName )
62 {
63 return createMembers( 1, clusterName ).get( 0 );
64 }
65
66
67
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
86
87 public synchronized void cleanup()
88 {
89 servers.forEach( HazelcastInstance::shutdown );
90 servers.clear();
91 }
92 }