View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
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.junit.AfterClass;
32  import org.junit.Assert;
33  import org.junit.BeforeClass;
34  import org.junit.Rule;
35  import org.junit.Test;
36  import org.junit.experimental.categories.Category;
37  import org.junit.rules.ExpectedException;
38  
39  
40  
41  @Category(MediumTests.class)
42  public class TestJMXListener {
43    private static final Log LOG = LogFactory.getLog(TestJMXListener.class);
44    private static HBaseTestingUtility UTIL = new HBaseTestingUtility();
45    private static int connectorPort = 61120;
46  
47    @BeforeClass
48    public static void setupBeforeClass() throws Exception {
49      Configuration conf = UTIL.getConfiguration();
50  
51      conf.set(CoprocessorHost.REGIONSERVER_COPROCESSOR_CONF_KEY,
52        JMXListener.class.getName());
53      conf.setInt("regionserver.rmi.registry.port", connectorPort);
54  
55      UTIL.startMiniCluster();
56    }
57  
58    @AfterClass
59    public static void tearDownAfterClass() throws Exception {
60      UTIL.shutdownMiniCluster();
61    }
62  
63    @Test
64    public void testStart() throws Exception {
65      JMXConnector connector = JMXConnectorFactory.connect(
66        JMXListener.buildJMXServiceURL(connectorPort,connectorPort));
67  
68      MBeanServerConnection mb = connector.getMBeanServerConnection();
69      String domain = mb.getDefaultDomain();
70      Assert.assertTrue("default domain is not correct",
71        !domain.isEmpty());
72      connector.close();
73  
74    }
75  
76    //shutdown hbase only. then try connect, IOException expected
77    @Rule
78    public ExpectedException expectedEx = ExpectedException.none();
79    @Test
80    public void testStop() throws Exception {
81      MiniHBaseCluster cluster = UTIL.getHBaseCluster();
82      LOG.info("shutdown hbase cluster...");
83      cluster.shutdown();
84      LOG.info("wait for the hbase cluster shutdown...");
85      cluster.waitUntilShutDown();
86  
87      JMXConnector connector = JMXConnectorFactory.newJMXConnector(
88        JMXListener.buildJMXServiceURL(connectorPort,connectorPort), null);
89      expectedEx.expect(IOException.class);
90      connector.connect();
91  
92    }
93  
94  
95  }