Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
K
kafka-beginners-cource
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Nagarjuna Reddy Pothi Reddy
kafka-beginners-cource
Commits
2ebdc99e
Commit
2ebdc99e
authored
Apr 15, 2020
by
Nagarjuna Reddy Pothi Reddy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Producer with Callback and Keys
parent
62242883
Changes
8
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
116 additions
and
1 deletion
+116
-1
ProducerDemo.java
...in/java/com/github/nagarjun/kafka/demo1/ProducerDemo.java
+1
-1
ProducerDemoKeys.java
...ava/com/github/nagarjun/kafka/demo1/ProducerDemoKeys.java
+61
-0
ProducerDemoWithCallback.java
...github/nagarjun/kafka/demo1/ProducerDemoWithCallback.java
+54
-0
ProducerDemo.class
...lasses/com/github/nagarjun/kafka/demo1/ProducerDemo.class
+0
-0
ProducerDemoKeys$1.class
.../com/github/nagarjun/kafka/demo1/ProducerDemoKeys$1.class
+0
-0
ProducerDemoKeys.class
...es/com/github/nagarjun/kafka/demo1/ProducerDemoKeys.class
+0
-0
ProducerDemoWithCallback$1.class
...hub/nagarjun/kafka/demo1/ProducerDemoWithCallback$1.class
+0
-0
ProducerDemoWithCallback.class
...ithub/nagarjun/kafka/demo1/ProducerDemoWithCallback.class
+0
-0
No files found.
src/main/java/com/github/nagarjun/kafka/demo1/ProducerDemo.java
View file @
2ebdc99e
...
@@ -24,7 +24,7 @@ public class ProducerDemo {
...
@@ -24,7 +24,7 @@ public class ProducerDemo {
// Create a Producer Record
// Create a Producer Record
ProducerRecord
<
String
,
String
>
record
=
ProducerRecord
<
String
,
String
>
record
=
new
ProducerRecord
<
String
,
String
>(
"
first_topic
"
,
"Hi, Welcome to Producer"
);
new
ProducerRecord
<
String
,
String
>(
"
my_topic1
"
,
"Hi, Welcome to Producer"
);
// Send Data - Asyncronous
// Send Data - Asyncronous
producer
.
send
(
record
);
producer
.
send
(
record
);
...
...
src/main/java/com/github/nagarjun/kafka/demo1/ProducerDemoKeys.java
0 → 100644
View file @
2ebdc99e
package
com
.
github
.
nagarjun
.
kafka
.
demo1
;
import
org.apache.kafka.clients.producer.*
;
import
org.apache.kafka.common.serialization.StringSerializer
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
java.util.Properties
;
import
java.util.concurrent.ExecutionException
;
public
class
ProducerDemoKeys
{
public
static
void
main
(
String
[]
args
)
throws
ExecutionException
,
InterruptedException
{
final
Logger
logger
=
LoggerFactory
.
getLogger
(
ProducerDemoKeys
.
class
);
//Create Producer Properties
Properties
properties
=
new
Properties
();
properties
.
setProperty
(
ProducerConfig
.
BOOTSTRAP_SERVERS_CONFIG
,
"127.0.0.1:9092"
);
properties
.
setProperty
(
ProducerConfig
.
KEY_SERIALIZER_CLASS_CONFIG
,
StringSerializer
.
class
.
getName
());
properties
.
setProperty
(
ProducerConfig
.
VALUE_SERIALIZER_CLASS_CONFIG
,
StringSerializer
.
class
.
getName
());
// Create Producer
KafkaProducer
<
String
,
String
>
producer
=
new
KafkaProducer
<
String
,
String
>(
properties
);
for
(
int
i
=
0
;
i
<
10
;
i
++)
{
String
topic
=
"my_topic1"
;
String
value
=
"Welcome to Producer. Message : "
+
Integer
.
toString
(
i
);
String
key
=
"ID_"
+
Integer
.
toString
(
i
);
logger
.
info
(
"Key :"
+
key
);
// Create a Producer Record
ProducerRecord
<
String
,
String
>
record
=
new
ProducerRecord
<
String
,
String
>(
topic
,
key
,
value
);
// Send Data - Asyncronous
producer
.
send
(
record
,
new
Callback
()
{
public
void
onCompletion
(
RecordMetadata
recordMetadata
,
Exception
e
)
{
//executes every time a record sent successfully or an exception is thrown
if
(
e
==
null
)
{
logger
.
info
(
"Recieved new Metadata . \n"
+
"Topic :"
+
recordMetadata
.
topic
()
+
"\n"
+
"Partition :"
+
recordMetadata
.
partition
()
+
"\n"
+
"Offset :"
+
recordMetadata
.
offset
()
+
"\n"
+
"Timestamp :"
+
recordMetadata
.
timestamp
());
}
else
{
logger
.
error
(
"Error while sending the data "
,
e
);
}
}
}).
get
();
// Synchronous
}
//Flush Data and Close Producer
producer
.
flush
();
producer
.
close
();
}
}
src/main/java/com/github/nagarjun/kafka/demo1/ProducerDemoWithCallback.java
0 → 100644
View file @
2ebdc99e
package
com
.
github
.
nagarjun
.
kafka
.
demo1
;
import
org.apache.kafka.clients.producer.*
;
import
org.apache.kafka.common.serialization.StringSerializer
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
java.util.Properties
;
public
class
ProducerDemoWithCallback
{
public
static
void
main
(
String
[]
args
)
{
final
Logger
logger
=
LoggerFactory
.
getLogger
(
ProducerDemoWithCallback
.
class
);
//Create Producer Properties
Properties
properties
=
new
Properties
();
properties
.
setProperty
(
ProducerConfig
.
BOOTSTRAP_SERVERS_CONFIG
,
"127.0.0.1:9092"
);
properties
.
setProperty
(
ProducerConfig
.
KEY_SERIALIZER_CLASS_CONFIG
,
StringSerializer
.
class
.
getName
());
properties
.
setProperty
(
ProducerConfig
.
VALUE_SERIALIZER_CLASS_CONFIG
,
StringSerializer
.
class
.
getName
());
// Create Producer
KafkaProducer
<
String
,
String
>
producer
=
new
KafkaProducer
<
String
,
String
>(
properties
);
for
(
int
i
=
0
;
i
<
10
;
i
++)
{
// Create a Producer Record
ProducerRecord
<
String
,
String
>
record
=
new
ProducerRecord
<
String
,
String
>(
"my_topic1"
,
"Hi, Welcome to Producer. Message : "
+
Integer
.
toString
(
i
));
// Send Data - Asyncronous
producer
.
send
(
record
,
new
Callback
()
{
public
void
onCompletion
(
RecordMetadata
recordMetadata
,
Exception
e
)
{
//executes every time a record sent successfully or an exception is thrown
if
(
e
==
null
)
{
logger
.
info
(
"Recieved new Metadata . \n"
+
"Topic :"
+
recordMetadata
.
topic
()
+
"\n"
+
"Partition :"
+
recordMetadata
.
partition
()
+
"\n"
+
"Offset :"
+
recordMetadata
.
offset
()
+
"\n"
+
"Timestamp :"
+
recordMetadata
.
timestamp
());
}
else
{
logger
.
error
(
"Error while sending the data "
,
e
);
}
}
});
}
//Flush Data and Close Producer
producer
.
flush
();
producer
.
close
();
}
}
target/classes/com/github/nagarjun/kafka/demo1/ProducerDemo.class
View file @
2ebdc99e
No preview for this file type
target/classes/com/github/nagarjun/kafka/demo1/ProducerDemoKeys$1.class
0 → 100644
View file @
2ebdc99e
File added
target/classes/com/github/nagarjun/kafka/demo1/ProducerDemoKeys.class
0 → 100644
View file @
2ebdc99e
File added
target/classes/com/github/nagarjun/kafka/demo1/ProducerDemoWithCallback$1.class
0 → 100644
View file @
2ebdc99e
File added
target/classes/com/github/nagarjun/kafka/demo1/ProducerDemoWithCallback.class
0 → 100644
View file @
2ebdc99e
File added
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment