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.apache.hadoop.hbase.testclassification.MediumTests;
32  import org.junit.AfterClass;
33  import org.junit.Assert;
34  import org.junit.BeforeClass;
35  import org.junit.Rule;
36  import org.junit.Test;
37  import org.junit.experimental.categories.Category;
38  import org.junit.rules.ExpectedException;
39  
40  
41  
42  @Category(MediumTests.class)
43  public class TestJMXListener {
44    private static final Log LOG = LogFactory.getLog(TestJMXListener.class);
45    private static HBaseTestingUtility UTIL = new HBaseTestingUtility();
46    private static int connectorPort = 61120;
47  
48    @BeforeClass
49    public static void setupBeforeClass() throws Exception {
50      Configuration conf = UTIL.getConfiguration();
51  
52      conf.set(CoprocessorHost.REGIONSERVER_COPROCESSOR_CONF_KEY,
53        JMXListener.class.getName());
54      conf.setInt("regionserver.rmi.registry.port", connectorPort);
55  
56      UTIL.startMiniCluster();
57    }
58  
59    @AfterClass
60    public static void tearDownAfterClass() throws Exception {
61      UTIL.shutdownMiniCluster();
62    }
63  
64    @Test
65    public void testStart() throws Exception {
66      JMXConnector connector = JMXConnectorFactory.connect(
67        JMXListener.buildJMXServiceURL(connectorPort,connectorPort));
68  
69      MBeanServerConnection mb = connector.getMBeanServerConnection();
70      String domain = mb.getDefaultDomain();
71      Assert.assertTrue("default domain is not correct",
72        !domain.isEmpty());
73      connector.close();
74  
75    }
76  
77    //shutdown hbase only. then try connect, IOException expected
78    @Rule
79    public ExpectedException expectedEx = ExpectedException.none();
80    @Test
81    public void testStop() throws Exception {
82      MiniHBaseCluster cluster = UTIL.getHBaseCluster();
83      LOG.info("shutdown hbase cluster...");
84      cluster.shutdown();
85      LOG.info("wait for the hbase cluster shutdown...");
86      cluster.waitUntilShutDown();
87  
88      JMXConnector connector = JMXConnectorFactory.newJMXConnector(
89        JMXListener.buildJMXServiceURL(connectorPort,connectorPort), null);
90      expectedEx.expect(IOException.class);
91      connector.connect();
92  
93    }
94  
95  
96  }