Zookeeper | API

作者: icebreakeros | 来源:发表于2019-07-04 07:59 被阅读0次

zookeeper api

maven打包

不包含依赖

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <encoding>UTF-8</encoding>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

mvn clean package

依赖库打到包外

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <encoding>UTF-8</encoding>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>com.icebreakerzr.boot.Application</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>
                            ${project.build.directory}/lib
                        </outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

mvn clean package

关于execution标签

  • phase:绑定目标的构建生命周期阶段执行
  • goals/goal*:执行给定的配置

依赖库打到包内

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <encoding>UTF-8</encoding>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
            <archive>
                <manifest>
                    <mainClass>com.icebreakerzr.boot.Application</mainClass>
                </manifest>
            </archive>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
        </configuration>
        <executions>
            <execution>
                <id>make-assembly</id>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
</plugins>

mvn clean package

zookeeper程序

# pom.xml
<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.4.9</version>
</dependency>

# DataWatcher.java
import org.apache.zookeeper.*;

import java.util.Optional;

public class DataWatcher implements Watcher, Runnable {

    private static String connectString = "";
    private static String path = "/icebreaker";
    private static int sessionTimeout = 2000;
    private ZooKeeper zooKeeper;

    public DataWatcher() {
        try {
            zooKeeper = new ZooKeeper(connectString, sessionTimeout, this);
            if (Optional.ofNullable(zooKeeper).isPresent()) {
                if (!Optional.ofNullable(zooKeeper.exists(path, this))
.isPresent()) {
                    zooKeeper.create(path, 
                      "".getBytes(), 
                      ZooDefs.Ids.OPEN_ACL_UNSAFE, 
                      CreateMode.PERSISTENT);
                }
                printData();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void process(WatchedEvent watchedEvent) {
        System.out.printf("Event Received: %s", watchedEvent.toString());
        if (watchedEvent.getType() == Event.EventType.NodeDataChanged) {
            printData();
        }
    }

    @Override
    public void run() {
        try {
            synchronized (this) {
                while (true) {
                    wait();
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            Thread.currentThread().interrupt();
        }
    }

    private void printData() {
        byte[] data = new byte[0];
        try {
            data = zooKeeper.getData(path, this, null);
            System.out.printf("[CUSTOM] Path: %s, Data: %s", path, 
              new String(data));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

相关文章

网友评论

    本文标题:Zookeeper | API

    本文链接:https://www.haomeiwen.com/subject/kfbkhctx.html