-
Notifications
You must be signed in to change notification settings - Fork 10
/
MyCustomMetricsRegister.java
46 lines (34 loc) · 1.25 KB
/
MyCustomMetricsRegister.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
package com.moraes.metrics;
import javax.annotation.PreDestroy;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.Initialized;
import javax.enterprise.event.Observes;
import javax.inject.Inject;
import javax.management.*;
@ApplicationScoped
public class MyCustomMetricsRegister {
@Inject
private MBeanServer mBeanServer;
@Inject
private MyCustomMetricsMBean myCustomMetricsMBean;
private ObjectName name;
void init(@Observes @Initialized(ApplicationScoped.class) Object event)
throws NotCompliantMBeanException,
InstanceAlreadyExistsException,
MBeanRegistrationException,
MalformedObjectNameException {
final Class<? extends MyCustomMetricsMBean> objectClass = myCustomMetricsMBean.getClass();
name = new ObjectName(
String.format("%s:type=basic,name=%s", objectClass.getPackage().getName(), objectClass.getName())
);
mBeanServer.registerMBean(myCustomMetricsMBean, name);
}
@PreDestroy
void preDestroy() {
try {
mBeanServer.unregisterMBean(name);
} catch (InstanceNotFoundException | MBeanRegistrationException e) {
throw new RuntimeException(e);
}
}
}