首先创建maven工程,需要引入的包:

<dependencies>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>0.10.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka_2.10</artifactId>
<version>0.10.2.1</version>
</dependency>
</dependencies>

然后就可以实现生产者与消费者,在创建topic时,如果需要删除已经存在的topic,则需要配置delete.topic.enable=true,否则无法删除对应的topic。

/**
消费者
**/
public class KafkaConsumerDemo
{
private final KafkaConsumer<String, String> consumer; private KafkaConsumerDemo(){
Properties props = new Properties();
props.put("bootstrap.servers", "10.xxx.xxx.149:9092, 10.xxx.xxx.182:9092, 10:xxx.xxx.190:9092");
props.put("group.id", "test");
props.put("enable.auto.commit", "true");
props.put("auto.commit.interval.ms", "");
props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
consumer = new KafkaConsumer<String, String>(props);
} void consume(){
consumer.subscribe(Arrays.asList(KafkaProducerDemo.TOPIC));
while (true) {
ConsumerRecords<String, String> records = consumer.poll();
for (ConsumerRecord<String, String> record : records)
System.out.printf("offset = %d, key = %s, value = %s%n", record.offset(), record.key(), record.value());
}
} public static void main(String[] args){
new KafkaConsumerDemo().consume();
}
}
/**
* 生成者
*
*/
public class KafkaProducerDemo
{
private final Producer<String, String> kafkaProducer; public final static String TOPIC = "JAVA_TOPIC"; private KafkaProducerDemo()
{
kafkaProducer = createKafkaProducer();
} private Producer<String, String> createKafkaProducer()
{
Properties props = new Properties();
props.put("bootstrap.servers", "10.185.156.149:9092, 10.185.156.182:9092, 10:185.156.190:9092");
props.put("acks", "all");
props.put("retries", 0);
props.put("batch.size", 16384);
props.put("linger.ms", 1);
props.put("buffer.memory", 33554432);
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer"); Producer<String, String> kafkaProducer = new KafkaProducer<String, String>(props);
return kafkaProducer;
} void produce()
{
for (int i = 1; i < 1000; i++)
{
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
String key = String.valueOf("key" + i);
String data = "hello kafka message:" + key;
kafkaProducer.send(new ProducerRecord<>(TOPIC, key, data), new Callback()
{
@Override
public void onCompletion(RecordMetadata recordMetadata, Exception e)
{
// do sth
}
});
System.out.println(data);
}
} public static void main(String[] args)
{
KafkaCreateTopic.createTopic("JAVA_TOPIC", 3, 1);
new KafkaProducerDemo().produce();
}
}
/**
创建topic
**/
public class KafkaCreateTopic
{ public static void createTopic(String topic, int partitions, int replicationFactor)
{
ZkUtils zkUtils = ZkUtils.apply("10.xxx.xxx.149:2181", 30000, 30000, JaasUtils.isZkSecurityEnabled());
if (AdminUtils.topicExists(zkUtils, topic))
{
deleteTopic(zkUtils, topic);
}
AdminUtils.createTopic(zkUtils, topic, partitions, replicationFactor, new Properties(),
RackAwareMode.Enforced$.MODULE$);
zkUtils.close();
} public static void deleteTopic(ZkUtils zkUtils, String topic)
{
AdminUtils.deleteTopic(zkUtils, topic);
System.out.println("delete the topic " + topic); }
}

