1.實(shí)現(xiàn)kafka的最最基本的功能

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容