本文整理匯總了Java中org.apache.kafka.clients.producer.Producer.flush方法的典型用法代碼示例。如果您正苦於以下問題:Java Producer.flush方法的具體用法?Java Producer.flush怎麽用?Java Producer.flush使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.kafka.clients.producer.Producer
的用法示例。
在下文中一共展示了Producer.flush方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: sendStringMessage
import org.apache.kafka.clients.producer.Producer; //導入方法依賴的package包/類
public static void sendStringMessage() throws Exception{
Properties props = new Properties();
props.put("bootstrap.servers", servers);
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> producer = new org.apache.kafka.clients.producer.KafkaProducer<>(props);
//沒有任何分區,默認1個分區,發送消息
int i=0;
while(i<1000){
Thread.sleep(1000L);
String message = "zhangsan"+i;
producer.send(new ProducerRecord<>("NL_U_APP_ALARM_APP_STRING",message));
i++;
producer.flush();
}
producer.close();
}
示例2: sendWrapperMessage
import org.apache.kafka.clients.producer.Producer; //導入方法依賴的package包/類
public static void sendWrapperMessage() throws Exception {
Properties props = new Properties();
props.put("bootstrap.servers", servers);
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", "com.gochinatv.spark.kafka.SerializedMessage");
Producer<String, WrapperAppMessage> producer = new org.apache.kafka.clients.producer.KafkaProducer<>(props);
//case 1:
//沒有任何分區,默認1個分區,發送消息
int i=0;
while(i<1000){
Thread.sleep(1000L);
WrapperAppMessage message = new WrapperAppMessage();
message.setAgreeId((i+1)%5);
message.setCityId((i+1)%3);
message.setConnectType((i+1)%4);
message.setCount((i+100)%10);
message.setInstanceId((i+1)%6);
message.setProvinceId((i+1)%4);
message.setTimestamp(System.currentTimeMillis());
message.setValue((float)((i+200)%4));
producer.send(new ProducerRecord<>("NL_U_APP_ALARM_APP",message));
System.out.println(message.toString());
i++;
producer.flush();
}
producer.close();
}
示例3: produceKeyValuesSynchronously
import org.apache.kafka.clients.producer.Producer; //導入方法依賴的package包/類
/**
* @param topic Kafka topic to write the data records to
* @param records Data records to write to Kafka
* @param producerConfig Kafka producer configuration
* @param <K> Key type of the data records
* @param <V> Value type of the data records
*/
public static <K, V> void produceKeyValuesSynchronously(
String topic, Collection<KeyValue<K, V>> records, Properties producerConfig)
throws ExecutionException, InterruptedException {
Producer<K, V> producer = new KafkaProducer<>(producerConfig);
for (KeyValue<K, V> record : records) {
Future<RecordMetadata> f = producer.send(
new ProducerRecord<>(topic, record.key, record.value));
f.get();
}
producer.flush();
producer.close();
}
開發者ID:kaiwaehner,項目名稱:kafka-streams-machine-learning-examples,代碼行數:20,代碼來源:IntegrationTestUtils.java