001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase;
019
020import static org.junit.Assert.fail;
021
022import java.io.IOException;
023import javax.management.MBeanServerConnection;
024import javax.management.remote.JMXConnector;
025import javax.management.remote.JMXConnectorFactory;
026import org.apache.hadoop.conf.Configuration;
027import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
028import org.apache.hadoop.hbase.testclassification.MediumTests;
029import org.apache.hadoop.hbase.testclassification.MiscTests;
030import org.apache.hadoop.hbase.util.JVMClusterUtil;
031import org.junit.AfterClass;
032import org.junit.Assert;
033import org.junit.BeforeClass;
034import org.junit.ClassRule;
035import org.junit.Rule;
036import org.junit.Test;
037import org.junit.experimental.categories.Category;
038import org.junit.rules.ExpectedException;
039import org.slf4j.Logger;
040import org.slf4j.LoggerFactory;
041
042@Category({MiscTests.class, MediumTests.class})
043public class TestJMXListener {
044
045  @ClassRule
046  public static final HBaseClassTestRule CLASS_RULE =
047      HBaseClassTestRule.forClass(TestJMXListener.class);
048
049  private static final Logger LOG = LoggerFactory.getLogger(TestJMXListener.class);
050  private static HBaseTestingUtility UTIL = new HBaseTestingUtility();
051  private static int connectorPort = UTIL.randomFreePort();
052
053  @BeforeClass
054  public static void setupBeforeClass() throws Exception {
055    Configuration conf = UTIL.getConfiguration();
056
057    conf.set(CoprocessorHost.REGIONSERVER_COPROCESSOR_CONF_KEY,
058      JMXListener.class.getName());
059    conf.setInt("regionserver.rmi.registry.port", connectorPort);
060
061    UTIL.startMiniCluster();
062  }
063
064  @AfterClass
065  public static void tearDownAfterClass() throws Exception {
066    UTIL.shutdownMiniCluster();
067  }
068
069  @Test
070  public void testStart() throws Exception {
071    JMXConnector connector = JMXConnectorFactory.connect(
072      JMXListener.buildJMXServiceURL(connectorPort,connectorPort));
073
074    MBeanServerConnection mb = connector.getMBeanServerConnection();
075    String domain = mb.getDefaultDomain();
076    Assert.assertTrue("default domain is not correct",
077      !domain.isEmpty());
078    connector.close();
079
080  }
081
082  //shutdown hbase only. then try connect, IOException expected
083  @Rule
084  public ExpectedException expectedEx = ExpectedException.none();
085  @Test
086  public void testStop() throws Exception {
087    MiniHBaseCluster cluster = UTIL.getHBaseCluster();
088    LOG.info("shutdown hbase cluster...");
089    cluster.shutdown();
090    LOG.info("wait for the hbase cluster shutdown...");
091    cluster.waitUntilShutDown();
092
093    JMXConnector connector = JMXConnectorFactory.newJMXConnector(
094      JMXListener.buildJMXServiceURL(connectorPort,connectorPort), null);
095    expectedEx.expect(IOException.class);
096    connector.connect();
097
098  }
099
100  @Test
101  public void testGetRegionServerCoprocessors() throws Exception {
102    for (JVMClusterUtil.RegionServerThread rs : UTIL.getHBaseCluster().getRegionServerThreads()) {
103      boolean find = false;
104      for (String s : rs.getRegionServer().getRegionServerCoprocessors()) {
105        if (s.equals(JMXListener.class.getSimpleName())) {
106          find = true;
107          break;
108        }
109      }
110      if (!find) {
111        fail("where is the JMXListener?");
112      }
113    }
114  }
115}