Kafka consumer cannot consume information and its handling methods

Source: Internet
Author: User
Tags error code json hipchat

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 ());

	


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.