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));
}
}
}
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.