|
| 1 | +package org.apache.zookeeper.server.embedded; |
| 2 | + |
| 3 | +/** |
| 4 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 5 | + * or more contributor license agreements. See the NOTICE file |
| 6 | + * distributed with this work for additional information |
| 7 | + * regarding copyright ownership. The ASF licenses this file |
| 8 | + * to you under the Apache License, Version 2.0 (the |
| 9 | + * "License"); you may not use this file except in compliance |
| 10 | + * with the License. You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, software |
| 15 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * See the License for the specific language governing permissions and |
| 18 | + * limitations under the License. |
| 19 | + */ |
| 20 | + |
| 21 | +import java.nio.file.Path; |
| 22 | +import java.util.Objects; |
| 23 | +import java.util.Properties; |
| 24 | +import org.apache.yetus.audience.InterfaceAudience; |
| 25 | +import org.apache.yetus.audience.InterfaceStability; |
| 26 | + |
| 27 | +/** |
| 28 | + * This API allows you to start a ZooKeeper server node from Java code <p> |
| 29 | + * The server will run inside the same process.<p> |
| 30 | + * Typical usecases are: |
| 31 | + * <ul> |
| 32 | + * <li>Running automated tests</li> |
| 33 | + * <li>Launch ZooKeeper server with a Java based service management system</li> |
| 34 | + * </ul> |
| 35 | + * <p> |
| 36 | + * Please take into consideration that in production usually it is better to not run the client |
| 37 | + * together with the server in order to avoid race conditions, especially around how ephemeral nodes work. |
| 38 | + */ |
| 39 | +@InterfaceAudience.Public |
| 40 | +@InterfaceStability.Evolving |
| 41 | +public interface ZooKeeperServerEmbedded extends AutoCloseable { |
| 42 | + /** |
| 43 | + * Builder for ZooKeeperServerEmbedded. |
| 44 | + */ |
| 45 | + class ZookKeeperServerEmbeddedBuilder { |
| 46 | + |
| 47 | + private Path baseDir; |
| 48 | + private Properties configuration; |
| 49 | + private ExitHandler exitHandler = ExitHandler.EXIT; |
| 50 | + |
| 51 | + /** |
| 52 | + * Base directory of the server. |
| 53 | + * The system will create a temporary configuration file inside this directory. |
| 54 | + * Please remember that dynamic configuration files wil be saved into this directory by default. |
| 55 | + * <p> |
| 56 | + * If you do not set a 'dataDir' configuration entry the system will use a subdirectory of baseDir. |
| 57 | + * @param baseDir |
| 58 | + * @return the builder |
| 59 | + */ |
| 60 | + public ZookKeeperServerEmbeddedBuilder baseDir(Path baseDir) { |
| 61 | + this.baseDir = Objects.requireNonNull(baseDir); |
| 62 | + return this; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Set the contents of the main configuration as it would be in zk_server.conf file. |
| 67 | + * @param configuration the configuration |
| 68 | + * @return the builder |
| 69 | + */ |
| 70 | + public ZookKeeperServerEmbeddedBuilder configuration(Properties configuration) { |
| 71 | + this.configuration = Objects.requireNonNull(configuration); |
| 72 | + return this; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Set the behaviour in case of hard system errors, see {@link ExitHandler}. |
| 77 | + * @param exitHandler the handler |
| 78 | + * @return the builder |
| 79 | + */ |
| 80 | + public ZookKeeperServerEmbeddedBuilder exitHandler(ExitHandler exitHandler) { |
| 81 | + this.exitHandler = Objects.requireNonNull(exitHandler); |
| 82 | + return this; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Validate the configuration and create the server, without starting it. |
| 87 | + * @return the new server |
| 88 | + * @throws Exception |
| 89 | + * @see #start() |
| 90 | + */ |
| 91 | + public ZooKeeperServerEmbedded build() throws Exception { |
| 92 | + if (baseDir == null) { |
| 93 | + throw new IllegalStateException("baseDir is null"); |
| 94 | + } |
| 95 | + if (configuration == null) { |
| 96 | + throw new IllegalStateException("configuration is null"); |
| 97 | + } |
| 98 | + return new ZooKeeperServerEmbeddedImpl(configuration, baseDir, exitHandler); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + static ZookKeeperServerEmbeddedBuilder builder() { |
| 103 | + return new ZookKeeperServerEmbeddedBuilder(); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Start the server. |
| 108 | + * @throws Exception |
| 109 | + */ |
| 110 | + void start() throws Exception; |
| 111 | + |
| 112 | + /** |
| 113 | + * Shutdown gracefully the server and wait for resources to be released. |
| 114 | + */ |
| 115 | + @Override |
| 116 | + void close(); |
| 117 | + |
| 118 | +} |
0 commit comments