Skip to content

Commit

Permalink
Merge pull request #3 from kmizumar/readme-simplecounter
Browse files Browse the repository at this point in the history
some improvements for SimpleCounter
  • Loading branch information
gwenshap committed Jan 14, 2016
2 parents 4079dc6 + bf16516 commit 0b65dd1
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
34 changes: 34 additions & 0 deletions SimpleCounter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# SimpleCounter

SimpleCounter is an example class to demonstrate how to implement a Kafka producer using Java.
There're two versions of Scala producer, see DemoProducerOld for old Scala producer
and DemoProducerNewJava for new Scala producer.

## Building Uber JAR

Uber JAR is built using [Apache Maven](http://maven.apache.org/).
To build Uber JAR, run:

mvn clean package

## Run SimpleCounter

Running SimpleCounter first requires [Building Uber JAR](#building-uber-jar).
Once Uber JAR is built, SimpleCounter can be run using:

./run-params.sh broker-list topic old/new sync/async delay count

broker-list : list of Kafka broker(s)
topic : Kafka topic to write message(s)
old/new : specify which producer to use (DemoProducerOld or DemoProducerNewJava)
sync/async : specify synchronous or asynchronous mode
delay : delay in milliseconds
count : numbers to generate

## A Note for O'Reilly Kafka Training Video viewers

If you saw Chapter 3, "Writing Kafka Producer" of
["Introduction to Apache Kafka, A Quick Primer for Developers and Administrators"](http://shop.oreilly.com/product/0636920038603.do)
and came to this site, the example code is slightly changed to show how to use new API.
Code shown in the video is moved into DemoProducerOld.java file and new argument is introduced to select
which version of API to use.
8 changes: 8 additions & 0 deletions SimpleCounter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@
<finalName>uber-${artifactId}-${version}</finalName>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ public interface DemoProducer {
* create configuration for the producer
* consult Kafka documentation for exact meaning of each configuration parameter
*/
public void configure(String brokerList, String sync);
void configure(String brokerList, String sync);

/* start the producer */
public void start();
void start();

/**
* create record and send to Kafka
* because the key is null, data will be sent to a random partition.
* exact behavior will be different depending on producer implementation
*/
public void produce(String s) throws ExecutionException, InterruptedException;
void produce(String s) throws ExecutionException, InterruptedException;

public void close();
void close();
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class SimpleCounter {
public static void main(String[] args) throws InterruptedException, ExecutionException {

if (args.length == 0) {
System.out.println("SimpleCounterOldProducer {broker-list} {topic} {type old/new} {type sync/async} {delay (ms)} {count}");
System.out.println("SimpleCounter {broker-list} {topic} {type old/new} {type sync/async} {delay (ms)} {count}");
return;
}

Expand All @@ -44,8 +44,10 @@ public static void main(String[] args) throws InterruptedException, ExecutionExc
producer = new DemoProducerOld(topic);
else if (age.equals("new"))
producer = new DemoProducerNewJava(topic);
else
else {
System.out.println("Third argument should be old or new, got " + age);
System.exit(-1);
}

/* start a producer */
producer.configure(brokerList, sync);
Expand Down

0 comments on commit 0b65dd1

Please sign in to comment.