package com.gochinatv.cdn.api.kafka;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;

public class MockTestData {
   
	public static  AtomicInteger global_id = new AtomicInteger(0);
	
	/**
	 * 构建数据格式{id:1,ts:2017-03-24 12:32:34,count:10,value:200,agreeid:190}
	 * topic:[app,server,sys,network]
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {

		Properties props = new Properties();
		props.put("bootstrap.servers", "192.168.2.150:9092");
		props.put("acks", "all");
		props.put("retries", 3);
		props.put("batch.size", 16384);
		props.put("max.block.ms", 1000); //配置超时时间
		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> producer = new KafkaProducer<>(props);
		
		int i=1;
		while(true){
		  Thread.sleep(200L);
		  MockTestData mtd = new MockTestData();
		  String data = "{\"id\":\""+global_id.getAndIncrement()+"\",\"ts\":\""+mtd.getTimeNow()+"\",\"count\":\""+getCount()+"\",\"value\":\""+getValue()+"\",\"agreeId\":\""+getAgreeId()+"\"}";
		  System.out.println(data);
		  producer.send(new ProducerRecord<String, String>("streaming-click", "key"+i, data));
		  i++;
		  if(i==11)break;
		}
		
		producer.flush();
		producer.close();
	}
	
	public static int getAgreeId(){
		int random=(int) (Math.random()*1+1);
		return random;
	}
	
	public static int getCount(){
		int random=(int) (Math.random()*5+1);
		return random;
	}
	
	public static int getValue(){
		int random=(int) (Math.random()*10+1);
		return random;
	}
	
	public String getTimeNow(){
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(date);
	}
	
}

@@ -169,24 +169,78 @@
 micInteger;%0D
+%0A%0D%0Aimport org.apache.kafka.clients.producer.Callback;%0D
 %0Aimport org.
@@ -394,16 +394,74 @@
 Record;%0D
+%0Aimport org.apache.kafka.clients.producer.RecordMetadata;%0D
 %0A%0D%0Apubli
@@ -554,16 +554,47 @@
 er(0);%0D%0A
+%09public static int ERROR = 0;%0D%0A
 %09%0D%0A%09/**%0D
@@ -889,21 +889,17 @@
 %22, %22
-192.168.2.150
+localhost
 :909
@@ -956,17 +956,17 @@
 tries%22, 
-3
+0
 );%0D%0A%09%09pr
@@ -1041,16 +1041,109 @@
 %E9%85%8D%E7%BD%AE%E8%B6%85%E6%97%B6%E6%97%B6%E9%97%B4%0D%0A
+%09%09//props.put(%22request.timeout.ms%22, 1000);%0D%0A%09%09props.put(%22metadata.fetch.timeout.ms%22, 2000);%0D%0A
 %09%09props.
@@ -1472,16 +1472,20 @@
 t i=1;%0D%0A
+%09%09%0D%0A
 %09%09while(
@@ -1513,24 +1513,30 @@
 eep(200L);%0D%0A
+%09%09  %0D%0A
 %09%09  MockTest
@@ -1765,33 +1765,8 @@
 %09%09  
-System.out.println(data);
 %0D%0A%09%09
@@ -1849,16 +1849,426 @@
 i, data)
+,new Callback() %7B%0D%0A%09%09%09@Override%0D%0A%09%09%09public void onCompletion(RecordMetadata metadata, Exception exception) %7B%0D%0A%09%09%09%09if(null == metadata)%7B%0D%0A%09%09%09%09%09try %7B%0D%0A%09%09%09%09%09%09ERROR ++;%0D%0A%09%09%09%09%09%09System.out.println(exception.getClass().toString());%0D%0A%09%09%09%09%09%09return;%0D%0A%09%09%09%09%09%7D catch (Exception e) %7B%0D%0A%09%09%09%09%09%09e.printStackTrace();%0D%0A%09%09%09%09%09%7D%0D%0A%09%09%09%09%7D%0D%0A%09%09%09%09ERROR = 0;%0D%0A%09%09%09%7D%0D%0A%09%09  %7D);%0D%0A%09%09  if(ERROR%3E0)%7B%0D%0A%09%09%09  break;%0D%0A%09%09  %7D%0D%0A%09%09  System.out.println(data
 );%0D%0A%09%09  
@@ -2287,10 +2287,9 @@
 (i==
-11
+3
 )bre
@@ -2420,16 +2420,17 @@
 ndom()*1
+0
 +1);%0D%0A%09%09

add request timeout and throw exception