1.使用 maven 生成項(xiàng)目
mvn archetype:generate -DgroupId=com.packt.samples -DartifactId=kafka_server -Dversion=1.0.0 -DinteractiveMode=false -DarchetypeCatalog=internal
2.編輯pom 文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
? xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
? <modelVersion>4.0.0</modelVersion>
? <groupId>com.packt.samples</groupId>
? <artifactId>kafka_server</artifactId>
? <packaging>jar</packaging>
? <version>1.0.0</version>
? <name>kafka_server</name>
? <url>http://maven.apache.org</url>
? <dependencies>
? ? <dependency>
? ? ? <groupId>junit</groupId>
? ? ? <artifactId>junit</artifactId>
? ? ? <version>3.8.1</version>
? ? ? <scope>test</scope>
? ? </dependency>
? </dependencies>
<build>
? <plugins>
? ? <plugin>
? ? ? <groupId>org.apache.maven.plugins</groupId>
? ? ? <artifactId>maven-assembly-plugin</artifactId>
? ? ? <version>2.4</version>
? ? ? <configuration>
? ? ? ? <descriptorRefs>
? ? ? ? ? <descriptorRef>jar-with-dependencies</descriptorRef>
? ? ? ? </descriptorRefs>
? ? ? ? <archive>
? ? ? ? ? <manifest>
? ? ? ? ? ? <addClasspath>true</addClasspath>
? ? ? ? ? ? <mainClass>com.packt.samples.App</mainClass>
? ? ? ? ? </manifest>
? ? ? ? </archive>
? ? ? </configuration>
? ? ? <executions>
? ? ? ? <execution>
? ? ? ? ? <id>assemble-all</id>
? ? ? ? ? <phase>package</phase>
? ? ? ? ? <goals>
? ? ? ? ? ? <goal>single</goal>
? ? ? ? ? </goals>
? ? ? ? </execution>
? ? ? </executions>
? ? </plugin>
? </plugins>
</build>
</project>
3.編輯代碼
[root@localhost kafka_server]# cat src/main/java/com/packt/samples/App.java
package com.packt.samples;
public class App
{
? ? public static void main( String[] args )
? ? {
? ? ? ? final KafkaServerStartable kafka_server = new KafkaServerStartable();
? ? ? ? Runtime.getRuntime().addShutdownHook(new Thread("kafka-shutdown-hook") {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void run() {
? ? ? ? ? ? ? ? kafka_server.shutdown();
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? kafka_server.startup();
? ? ? ? kafka_server.awaitShutdown();
? ? }
}
[root@localhost kafka_server]# cat src/main/java/com/packt/samples/KafkaServerStartable.java
package com.packt.samples;
public class KafkaServerStartable {
? ? private KafkaServer server;
? ? public KafkaServerStartable() {
? ? ? ? server = new KafkaServer();
? ? }
? ? public void startup() {
? ? ? ? server.startup();
? ? }
? ? public void awaitShutdown() {
? ? ? ? server.awaitShutdown();
? ? }
? ? public void shutdown() {
? ? ? ? try {
? ? ? ? ? ? server.shutdown();
? ? ? ? } catch(Exception e) {
? ? ? ? ? ? System.out.println(e);
? ? ? ? ? ? System.exit(1);
? ? ? ? }
? ? }
}
[root@localhost kafka_server]# cat src/main/java/com/packt/samples/KafkaServer.java
package com.packt.samples;
import java.util.concurrent.CountDownLatch;
public class KafkaServer {
? ? public CountDownLatch shutdownLatch;
? ? public KafkaServer() {
? ? }
? ? public void startup() {
? ? ? ? shutdownLatch = new CountDownLatch(1);
? ? }
? ? public void awaitShutdown() {
? ? ? ? try {
? ? ? ? ? ? shutdownLatch.await();
? ? ? ? } catch(InterruptedException e) {
? ? ? ? ? ? System.err.println(e);
? ? ? ? }
? ? }
? ? public void shutdown() {
? ? ? ? shutdownLatch.countDown();
? ? }
}
4.編譯
mvn clean package -DskipTests
5.測(cè)試
java -jar target/kafka_server-1.0.0-jar-with-dependencies.jar