parent
f0a35766b8
commit
59c1f3bd43
@ -0,0 +1,27 @@
|
|||||||
|
package com.bsmlab.dfx.agent.config.jmx;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
|
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class DfxAgentControl implements DfxAgentControlMBean {
|
||||||
|
private final ConfigurableApplicationContext ctx;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String status() {
|
||||||
|
return "OK";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void shutdown(int exitCode, int delaySeconds) {
|
||||||
|
new Thread(() -> {
|
||||||
|
try {
|
||||||
|
if (delaySeconds > 0) Thread.sleep(delaySeconds * 1000L);
|
||||||
|
} catch (InterruptedException ignored) { }
|
||||||
|
|
||||||
|
int code = SpringApplication.exit(ctx, () -> exitCode);
|
||||||
|
System.exit(code);
|
||||||
|
}, "dfxagent-jmx-shutdown").start();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,6 @@
|
|||||||
|
package com.bsmlab.dfx.agent.config.jmx;
|
||||||
|
|
||||||
|
public interface DfxAgentControlMBean {
|
||||||
|
String status();
|
||||||
|
void shutdown(int exitCode, int delaySeconds);
|
||||||
|
}
|
||||||
@ -0,0 +1,28 @@
|
|||||||
|
package com.bsmlab.dfx.agent.config.jmx;
|
||||||
|
|
||||||
|
import javax.management.MBeanServer;
|
||||||
|
import javax.management.ObjectName;
|
||||||
|
import java.lang.management.ManagementFactory;
|
||||||
|
|
||||||
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
|
|
||||||
|
public final class DfxJmxRegistrar {
|
||||||
|
|
||||||
|
private DfxJmxRegistrar() {}
|
||||||
|
|
||||||
|
public static ObjectName register(String agentId, ConfigurableApplicationContext configurableApplicationContext) {
|
||||||
|
try {
|
||||||
|
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
|
||||||
|
// agentId는 특수문자 가능하므로 quote 처리
|
||||||
|
String quotedAgentId = ObjectName.quote(agentId);
|
||||||
|
ObjectName name = new ObjectName("com.bsmlab.dfx.agent:type=Control,agentId=" + quotedAgentId);
|
||||||
|
if (!mbs.isRegistered(name)) {
|
||||||
|
mbs.registerMBean(new DfxAgentControl(configurableApplicationContext), name);
|
||||||
|
}
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
throw new IllegalStateException("Failed to register JMX MBean for agentId=" + agentId, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in new issue