kafka-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gwens...@apache.org
Subject kafka git commit: MINOR: Example style improvements
Date Mon, 22 Feb 2016 19:40:41 GMT
Repository: kafka
Updated Branches:
  refs/heads/trunk d2527af99 -> e3ab96b2f


MINOR: Example style improvements

These are minor, but no reason to make our example code look worse than it has to.

Author: Ismael Juma <ismael@juma.me.uk>

Reviewers: Grant Henke

Closes #940 from ijuma/example-style-improvements


Project: http://git-wip-us.apache.org/repos/asf/kafka/repo
Commit: http://git-wip-us.apache.org/repos/asf/kafka/commit/e3ab96b2
Tree: http://git-wip-us.apache.org/repos/asf/kafka/tree/e3ab96b2
Diff: http://git-wip-us.apache.org/repos/asf/kafka/diff/e3ab96b2

Branch: refs/heads/trunk
Commit: e3ab96b2f0b429e0fe5991a185cd980b0d490e25
Parents: d2527af
Author: Ismael Juma <ismael@juma.me.uk>
Authored: Mon Feb 22 11:40:33 2016 -0800
Committer: Gwen Shapira <cshapi@gmail.com>
Committed: Mon Feb 22 11:40:33 2016 -0800

----------------------------------------------------------------------
 .../examples/KafkaConsumerProducerDemo.java     |  4 ++--
 .../java/kafka/examples/KafkaProperties.java    | 23 ++++++++++----------
 .../src/main/java/kafka/examples/Producer.java  | 16 ++++++--------
 .../java/kafka/examples/SimpleConsumerDemo.java |  2 +-
 4 files changed, 21 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/kafka/blob/e3ab96b2/examples/src/main/java/kafka/examples/KafkaConsumerProducerDemo.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java/kafka/examples/KafkaConsumerProducerDemo.java b/examples/src/main/java/kafka/examples/KafkaConsumerProducerDemo.java
index e732d5c..414a6f7 100644
--- a/examples/src/main/java/kafka/examples/KafkaConsumerProducerDemo.java
+++ b/examples/src/main/java/kafka/examples/KafkaConsumerProducerDemo.java
@@ -16,9 +16,9 @@
  */
 package kafka.examples;
 
