Kafka Push Message (Producer) _kafka

Source: Internet
Author: User
Tags getmessage
Store map
public static map<string, islicodelog> Logmap = Collections
. Synchronizedmap (New hashmap<string, islicodelog> ());
Theme
public static String Mytopic = systemconfig.getstring ("Kafka_log_topic");
Service Code
public static String Sccode = systemconfig.getstring ("code");


/**
* 1. Log directly to Kafka server; 2. If Kafka downtime, log is saved to database
*
* @param log
* Log Entity
*/
public void Pushlogtokafka () {


if (Loglock.trylock ()) {
producer<string, string> Producer = null;
try {
if (logmap.size () > 0) {

Producer = new Kafkautil (). Getproducer ();
Traversing a map using iterator
For (map.entry<string, islicodelog> Entry:logmap
. EntrySet ()) {
if (null = = Entry) {
Break
}


Logger.info ("logmap.size=======>>>>>" + logmap.size ());
Islicodelog log = Entry.getvalue ();
if (null!= log) {
Log.setservicecode (Sccode);//Service Code
producerrecord<string, string> record = new producerrecord<string, string> (
Mytopic, Log.getislicode (),
Jsonutils.tojson (log));


/**
* Post message (asynchronous)
*
* 1.kafka server is normal, log sent directly to Kafka
*
* 2. The problem is: Kafka downtime, send () do not throw exceptions, this case log inserted into the database
*
*/
Producer.send (Record,
New Pushlogtokafkacallback (entry));
}
}
}


catch (Exception e) {
Logger.error ("[Parseislicodeservice.pushlogtokafka] Error:"
+ e.getmessage ());
finally {


if (null!= producer) {
Producer.close ();
}


Loglock.unlock ();


}
}


}




/**
* Log message pushed to Kafka callback function class
*
* @author WUJH
*
*/
Class Pushlogtokafkacallback implements Callback {


Private map.entry<string, islicodelog> Entry;


Pushlogtokafkacallback (map.entry<string, islicodelog> Entry) {
This.entry = entry;
}


@Override
public void Oncompletion (Recordmetadata metadata, Exception e) {
try {
if (null!= e) {
Parseislicodelogdao.insertislicodelog (Entry.getvalue ());
Logger.error ("Pushlogtokafkacallback[oncompletion] Error:"
+ e.getmessage ());
}
catch (Exception ex) {
Logger.error ("pushlogtokafkacallback[oncompletion] Catch_error:"
+ ex.getmessage ());
finally {
String key = Entry.getkey ();
if (Logmap.containskey (key)) {
Logmap.remove (Entry.getkey ());//delete map
}
}
}


}

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.