生产者与消费者的Java实现的更多相关文章

  1. 生产者和消费者模式--java设计模式

    生产者和消费者: 就犹如在快餐店点餐一样,有多个打饭的,有不定时的人来买饭,买饭的人从快餐店自动取餐,如果快餐的库存数量达到下限值时,自动启动打饭的,补充盒饭. 通过while循环的方式,传入变量is ...

  2. JAVA并发框架之Semaphore实现生产者与消费者模型

    分类: Java技术      锁和信号量(Semaphore)是实现多线程同步的两种常用的手段.信号量需要初始化一个许可值,许可值可以大于0,也可以小于0,也可以等于0.      如果大于0,表示 ...

  3. Java实现生产者和消费者

    生产者和消费者问题是操作系统的经典问题,在实际工作中也常会用到,主要的难点在于协调生产者和消费者,因为生产者的个数和消费者的个数不确定,而生产者的生成速度与消费者的消费速度也不一样,同时还要实现生产者 ...

  4. Java生产者和消费者问题

    容器类Box.java public class Box { private int num = 0; public void put(){ if(num==10){ try { System.out ...

  5. java多线程中的生产者与消费者之等待唤醒机制@Version1.0

    一.生产者消费者模式的学生类成员变量生产与消费demo,第一版1.等待唤醒:    Object类中提供了三个方法:    wait():等待    notify():唤醒单个线程    notify ...

  6. java之生产者与消费者

    package com.produce; import java.util.LinkedList; import java.util.Queue; /*@author shijin * 生产者与消费者 ...

  7. Android(java)学习笔记71:生产者和消费者之等待唤醒机制

    1. 首先我们根据梳理我们之前Android(java)学习笔记70中关于生产者和消费者程序思路: 2. 下面我们就要重点介绍这个等待唤醒机制: (1)第一步:还是先通过代码体现出等待唤醒机制 pac ...

  8. JAVA并发实现五(生产者和消费者模式wait和notify方式实现)

    package com.subject01; import java.util.PriorityQueue; /** * 通过wait和notify 实现 * 生产者-消费者模型:当队列满时,生产者需 ...

  9. java线 生产者和消费者

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGlhbmdydWkxOTg4/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

随机推荐

  1. Atom中设置你的Snippet,atom技巧(二)

    我们经常需要对二维数组进行迭代,比如这样: for(int x = 0; x < width; x++){ for(int y = 0; y < height; y++){ screen[ ...

  2. ElasticSearch(三) ElasticSearch中文分词插件IK的安装

    正因为Elasticsearch 内置的分词器对中文不友好,会把中文分成单个字来进行全文检索,所以我们需要借助中文分词插件来解决这个问题. 一.安装maven管理工具 Elasticsearch 要使 ...

  3. CF1137F Matches Are Not a Child's Play(LCT思维题)

    题目 CF1137F 很有意思的题目 做法 直接考虑带修改的做法,上一次最大值为u,这次修改v,则最大值为v了 我们发现:\(u-v\)这条链会留到最后,序列里的其他元素相对位置不变,这条链会\(u\ ...

  4. kafka监控搭建

    1. 下载 wget https://github.com/quantifind/KafkaOffsetMonitor/releases/download/v0.2.1/KafkaOffsetMoni ...

  5. Xcode Missing file的解决方案

    因为没在工程里面删除文件,导致Xcode报了一大堆警告,都是Missing file的警告,研究了一下,下面是我的解决方案: Missing file出现的原因 原因就是你在文件里面删除了文件,但是在 ...

  6. RedHat7.4最小化安装yum源不可用问题解决

    本次安装的RedHat7.4是安装在Oracle VM VirtualBox5.2.8虚拟机上面的,本文不对安装虚拟机步骤做详细说明. 工具准备: Oracle VM VirtualBox5.2.8 ...

  7. 正则表达式-RegExp-常用正则表达式

    正则表达式-RegExp-常用正则表达式   作者:nuysoft/JS攻城师/高云 QQ:47214707 EMail:nuysoft@gmail.com 声明:本文为原创文章,如需转载,请注明来源 ...

  8. Linux Shell参数替换

    Linux Shell参数替换 2013-06-03 10:01 by 轩脉刃, 1816 阅读, 0 评论, 收藏, 编辑 Bash中的符号的作用是参数替换,将参数名替换为参数所代表的值.对于 来说 ...

  9. .net 获取浏览器Cookie(包括HttpOnly)

    网上好不容易找到的,分享+收藏 一.接口文件 using System; using System.ComponentModel; using System.Net; using System.Run ...

  10. 用gitolite搭建git server

    在Ubuntu上测试安装一下git server,为后面项目的代码管理做准备.记录流水账如下, 中间关于git 命令的使用说明不做过多解释,需要了解的请google或者直接git help: 我用到了 ...