-public class KafkaConsumerProducerDemo implements KafkaProperties {
+public class KafkaConsumerProducerDemo {
     public static void main(String[] args) {
-        final boolean isAsync = args.length > 0 ? !args[0].trim().toLowerCase().equals("sync")
: true;
+        boolean isAsync = args.length == 0 || !args[0].trim().toLowerCase().equals("sync");
         Producer producerThread = new Producer(KafkaProperties.TOPIC, isAsync);
         producerThread.start();
 

http://git-wip-us.apache.org/repos/asf/kafka/blob/e3ab96b2/examples/src/main/java/kafka/examples/KafkaProperties.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java/kafka/examples/KafkaProperties.java b/examples/src/main/java/kafka/examples/KafkaProperties.java
index b57e1bd..853d6bf 100644
--- a/examples/src/main/java/kafka/examples/KafkaProperties.java
+++ b/examples/src/main/java/kafka/examples/KafkaProperties.java
@@ -16,16 +16,15 @@
  */
 package kafka.examples;
 
-public interface KafkaProperties {
-    String ZK_CONNECT = "127.0.0.1:2181";
-    String GROUP_ID = "group1";
-    String TOPIC = "topic1";
-    String KAFKA_SERVER_URL = "localhost";
-    int KAFKA_SERVER_PORT = 9092;
-    int KAFKA_PRODUCER_BUFFER_SIZE = 64 * 1024;
-    int CONNECTION_TIMEOUT = 100000;
-    int RECONNECT_INTERVAL = 10000;
-    String TOPIC2 = "topic2";
-    String TOPIC3 = "topic3";
-    String CLIENT_ID = "SimpleConsumerDemoClient";
+public class KafkaProperties {
+    public static final String TOPIC = "topic1";
+    public static final String KAFKA_SERVER_URL = "localhost";
+    public static final int KAFKA_SERVER_PORT = 9092;
+    public static final int KAFKA_PRODUCER_BUFFER_SIZE = 64 * 1024;
+    public static final int CONNECTION_TIMEOUT = 100000;
+    public static final String TOPIC2 = "topic2";
+    public static final String TOPIC3 = "topic3";
+    public static final String CLIENT_ID = "SimpleConsumerDemoClient";
+
+    private KafkaProperties() {}
 }

http://git-wip-us.apache.org/repos/asf/kafka/blob/e3ab96b2/examples/src/main/java/kafka/examples/Producer.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java/kafka/examples/Producer.java b/examples/src/main/java/kafka/examples/Producer.java
index 393bf1e..b83c029 100644
--- a/examples/src/main/java/kafka/examples/Producer.java
+++ b/examples/src/main/java/kafka/examples/Producer.java
@@ -35,7 +35,7 @@ public class Producer extends Thread {
         props.put("client.id", "DemoProducer");
         props.put("key.serializer", "org.apache.kafka.common.serialization.IntegerSerializer");
         props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
-        producer = new KafkaProducer<Integer, String>(props);
+        producer = new KafkaProducer<>(props);
         this.topic = topic;
         this.isAsync = isAsync;
     }
@@ -46,18 +46,16 @@ public class Producer extends Thread {
             String messageStr = "Message_" + messageNo;
             long startTime = System.currentTimeMillis();
             if (isAsync) { // Send asynchronously
-                producer.send(new ProducerRecord<Integer, String>(topic,
+                producer.send(new ProducerRecord<>(topic,
                     messageNo,
                     messageStr), new DemoCallBack(startTime, messageNo, messageStr));
             } else { // Send synchronously
                 try {
-                    producer.send(new ProducerRecord<Integer, String>(topic,
+                    producer.send(new ProducerRecord<>(topic,
                         messageNo,
                         messageStr)).get();
                     System.out.println("Sent message: (" + messageNo + ", " + messageStr
+ ")");
-                } catch (InterruptedException e) {
-                    e.printStackTrace();
-                } catch (ExecutionException e) {
+                } catch (InterruptedException | ExecutionException e) {
                     e.printStackTrace();
                 }
             }
@@ -68,9 +66,9 @@ public class Producer extends Thread {
 
 class DemoCallBack implements Callback {
 
-    private long startTime;
-    private int key;
-    private String message;
+    private final long startTime;
+    private final int key;
+    private final String message;
 
     public DemoCallBack(long startTime, int key, String message) {
         this.startTime = startTime;

http://git-wip-us.apache.org/repos/asf/kafka/blob/e3ab96b2/examples/src/main/java/kafka/examples/SimpleConsumerDemo.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java/kafka/examples/SimpleConsumerDemo.java b/examples/src/main/java/kafka/examples/SimpleConsumerDemo.java
index 1c56867..7eef51e 100644
--- a/examples/src/main/java/kafka/examples/SimpleConsumerDemo.java
+++ b/examples/src/main/java/kafka/examples/SimpleConsumerDemo.java
@@ -71,7 +71,7 @@ public class SimpleConsumerDemo {
         printMessages(fetchResponse.messageSet(KafkaProperties.TOPIC2, 0));
 
         System.out.println("Testing single multi-fetch");
-        Map<String, List<Integer>> topicMap = new HashMap<String, List<Integer>>();
+        Map<String, List<Integer>> topicMap = new HashMap<>();
         topicMap.put(KafkaProperties.TOPIC2, Collections.singletonList(0));
         topicMap.put(KafkaProperties.TOPIC3, Collections.singletonList(0));
         req = new FetchRequestBuilder()


Mime
View raw message