打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
kafka java 实现消息队列demo
  kafka是吞吐量巨大的一个消息系统,它是用scala写的,和普通的消息的生产消费还有所不同,写了个demo程序供大家参考。kafka的安装请参考官方文档。

首先我们需要新建一个maven项目,然后在pom中引用kafka jar包,引用依赖如下:

  1. <dependency>  
  2.          <groupId>org.apache.kafka</groupId>  
  3.          <artifactId>kafka_2.10</artifactId>  
  4.          <version>0.8.0</version>  
  5.      </dependency>  

  1. package kafka;  
  2.   
  3. import kafka.consumer.ConsumerConfig;  
  4. import kafka.consumer.ConsumerIterator;  
  5. import kafka.consumer.KafkaStream;  
  6. import kafka.javaapi.consumer.ConsumerConnector;  
  7. import kafka.serializer.StringDecoder;  
  8. import kafka.utils.VerifiableProperties;  
  9.   
  10. import java.util.HashMap;  
  11. import java.util.List;  
  12. import java.util.Map;  
  13. import java.util.Properties;  
  14.   
  15. /** 
  16.  *   kafka下载和安装  http://mirrors.hust.edu.cn/apache/kafka/0.9.0.0/ 
  17.  *   http://czj4451.iteye.com/blog/2041096 
  18.  *   window 启动: 
  19.  bin\windows\zookeeper-server-start.bat config\zookeeper.properties 
  20.  bin\windows\kafka-server-start.bat config\server.properties 
  21.  */  
  22. public class KafkaConsumer {  
  23.   
  24.     private final ConsumerConnector consumer;  
  25.   
  26.     private KafkaConsumer() {  
  27.         Properties props = new Properties();  
  28.         //zookeeper 配置  
  29.         props.put("zookeeper.connect", "127.0.0.1:2181");  
  30.   
  31.         //group 代表一个消费组  
  32.         props.put("group.id", "jd-group");  
  33.   
  34.         //zk连接超时  
  35.         props.put("zookeeper.session.timeout.ms", "4000");  
  36.         props.put("zookeeper.sync.time.ms", "200");  
  37.         props.put("auto.commit.interval.ms", "1000");  
  38.         props.put("auto.offset.reset", "smallest");  
  39.         //序列化类  
  40.         props.put("serializer.class", "kafka.serializer.StringEncoder");  
  41.   
  42.         ConsumerConfig config = new ConsumerConfig(props);  
  43.   
  44.         consumer = kafka.consumer.Consumer.createJavaConsumerConnector(config);  
  45.     }  
  46.   
  47.     void consume() {  
  48.         Map<String, Integer> topicCountMap = new HashMap<String, Integer>();  
  49.         topicCountMap.put(KafkaProducer.TOPIC, new Integer(1));  
  50.   
  51.         StringDecoder keyDecoder = new StringDecoder(new VerifiableProperties());  
  52.         StringDecoder valueDecoder = new StringDecoder(new VerifiableProperties());  
  53.   
  54.         Map<String, List<KafkaStream<String, String>>> consumerMap =  
  55.                 consumer.createMessageStreams(topicCountMap,keyDecoder,valueDecoder);  
  56.         KafkaStream<String, String> stream = consumerMap.get(KafkaProducer.TOPIC).get(0);  
  57.         ConsumerIterator<String, String> it = stream.iterator();  
  58.         while (it.hasNext())  
  59.         {  
  60.             System.out.println(it.next().message());  
  61.         }  
  62.         System.out.println("finish");  
  63.   
  64.   
  65.     }  
  66.   
  67.     //http://www.open-open.com/lib/view/open1412991579999.html  
  68.     public static void main(String[] args) {  
  69.         new KafkaConsumer().consume();  
  70.     }  
  71. }  
  1. package kafka;  
  2. import kafka.javaapi.producer.Producer;  
  3. import kafka.producer.KeyedMessage;  
  4. import kafka.producer.ProducerConfig;  
  5.   
  6. import java.util.Properties;  
  7.   
  8.   
  9. /** 
  10.  *   kafka下载和安装  http://mirrors.hust.edu.cn/apache/kafka/0.9.0.0/ 
  11.  *   http://czj4451.iteye.com/blog/2041096 
  12.  *   window 启动: 
  13.  bin\windows\zookeeper-server-start.bat config\zookeeper.properties 
  14.  bin\windows\kafka-server-start.bat config\server.properties 
  15.  */  
  16. public class KafkaProducer  
  17. {  
  18.     private final Producer<String, String> producer;  
  19.     public final static String TOPIC = "TEST-TOPIC";  
  20.   
  21.     private KafkaProducer(){  
  22.         Properties props = new Properties();  
  23.         //此处配置的是kafka的端口  
  24.         props.put("metadata.broker.list", "127.0.0.1:9092");  
  25.   
  26.         //配置value的序列化类  
  27.         props.put("serializer.class", "kafka.serializer.StringEncoder");  
  28.         //配置key的序列化类  
  29.         props.put("key.serializer.class", "kafka.serializer.StringEncoder");  
  30.   
  31.         //request.required.acks  
  32.         //0, which means that the producer never waits for an acknowledgement from the broker (the same behavior as 0.7). This option provides the lowest latency but the weakest durability guarantees (some data will be lost when a server fails).  
  33.         //1, which means that the producer gets an acknowledgement after the leader replica has received the data. This option provides better durability as the client waits until the server acknowledges the request as successful (only messages that were written to the now-dead leader but not yet replicated will be lost).  
  34.         //-1, which means that the producer gets an acknowledgement after all in-sync replicas have received the data. This option provides the best durability, we guarantee that no messages will be lost as long as at least one in sync replica remains.  
  35.         props.put("request.required.acks","-1");  
  36.   
  37.         producer = new Producer<String, String>(new ProducerConfig(props));  
  38.     }  
  39.   
  40.     void produce() {  
  41.         int messageNo = 100;  
  42.         final int COUNT = 1000;  
  43.   
  44.         while (messageNo < COUNT) {  
  45.             String key = String.valueOf(messageNo);  
  46.             String data = "hello kafka message " + key;  
  47.             producer.send(new KeyedMessage<String, String>(TOPIC, key ,data));  
  48.             System.out.println(data);  
  49.             messageNo ++;  
  50.         }  
  51.     }  
  52.     //http://www.open-open.com/lib/view/open1412991579999.html  
  53.     public static void main( String[] args )  
  54.     {  
  55.         new KafkaProducer().produce();  
  56.     }  
  57. }  

转自:http://www.open-open.com/lib/view/open1412991579999.html

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
利用Flink stream从kafka中写数据到mysql
JMeter 扩展插件实现对自定义协议的支持
Java导出Excel完整例子+完整代码,使用easypoi导出Excel+通用工具类
使用commons configuration管理配置文件
MyBatis(三):自定义持久层框架实现
Java 实现音频添加自定义时长静音(附代码) | Java工具类
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服