-
Notifications
You must be signed in to change notification settings - Fork 17
/
HazelcastConfigurer.java
164 lines (145 loc) · 5.89 KB
/
HazelcastConfigurer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* Copyright 2011-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.appng.core.service;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.appng.api.Environment;
import org.appng.api.Platform;
import org.appng.api.Scope;
import org.appng.api.support.environment.DefaultEnvironment;
import org.appng.api.support.environment.ScopedEnvironment;
import org.appng.core.controller.messaging.HazelcastReceiver;
import org.appng.core.controller.messaging.NodeEvent;
import org.appng.core.controller.messaging.NodeEvent.NodeState;
import com.hazelcast.client.HazelcastClient;
import com.hazelcast.client.config.ClientConfig;
import com.hazelcast.client.config.XmlClientConfigBuilder;
import com.hazelcast.cluster.Address;
import com.hazelcast.cluster.Member;
import com.hazelcast.cluster.MembershipEvent;
import com.hazelcast.cluster.MembershipListener;
import com.hazelcast.config.Config;
import com.hazelcast.config.XmlConfigBuilder;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import lombok.extern.slf4j.Slf4j;
/**
* Utility class to retrieve the {@link HazelcastInstance} to be used by appNG.
* This instance is then being used by {@link CacheService} and also by
* {@link HazelcastReceiver}.
*
* @author Matthias Müller
*/
@Slf4j
public class HazelcastConfigurer {
private static final String HAZELCAST_CLIENT_XML = "WEB-INF/conf/hazelcast-client.xml";
public static final String HAZELCAST_USE_CLIENT = "hazelcastUseClient";
private static HazelcastInstance instance;
private static boolean isClient = false;
HazelcastConfigurer() {
}
public static HazelcastInstance getInstance(PlatformProperties platformProperties) {
return getInstance(platformProperties, null, null);
}
public static HazelcastInstance getInstance(PlatformProperties platformProps, String clientId,
Environment environment) {
if (null == instance) {
if (null != platformProps) {
try {
String providerType = System.getProperty("hazelcast.jcache.provider.type");
Boolean useClient = platformProps.getBoolean(HAZELCAST_USE_CLIENT, false);
if ("client".equals(providerType) || useClient) {
String appNGData = platformProps.getString(Platform.Property.APPNG_DATA);
FileInputStream clientIs = new FileInputStream(new File(appNGData, HAZELCAST_CLIENT_XML));
ClientConfig clientConfig = new XmlClientConfigBuilder(clientIs).build();
if (StringUtils.isNotBlank(clientId)) {
clientConfig.setInstanceName(clientConfig.getInstanceName() + "_" + clientId);
}
instance = HazelcastClient.getHazelcastClientByName(clientConfig.getInstanceName());
if (null == instance) {
instance = HazelcastClient.newHazelcastClient(clientConfig);
LOGGER.info("Created new client '{}' for ID '{}'", instance.getName(), clientId);
} else {
LOGGER.info("Using existing client '{}' for ID '{}'", instance.getName(), clientId);
}
isClient = true;
} else {
InputStream cacheConfig = platformProps.getCacheConfig();
if (null != cacheConfig) {
Config config = new XmlConfigBuilder(cacheConfig).build();
instance = Hazelcast.getOrCreateHazelcastInstance(config);
LOGGER.info("Using {}", instance);
}
}
instance.getCluster().addMembershipListener(getMembershipListener(environment));
} catch (IOException e) {
LOGGER.error("failed to create Hazelcast instance!", e);
}
}
} else if (isClient) {
LOGGER.info("Using existing client '{}' for ID '{}'", instance.getName(), clientId);
}
if (null == instance) {
LOGGER.warn("No Hazelcast configuration could be found, using default!");
instance = Hazelcast.newHazelcastInstance();
instance.getCluster().addMembershipListener(getMembershipListener(environment));
LOGGER.info("Created default instance {}", instance);
}
return instance;
}
private static MembershipListener getMembershipListener(Environment environment) {
return new MembershipListener() {
final ScopedEnvironment scoped = ((DefaultEnvironment) environment).getEnvironment(Scope.PLATFORM);
@Override
public void memberRemoved(MembershipEvent me) {
Member member = me.getMember();
Address address = member.getAddress();
InetSocketAddress socketAddress = member.getSocketAddress();
String nodeId = socketAddress.getHostName();
LOGGER.info("Node removed: {} ({})", address, nodeId);
Map<String, NodeState> clusterState = scoped.getAttribute(NodeEvent.NODE_STATE);
NodeState removed = clusterState.remove(nodeId);
if (removed == null) {
LOGGER.warn("Failed removing node '{}' from cluster state.", nodeId);
} else {
LOGGER.info("Removed node '{}' from cluster state.", nodeId);
}
}
@Override
public void memberAdded(MembershipEvent me) {
Member member = me.getMember();
Address address = member.getAddress();
InetSocketAddress socketAddress = member.getSocketAddress();
LOGGER.info("Node added: {} ({})", address, socketAddress.getHostName());
}
};
}
public static void shutdown() {
if (null != instance) {
LOGGER.info("Shutting down instance {}", instance.getName());
instance.shutdown();
instance = null;
}
}
public static boolean isClient() {
return isClient;
}
}