生产者与消费者的Java实现
首先创建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实现的更多相关文章
- 生产者和消费者模式--java设计模式
生产者和消费者: 就犹如在快餐店点餐一样,有多个打饭的,有不定时的人来买饭,买饭的人从快餐店自动取餐,如果快餐的库存数量达到下限值时,自动启动打饭的,补充盒饭. 通过while循环的方式,传入变量is ...
- JAVA并发框架之Semaphore实现生产者与消费者模型
分类: Java技术 锁和信号量(Semaphore)是实现多线程同步的两种常用的手段.信号量需要初始化一个许可值,许可值可以大于0,也可以小于0,也可以等于0. 如果大于0,表示 ...
- Java实现生产者和消费者
生产者和消费者问题是操作系统的经典问题,在实际工作中也常会用到,主要的难点在于协调生产者和消费者,因为生产者的个数和消费者的个数不确定,而生产者的生成速度与消费者的消费速度也不一样,同时还要实现生产者 ...
- Java生产者和消费者问题
容器类Box.java public class Box { private int num = 0; public void put(){ if(num==10){ try { System.out ...
- java多线程中的生产者与消费者之等待唤醒机制@Version1.0
一.生产者消费者模式的学生类成员变量生产与消费demo,第一版1.等待唤醒: Object类中提供了三个方法: wait():等待 notify():唤醒单个线程 notify ...
- java之生产者与消费者
package com.produce; import java.util.LinkedList; import java.util.Queue; /*@author shijin * 生产者与消费者 ...
- Android(java)学习笔记71:生产者和消费者之等待唤醒机制
1. 首先我们根据梳理我们之前Android(java)学习笔记70中关于生产者和消费者程序思路: 2. 下面我们就要重点介绍这个等待唤醒机制: (1)第一步:还是先通过代码体现出等待唤醒机制 pac ...
- JAVA并发实现五(生产者和消费者模式wait和notify方式实现)
package com.subject01; import java.util.PriorityQueue; /** * 通过wait和notify 实现 * 生产者-消费者模型:当队列满时,生产者需 ...
- java线 生产者和消费者
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGlhbmdydWkxOTg4/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
随机推荐
- Apache 部署HTTPS
Apache 部署HTTPS 系统:Linux Centos 7.4 x64 应用:Apache 2.4.6 需要安装:mod_ssl 注:确认开启 Include conf/extra/httpd- ...
- oracle错误一览表
ORA-00001: 违反唯一约束条件 (.)ORA-00017: 请求会话以设置跟踪事件ORA-00018: 超出最大会话数ORA-00019: 超出最大会话许可数ORA-00020: 超出最大进程 ...
- spring手动配置
本文总结自:https://www.cnblogs.com/V1haoge/p/7183408.html SpringBoot中免除了大部分配置,但是对于一些特定的情况,还是需要我们进行手动配置的. ...
- Jquery14 工具函数
学习要点: 1.字符串操作 2.数组和对象操作 3.测试操作 4.URL 操作 5.浏览器检测 6.其他操作 工具函数是指直接依附于 jQuery 对象,针对 jQuery 对象本身定义的方法,即全局 ...
- linxu 安装rabbitMQ
转载自:http://blog.csdn.net/mooreliu/article/details/44645807 首先使EPEL(http://fedoraproject.org/wiki/EPE ...
- hadoop的安装配置
资源下载路径:https://archive.cloudera.com/cdh5/cdh/5/:https://archive.cloudera.com/cdh5/cdh/5/hadoop-2.6.0 ...
- UVA 12657 Boxes in a Line(双向链表+小技巧)
题意:对于一行按照顺序排列盒子数字与位置都为 1,2,3,4....n 执行四种操作 c = 1 x 放到 y 的左边 c =2 x 放到 y 的右边 c =3 交换 x, y c =4 ...
- 解题报告: hdu 3949 - 线性基
#include <iostream> #include <cstdio> #define LL long long using namespace std; const in ...
- geoserver源码学习与扩展——restAPI访问
产生这篇文章的想法是在前端通过js调用restAPI时,总是不成功,发送ajax请求时还总是出现类似跨域的问题,后来查找才发现,默认情况下restAPI的访问都需要管理员权限,而通过ajax请求传输用 ...
- 模仿某旅行网站 纯css实现背景放大效果
基本功能是鼠标移动到图片上,对应宽度变宽.其中布局和基本样式直接copy官网,功能部分是自己瞎鼓捣实现的. 直接上代码: HTML部分 <div class="fold_wrap&qu ...