ManagedZooKeeperServerMain.java 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package org.apache.zookeeper.server;
  19. import static org.apache.zookeeper.server.ServerConfig.getClientPort;
  20. import java.io.File;
  21. import java.io.IOException;
  22. import org.apache.log4j.Logger;
  23. import org.apache.zookeeper.jmx.server.ConnectionMXBean;
  24. import org.apache.zookeeper.jmx.server.DataTreeMXBean;
  25. import org.apache.zookeeper.jmx.server.ZooKeeperServerMXBean;
  26. import org.apache.zookeeper.server.persistence.FileTxnLog;
  27. import org.apache.zookeeper.server.persistence.FileTxnSnapLog;
  28. import org.apache.zookeeper.server.util.ZooKeeperObserverManager;
  29. /**
  30. * This class launches a standalone zookeeper server with JMX support
  31. * enabled. The users can connect to the server JVM and manage the server state
  32. * (such as currently open client connections) and view runtime statistics using
  33. * one of existing GUI JMX consoles (jconsole, for example). Please refer to
  34. * the JDK vendor documentation for further information on how to enable JMX
  35. * support in the JVM.
  36. * <p>
  37. * The server provides following MBeans:
  38. * <ul>
  39. * <li>Zookeeper server MBean -- provides various configuraton data and runtime
  40. * statistics, see {@link ZooKeeperServerMXBean}
  41. * <li>Data tree MBean -- provides runtime data tree statistics, see
  42. * {@link DataTreeMXBean}
  43. * <li>Client connection MBean -- provides runtime statistics as well as
  44. * connection management operations, see {@link ConnectionMXBean}
  45. * </ul>
  46. * The client connection is a dynamic resource and therefore the connection
  47. * MBeans are dynamically created and destroyed as the clients connect to and
  48. * disconnect from the server.
  49. */
  50. public class ManagedZooKeeperServerMain extends ZooKeeperServerMain {
  51. private static final Logger LOG = Logger.getLogger(ManagedZooKeeperServerMain.class);
  52. /**
  53. * To start the server specify the client port number and the data directory
  54. * on the command line.
  55. * @see ServerConfig#parse(String[])
  56. * @param args command line parameters.
  57. */
  58. public static void main(String[] args) {
  59. try {
  60. ServerConfig.parse(args);
  61. } catch(Exception e) {
  62. LOG.fatal("Error in config", e);
  63. System.exit(2);
  64. }
  65. ZooKeeperObserverManager.setAsConcrete();
  66. runStandalone(new ZooKeeperServer.Factory() {
  67. public NIOServerCnxn.Factory createConnectionFactory()throws IOException {
  68. return new ObservableNIOServerCnxn.Factory(getClientPort());
  69. }
  70. public ZooKeeperServer createServer() throws IOException {
  71. ManagedZooKeeperServer zks = new ManagedZooKeeperServer(
  72. new FileTxnSnapLog(new File(ServerConfig.getDataDir()),
  73. new File(ServerConfig.getDataLogDir())),
  74. new ZooKeeperServer.BasicDataTreeBuilder());
  75. return zks;
  76. }
  77. });
  78. }
  79. }