List of usage examples for org.apache.zookeeper.server ServerCnxnFactory createFactory
public static ServerCnxnFactory createFactory() throws IOException
From source file:beans.SystemStartable.java
License:Apache License
@Override public void onStart() throws Throwable { // Initialize ZooKeeper. Properties properties = new Properties(); InputStream propertiesStream = getClass().getClassLoader().getResourceAsStream("zoo.cfg"); properties.load(propertiesStream);/*from w ww . ja va 2 s. co m*/ QuorumPeerConfig quorumConfig = new QuorumPeerConfig(); quorumConfig.parseProperties(properties); zkServer = new ZooKeeperServer(); zkServer.setTickTime(quorumConfig.getTickTime()); zkServer.setMinSessionTimeout(quorumConfig.getMinSessionTimeout()); zkServer.setMaxSessionTimeout(quorumConfig.getMaxSessionTimeout()); zkDirectory = Files.createTempDirectory("zookeeper").toFile(); LOG.info("Using ZooKeeper directory: " + zkDirectory); FileTxnSnapLog txnLog = new FileTxnSnapLog(zkDirectory, zkDirectory); zkServer.setTxnLogFactory(txnLog); cnxnFactory = ServerCnxnFactory.createFactory(); cnxnFactory.configure(quorumConfig.getClientPortAddress(), quorumConfig.getMaxClientCnxns()); cnxnFactory.startup(zkServer); }
From source file:co.paralleluniverse.galaxy.test.GalaxyTestingUtils.java
License:Open Source License
public static ServerCnxnFactory startZookeeper(final String configResource, final String dataDirName) throws IOException, QuorumPeerConfig.ConfigException { ServerConfig sc = new ServerConfig(); sc.parse(pathToResource(configResource)); deleteDir(dataDirName);/*from w ww .j av a 2 s. c o m*/ new File(dataDirName).mkdirs(); FileTxnSnapLog txnLog = null; try { ZooKeeperServer zkServer = new ZooKeeperServer(); txnLog = new FileTxnSnapLog(new File(sc.getDataDir()), new File(sc.getDataDir())); zkServer.setTxnLogFactory(txnLog); zkServer.setTickTime(sc.getTickTime()); zkServer.setMinSessionTimeout(sc.getMinSessionTimeout()); zkServer.setMaxSessionTimeout(sc.getMaxSessionTimeout()); ServerCnxnFactory cnxnFactory = ServerCnxnFactory.createFactory(); cnxnFactory.configure(sc.getClientPortAddress(), sc.getMaxClientCnxns()); cnxnFactory.startup(zkServer); return cnxnFactory; } catch (InterruptedException e) { throw new RuntimeException(e); } finally { if (txnLog != null) { txnLog.close(); } } }
From source file:com.anhth12.zk.util.LocalZKServer.java
/** * Starts Zookeeper./*from ww w . j a va 2 s . c o m*/ * * @throws IOException if an error occurs during initialization * @throws InterruptedException if an error occurs during initialization */ public synchronized void start() throws IOException, InterruptedException { log.info("Starting Zookeeper on port {}", port); dataDir = Files.createTempDirectory(LocalZKServer.class.getSimpleName()); dataDir.toFile().deleteOnExit(); Properties properties = new Properties(); properties.setProperty("dataDir", dataDir.toAbsolutePath().toString()); properties.setProperty("clientPort", Integer.toString(port)); log.info("ZK config: {}", properties); QuorumPeerConfig quorumConfig = new QuorumPeerConfig(); try { quorumConfig.parseProperties(properties); } catch (QuorumPeerConfig.ConfigException e) { throw new IllegalArgumentException(e); } purgeManager = new DatadirCleanupManager(quorumConfig.getDataDir(), quorumConfig.getDataLogDir(), quorumConfig.getSnapRetainCount(), quorumConfig.getPurgeInterval()); purgeManager.start(); ServerConfig serverConfig = new ServerConfig(); serverConfig.readFrom(quorumConfig); zkServer = new ZooKeeperServer(); zkServer.setTickTime(serverConfig.getTickTime()); zkServer.setMinSessionTimeout(serverConfig.getMinSessionTimeout()); zkServer.setMaxSessionTimeout(serverConfig.getMaxSessionTimeout()); // These two ServerConfig methods returned String in 3.4.x and File in 3.5.x transactionLog = new FileTxnSnapLog(new File(serverConfig.getDataLogDir().toString()), new File(serverConfig.getDataDir().toString())); zkServer.setTxnLogFactory(transactionLog); connectionFactory = ServerCnxnFactory.createFactory(); connectionFactory.configure(serverConfig.getClientPortAddress(), serverConfig.getMaxClientCnxns()); connectionFactory.startup(zkServer); }
From source file:com.chiralBehaviors.autoconfigure.ZookeeperLauncher.java
License:Open Source License
/** * Copied from QuorumPeerMain/*from ww w .j a v a2 s .co m*/ */ protected void runFromConfig(QuorumPeerConfig config) throws IOException { LOG.info("Starting quorum peer"); ServerCnxnFactory cnxnFactory = ServerCnxnFactory.createFactory(); cnxnFactory.configure(config.getClientPortAddress(), config.getMaxClientCnxns()); quorumPeer = new QuorumPeer(); quorumPeer.setClientPortAddress(config.getClientPortAddress()); quorumPeer .setTxnFactory(new FileTxnSnapLog(new File(config.getDataLogDir()), new File(config.getDataDir()))); quorumPeer.setQuorumPeers(config.getServers()); quorumPeer.setElectionType(config.getElectionAlg()); quorumPeer.setMyid(config.getServerId()); quorumPeer.setTickTime(config.getTickTime()); quorumPeer.setMinSessionTimeout(config.getMinSessionTimeout()); quorumPeer.setMaxSessionTimeout(config.getMaxSessionTimeout()); quorumPeer.setInitLimit(config.getInitLimit()); quorumPeer.setSyncLimit(config.getSyncLimit()); quorumPeer.setQuorumVerifier(config.getQuorumVerifier()); quorumPeer.setCnxnFactory(cnxnFactory); quorumPeer.setZKDatabase(new ZKDatabase(quorumPeer.getTxnFactory())); quorumPeer.setLearnerType(config.getPeerType()); quorumPeer.start(); }
From source file:com.cloudera.oryx.kafka.util.LocalZKServer.java
License:Open Source License
/** * Starts Zookeeper./* ww w . j a v a2 s . co m*/ * * @throws IOException if an error occurs during initialization * @throws InterruptedException if an error occurs during initialization */ public synchronized void start() throws IOException, InterruptedException { log.info("Starting Zookeeper on port {}", port); dataDir = Files.createTempDirectory(LocalZKServer.class.getSimpleName()); dataDir.toFile().deleteOnExit(); QuorumPeerConfig quorumConfig = new QuorumPeerConfig(); try { quorumConfig.parseProperties( ConfigUtils.keyValueToProperties("dataDir", dataDir.toAbsolutePath(), "clientPort", port)); } catch (QuorumPeerConfig.ConfigException e) { throw new IllegalArgumentException(e); } purgeManager = new DatadirCleanupManager(quorumConfig.getDataDir(), quorumConfig.getDataLogDir(), quorumConfig.getSnapRetainCount(), quorumConfig.getPurgeInterval()); purgeManager.start(); ServerConfig serverConfig = new ServerConfig(); serverConfig.readFrom(quorumConfig); zkServer = new ZooKeeperServer(); zkServer.setTickTime(serverConfig.getTickTime()); zkServer.setMinSessionTimeout(serverConfig.getMinSessionTimeout()); zkServer.setMaxSessionTimeout(serverConfig.getMaxSessionTimeout()); // These two ServerConfig methods returned String in 3.4.x and File in 3.5.x transactionLog = new FileTxnSnapLog(new File(serverConfig.getDataLogDir().toString()), new File(serverConfig.getDataDir().toString())); zkServer.setTxnLogFactory(transactionLog); connectionFactory = ServerCnxnFactory.createFactory(); connectionFactory.configure(serverConfig.getClientPortAddress(), serverConfig.getMaxClientCnxns()); connectionFactory.startup(zkServer); }
From source file:com.comcast.viper.flume2storm.zookeeper.ZkTestServer.java
License:Apache License
/** * Starts the test ZooKeeper server/*w w w .jav a 2 s.c o m*/ */ public void start() { if (started) { LOG.debug("Already started"); return; } try { LOG.debug("Starting..."); ServerConfig config = new ServerConfig(); config.parse(new String[] { port.toString(), ZK_DIR.getCanonicalPath(), ticktime.toString() }); zkServer = new ZooKeeperServer(); FileTxnSnapLog ftxn = new FileTxnSnapLog(new File(config.getDataLogDir()), new File(config.getDataDir())); zkServer.setTxnLogFactory(ftxn); zkServer.setTickTime(config.getTickTime()); zkServer.setMinSessionTimeout(config.getMinSessionTimeout()); zkServer.setMaxSessionTimeout(config.getMaxSessionTimeout()); cnxnFactory = ServerCnxnFactory.createFactory(); cnxnFactory.configure(config.getClientPortAddress(), config.getMaxClientCnxns()); cnxnFactory.startup(zkServer); started = true; LOG.info("Started, {}", getConnectString()); } catch (Exception e) { LOG.error("Failed to start: " + e.getMessage(), e); } }
From source file:com.glaf.cluster.catalina.session.QuorumPeerMain.java
License:Apache License
public void runFromConfig(QuorumPeerConfig config) throws IOException { try {/* w ww . j av a2s . c o m*/ ManagedUtil.registerLog4jMBeans(); } catch (JMException e) { LOG.warn("Unable to register log4j JMX control", e); } LOG.info("Starting quorum peer"); try { ServerCnxnFactory cnxnFactory = ServerCnxnFactory.createFactory(); cnxnFactory.configure(config.getClientPortAddress(), config.getMaxClientCnxns()); quorumPeer = new QuorumPeer(); quorumPeer.setClientPortAddress(config.getClientPortAddress()); quorumPeer.setTxnFactory( new FileTxnSnapLog(new File(config.getDataLogDir()), new File(config.getDataDir()))); quorumPeer.setQuorumPeers(config.getServers()); quorumPeer.setElectionType(config.getElectionAlg()); quorumPeer.setMyid(config.getServerId()); quorumPeer.setTickTime(config.getTickTime()); quorumPeer.setMinSessionTimeout(config.getMinSessionTimeout()); quorumPeer.setMaxSessionTimeout(config.getMaxSessionTimeout()); quorumPeer.setInitLimit(config.getInitLimit()); quorumPeer.setSyncLimit(config.getSyncLimit()); quorumPeer.setQuorumVerifier(config.getQuorumVerifier()); quorumPeer.setCnxnFactory(cnxnFactory); quorumPeer.setZKDatabase(new ZKDatabase(quorumPeer.getTxnFactory())); quorumPeer.setLearnerType(config.getPeerType()); quorumPeer.start(); quorumPeer.join(); } catch (InterruptedException e) { // warn, but generally this is ok LOG.warn("Quorum Peer interrupted", e); } }
From source file:com.heliosapm.streams.TestZooKeeperServer.java
License:Apache License
/** * Run from a ServerConfig./*from ww w . j a v a 2 s . co m*/ * @param config ServerConfig to use. * @throws IOException on any error */ public void runFromConfig(ServerConfig config) throws IOException { LOG.info(">>>>> Starting Test ZooKeep Server..."); FileTxnSnapLog txnLog = null; 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(); txnLog = new FileTxnSnapLog(new File(config.getDataLogDir()), new File(config.getDataDir())); zkServer.setTxnLogFactory(txnLog); zkServer.setTickTime(config.getTickTime()); zkServer.setMinSessionTimeout(config.getMinSessionTimeout()); zkServer.setMaxSessionTimeout(config.getMaxSessionTimeout()); cnxnFactory = ServerCnxnFactory.createFactory(); cnxnFactory.configure(config.getClientPortAddress(), config.getMaxClientCnxns()); cnxnFactory.startup(zkServer); LOG.info("<<<<< Test ZooKeep Server Started."); // cnxnFactory.join(); // if (zkServer.isRunning()) { // zkServer.shutdown(); // } } catch (InterruptedException e) { // warn, but generally this is ok LOG.warn("Server interrupted", e); } finally { // if (txnLog != null) { // txnLog.close(); // } } }
From source file:com.huawei.streaming.cql.LocalTaskCommons.java
License:Apache License
/** * Run from a ServerConfig./* w ww .j a va 2 s. co m*/ * */ private static void runFromConfig(ServerConfig config) throws IOException { LOG.info("Starting server"); String newDataDir = LocalTaskCommons.setDir(ZKDATA); String newLogDir = LocalTaskCommons.setDir(ZKLOG); try { removeZKTmpDir(newDataDir, newLogDir); ZooKeeperServer zkServer = new ZooKeeperServer(); FileTxnSnapLog ftxn = new FileTxnSnapLog(new File(newLogDir), new File(newDataDir)); zkServer.setTxnLogFactory(ftxn); zkServer.setTickTime(config.getTickTime()); zkServer.setMinSessionTimeout(config.getMinSessionTimeout()); zkServer.setMaxSessionTimeout(config.getMaxSessionTimeout()); cnxnFactory = ServerCnxnFactory.createFactory(); cnxnFactory.configure(config.getClientPortAddress(), config.getMaxClientCnxns()); cnxnFactory.startup(zkServer); } catch (InterruptedException e) { // warn, but generally this is ok LOG.warn("Server interrupted", e); } }
From source file:com.intropro.prairie.unit.zookeeper.ZookeeperUnit.java
License:Apache License
private void runFromConfig(ServerConfig config) throws IOException { zkServer = new ZooKeeperServer(); try {//from www . j a v a2s .c o m txnLog = new FileTxnSnapLog(new File(config.getDataLogDir()), new File(config.getDataDir())); zkServer.setTxnLogFactory(txnLog); zkServer.setTickTime(config.getTickTime()); zkServer.setMinSessionTimeout(config.getMinSessionTimeout()); zkServer.setMaxSessionTimeout(config.getMaxSessionTimeout()); cnxnFactory = ServerCnxnFactory.createFactory(); cnxnFactory.configure(config.getClientPortAddress(), config.getMaxClientCnxns()); cnxnFactory.startup(zkServer); } catch (InterruptedException e) { if (zkServer.isRunning()) { zkServer.shutdown(); } } }