diff options
author | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-20 23:53:35 +0000 |
---|---|---|
committer | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-20 23:53:35 +0000 |
commit | a3c48da9bb8971497d414f86e352123d95b9c3da (patch) | |
tree | fdf0f3636b65946c061c8b2e89d657b488be274e /sca-java-2.x/trunk/modules/endpoint-zookeeper/src/main/java/org/apache/tuscany/sca/endpoint/zookeeper/LocalZooKeeperServer.java | |
parent | cc7496466097c3cb8e793ebf3e332b025705aaa7 (diff) |
Moving 2.x trunk
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@882795 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-2.x/trunk/modules/endpoint-zookeeper/src/main/java/org/apache/tuscany/sca/endpoint/zookeeper/LocalZooKeeperServer.java')
-rw-r--r-- | sca-java-2.x/trunk/modules/endpoint-zookeeper/src/main/java/org/apache/tuscany/sca/endpoint/zookeeper/LocalZooKeeperServer.java | 148 |
1 files changed, 148 insertions, 0 deletions
diff --git a/sca-java-2.x/trunk/modules/endpoint-zookeeper/src/main/java/org/apache/tuscany/sca/endpoint/zookeeper/LocalZooKeeperServer.java b/sca-java-2.x/trunk/modules/endpoint-zookeeper/src/main/java/org/apache/tuscany/sca/endpoint/zookeeper/LocalZooKeeperServer.java new file mode 100644 index 0000000000..49bb46f983 --- /dev/null +++ b/sca-java-2.x/trunk/modules/endpoint-zookeeper/src/main/java/org/apache/tuscany/sca/endpoint/zookeeper/LocalZooKeeperServer.java @@ -0,0 +1,148 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.endpoint.zookeeper; + +import java.io.File; +import java.io.IOException; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.apache.zookeeper.server.NIOServerCnxn; +import org.apache.zookeeper.server.ServerConfig; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; +import org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException; + +/** + * This class starts and runs a standalone ZooKeeperServer. + */ +public class LocalZooKeeperServer { + private static final Logger logger = Logger.getLogger(LocalZooKeeperServer.class.getName()); + private static final String USAGE = "Usage: LocalZooKeeperServer configfile | port datadir [ticktime] [maxcnxns]"; + private NIOServerCnxn.Factory cnxnFactory; + private ZooKeeperServer zkServer; + + /** + * + */ + public LocalZooKeeperServer() { + super(); + } + + public Thread folk(final ServerConfig config) { + Thread thread = new Thread() { + public void run() { + try { + LocalZooKeeperServer.this.run(config); + } catch (IOException e) { + throw new IllegalStateException(e); + } + } + }; + thread.start(); + return thread; + } + + public Thread folk(final String[] args) { + Thread thread = new Thread() { + public void run() { + try { + LocalZooKeeperServer.this.run(args); + } catch (Exception e) { + throw new IllegalStateException(e); + } + } + }; + thread.start(); + return thread; + } + + /** + * Run from a ServerConfig. + * @param config ServerConfig to use. + * @throws IOException + */ + public void run(ServerConfig config) throws IOException { + logger.info("Starting ZooKeeper server"); + try { + // Note that this thread isn't going to be doing anything else, + // so rather than spawning another thread, we will just call + // run() in this thread. + // create a file logger url from the command line args + zkServer = new ZooKeeperServer(); + + FileTxnSnapLog ftxn = new FileTxnSnapLog(new File(config.getDataLogDir()), new File(config.getDataDir())); + zkServer.setTxnLogFactory(ftxn); + zkServer.setTickTime(config.getTickTime()); + cnxnFactory = new NIOServerCnxn.Factory(config.getClientPort(), config.getMaxClientCnxns()); + cnxnFactory.startup(zkServer); + cnxnFactory.join(); + if (zkServer.isRunning()) { + zkServer.shutdown(); + } + } catch (InterruptedException e) { + // warn, but generally this is ok + logger.log(Level.WARNING, "Server interrupted", e); + } + } + + public void run(String[] args) throws ConfigException, IOException { + ServerConfig config = new ServerConfig(); + if (args.length == 1) { + config.parse(args[0]); + } else { + config.parse(args); + } + run(config); + } + + /** + * Shutdown the serving instance + */ + public void shutdown() { + cnxnFactory.shutdown(); + } + + /* + * Start up the ZooKeeper server. + * + * @param args the configfile or the port datadir [ticktime] + */ + public static void main(String[] args) { + LocalZooKeeperServer main = new LocalZooKeeperServer(); + try { + main.run(args); + } catch (IllegalArgumentException e) { + logger.log(Level.SEVERE, "Invalid arguments, exiting abnormally", e); + logger.info(USAGE); + System.err.println(USAGE); + System.exit(2); + } catch (ConfigException e) { + logger.log(Level.SEVERE, "Invalid config, exiting abnormally", e); + System.err.println("Invalid config, exiting abnormally"); + System.exit(2); + } catch (Exception e) { + logger.log(Level.SEVERE, "Unexpected exception, exiting abnormally", e); + System.exit(1); + } + logger.info("Exiting normally"); + System.exit(0); + } +} |