1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase;
20
21 import java.io.IOException;
22
23 import javax.management.MBeanServerConnection;
24 import javax.management.remote.JMXConnector;
25 import javax.management.remote.JMXConnectorFactory;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.hadoop.conf.Configuration;
30 import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
31 import org.apache.hadoop.hbase.testclassification.MediumTests;
32 import org.apache.hadoop.hbase.util.JVMClusterUtil;
33 import org.junit.AfterClass;
34 import org.junit.Assert;
35 import static org.junit.Assert.fail;
36 import org.junit.BeforeClass;
37 import org.junit.Rule;
38 import org.junit.Test;
39 import org.junit.experimental.categories.Category;
40 import org.junit.rules.ExpectedException;
41
42
43
44 @Category(MediumTests.class)
45 public class TestJMXListener {
46 private static final Log LOG = LogFactory.getLog(TestJMXListener.class);
47 private static HBaseTestingUtility UTIL = new HBaseTestingUtility();
48 private static int connectorPort = UTIL.randomFreePort();
49
50 @BeforeClass
51 public static void setupBeforeClass() throws Exception {
52 Configuration conf = UTIL.getConfiguration();
53
54 conf.set(CoprocessorHost.REGIONSERVER_COPROCESSOR_CONF_KEY,
55 JMXListener.class.getName());
56 conf.setInt("regionserver.rmi.registry.port", connectorPort);
57
58 UTIL.startMiniCluster();
59 }
60
61 @AfterClass
62 public static void tearDownAfterClass() throws Exception {
63 UTIL.shutdownMiniCluster();
64 }
65
66 @Test
67 public void testStart() throws Exception {
68 JMXConnector connector = JMXConnectorFactory.connect(
69 JMXListener.buildJMXServiceURL(connectorPort,connectorPort));
70
71 MBeanServerConnection mb = connector.getMBeanServerConnection();
72 String domain = mb.getDefaultDomain();
73 Assert.assertTrue("default domain is not correct",
74 !domain.isEmpty());
75 connector.close();
76
77 }
78
79
80 @Rule
81 public ExpectedException expectedEx = ExpectedException.none();
82 @Test
83 public void testStop() throws Exception {
84 MiniHBaseCluster cluster = UTIL.getHBaseCluster();
85 LOG.info("shutdown hbase cluster...");
86 cluster.shutdown();
87 LOG.info("wait for the hbase cluster shutdown...");
88 cluster.waitUntilShutDown();
89
90 JMXConnector connector = JMXConnectorFactory.newJMXConnector(
91 JMXListener.buildJMXServiceURL(connectorPort,connectorPort), null);
92 expectedEx.expect(IOException.class);
93 connector.connect();
94
95 }
96
97 @Test
98 public void testGetRegionServerCoprocessors() throws Exception {
99 for (JVMClusterUtil.RegionServerThread rs : UTIL.getHBaseCluster().getRegionServerThreads()) {
100 boolean find = false;
101 for (String s : rs.getRegionServer().getRegionServerCoprocessors()) {
102 if (s.equals(JMXListener.class.getSimpleName())) {
103 find = true;
104 break;
105 }
106 }
107 if (!find) {
108 fail("where is the JMXListener?");
109 }
110 }
111 }
112
113 }