forked from infinispan/infinispan
/
AbstractProtocolServer.java
150 lines (121 loc) · 4.8 KB
/
AbstractProtocolServer.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
package org.infinispan.server.core;
import java.net.InetSocketAddress;
import javax.management.DynamicMBean;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import org.infinispan.commons.CacheException;
import org.infinispan.commons.logging.LogFactory;
import org.infinispan.configuration.global.GlobalConfiguration;
import org.infinispan.factories.components.ManageableComponentMetadata;
import org.infinispan.jmx.JmxUtil;
import org.infinispan.jmx.ResourceDMBean;
import org.infinispan.manager.EmbeddedCacheManager;
import org.infinispan.server.core.configuration.ProtocolServerConfiguration;
import org.infinispan.server.core.logging.Log;
import org.infinispan.server.core.transport.NettyTransport;
/**
* A common protocol server dealing with common property parameter validation and assignment and transport lifecycle.
*
* @author Galder Zamarreño
* @author wburns
* @since 4.1
*/
public abstract class AbstractProtocolServer<A extends ProtocolServerConfiguration> extends AbstractCacheIgnoreAware
implements ProtocolServer<A> {
private final Log log = LogFactory.getLog(getClass(), Log.class);
private final String protocolName;
protected NettyTransport transport;
protected EmbeddedCacheManager cacheManager;
protected A configuration;
private ObjectName transportObjName;
private MBeanServer mbeanServer;
protected AbstractProtocolServer(String protocolName) {
this.protocolName = protocolName;
}
protected void startInternal(A configuration, EmbeddedCacheManager cacheManager) {
this.configuration = configuration;
this.cacheManager = cacheManager;
if (log.isDebugEnabled()) {
log.debugf("Starting server with configuration: %s", configuration);
}
// Start default cache
startDefaultCache();
startTransport();
}
@Override
public final void start(A configuration, EmbeddedCacheManager cacheManager) {
try {
configuration.ignoredCaches().forEach(this::ignoreCache);
startInternal(configuration, cacheManager);
} catch (RuntimeException t) {
stop();
throw t;
}
}
protected void startTransport() {
InetSocketAddress address = new InetSocketAddress(configuration.host(), configuration.port());
transport = new NettyTransport(address, configuration, getQualifiedName(), cacheManager);
transport.initializeHandler(getInitializer());
// Register transport MBean regardless
registerTransportMBean();
transport.start();
}
protected void registerTransportMBean() {
GlobalConfiguration globalCfg = cacheManager.getCacheManagerConfiguration();
mbeanServer = JmxUtil.lookupMBeanServer(globalCfg);
String groupName = String.format("type=Server,name=%s", getQualifiedName());
String jmxDomain = JmxUtil.buildJmxDomain(globalCfg, mbeanServer, groupName);
// Pick up metadata from the component metadata repository
ManageableComponentMetadata meta = LifecycleCallbacks.componentMetadataRepo
.findComponentMetadata(transport.getClass()).toManageableComponentMetadata();
try {
// And use this metadata when registering the transport as a dynamic MBean
DynamicMBean dynamicMBean = new ResourceDMBean(transport, meta);
transportObjName = new ObjectName(String.format("%s:%s,component=%s", jmxDomain, groupName,
meta.getJmxObjectName()));
JmxUtil.registerMBean(dynamicMBean, transportObjName, mbeanServer);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
protected void unregisterTransportMBean() throws Exception {
if (mbeanServer != null && transportObjName != null) {
// Unregister mbean(s)
JmxUtil.unregisterMBean(transportObjName, mbeanServer);
}
}
protected String getQualifiedName() {
return protocolName + (configuration.name().length() > 0 ? "-" : "") + configuration.name();
}
@Override
public void stop() {
boolean isDebug = log.isDebugEnabled();
if (isDebug && configuration != null)
log.debugf("Stopping server listening in %s:%d", configuration.host(), configuration.port());
if (transport != null)
transport.stop();
try {
unregisterTransportMBean();
} catch (Exception e) {
throw new CacheException(e);
}
if (isDebug)
log.debug("Server stopped");
}
public EmbeddedCacheManager getCacheManager() {
return cacheManager;
}
public String getHost() {
return configuration.host();
}
public int getPort() {
return configuration.port();
}
@Override
public A getConfiguration() {
return configuration;
}
protected void startDefaultCache() {
cacheManager.getCache(configuration.defaultCacheName());
}
}