I here Kafka consumer code is copied online, is to open a thread monitoring Kafka topic, a message on the processing. The code to begin with is this:
public void Kafkastart () {final String topic = HipchatAction.properties.getProperty ("Kafka.hipchat.topic"); Final int partitionnum = integer.valueof (HipchatAction.properties.getProperty ("Kafka.hipchat.topic.partitionNum"))
;
Log.debug ("Comes to Kafkastart () with topic:" + topic + ", Partitionnum:" + partitionnum);
map<string, integer> topiccountmap = new hashmap<> ();
Topiccountmap.put (topic, partitionnum); Map<string, list<kafkastream<byte[], byte[]>>> streams = Connector.createmessagestreams (
TOPICCOUNTMAP);
List<kafkastream<byte[], byte[]>> partitions = streams.get (topic);
Executorservice executor = Executors.newfixedthreadpool (Partitionnum * 2); For (kafkastream<byte[], byte[]> partition:partitions) {/** * Here also it's an unknown Issue, if use anonymous inner class, then the thread seems died, must has * a named Inner class!
*/Executor.execute (new Messagerunner (partition));
}} class Messagerunner implements Runnable {private kafkastream<byte[], byte[]> partition;
Messagerunner (kafkastream<byte[], byte[]> partition) {this.partition = partition;
} public void Run () {consumeriterator<byte[], byte[]> it = Partition.iterator ();
while (It.hasnext ()) {messageandmetadata<byte[], byte[]> Msgmeta = It.next ();
/** * In order to consume Chinese a message, here should use CharSet.
*/String jsonstr = new String (Msgmeta.message (), standardcharsets.utf_8);
Log.debug ("********* Message to being consumed in UTF-8 is::" + jsonstr);
Kafkamsgvo msg = new Gson (). Fromjson (Jsonstr, Kafkamsgvo.class); Hipchataction.sendmessagetoroom (Msg.getroomname (), Msg.gettoken (), Msg.getmsgtext ()); }
}
}
Method Kafkastart () is executed when the spring container is started.
For kafkamsgvo msg = new Gson (). Fromjson (Jsonstr, Kafkamsgvo.class); This line, because my program on the message request is an object named KAFKAMSGVO JSON format data, but the test when the tester randomly sent a message, no object property assignment and assembled into JSON data, so throw jsonsyntaxexception. The problem is this, because once an exception is thrown, the thread is destroyed and there is no way to consume subsequent messages, although Kafka consumer still senses that there are new messages coming in topic.
Workaround:
In order not to allow the exception to break the consumer thread, I took a position transfer of the error code, forwarded the resulting message directly through Apache AKKA, and then handled the message by AKKA's OnReceive (), which is the error code moved to OnReceive (), so The robustness of the consumer thread is ensured. The code is as follows:
Class Messagerunner implements Runnable {
private kafkastream<byte[], byte[]> partition;
Messagerunner (kafkastream<byte[], byte[]> partition) {
this.partition = partition;
}
public void Run () {
consumeriterator<byte[], byte[]> it = Partition.iterator ();
while (It.hasnext ()) {
messageandmetadata<byte[], byte[]> Msgmeta = It.next ();
/**
* In order to consume Chinese a message, here should use CharSet.
*/
string jsonstr = new String (Msgmeta.message (), standardcharsets.utf_8);
Log.debug ("********* Message to being consumed in UTF-8 is::" + jsonstr);
Actorref sender = Akkasystem.getmsgconductor ();
Sender.tell (New Akkaadaptor (Jsonstr, hipchataction), sender);}}}
Once the message has been AKKA forwarded, the message processing code is placed in the Actor of the AKKA:
} else if (message instanceof akkaadaptor) {
Akkaadaptor akkaadaptor = (akkaadaptor) message;
String textmessage = Akkaadaptor.gettextmessage ();
Kafkamsgvo msg = null;
try {
msg = new Gson (). Fromjson (TextMessage, Kafkamsgvo.class);
} catch (Exception e) {
log.debug ( TextMessage + "is malformed, it could miss some important property (value).");
return;
}
Hipchataction hipchataction = Akkaadaptor.gethipchataction ();
Log.debug ("Kafka message sent by AKKA is::" + msg.getmsgtext ());
Hipchataction.sendmessagetoroom (Msg.getroomname (), Msg.gettoken (), Msg.getmsgtext ());