Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
core-common/src/main/java/org/apache/kylin/common/kafka/KafkaMsgProducer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.kylin.common.kafka; | ||
|
||
import org.apache.kafka.clients.producer.KafkaProducer; | ||
import org.apache.kafka.clients.producer.Producer; | ||
import org.apache.kafka.clients.producer.ProducerConfig; | ||
import org.apache.kafka.clients.producer.ProducerRecord; | ||
import org.apache.kylin.common.KylinConfig; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
|
||
public class KafkaMsgProducer { | ||
private static final Logger logger = LoggerFactory.getLogger(KafkaMsgProducer.class); | ||
|
||
private static Producer<String, String> producer; | ||
private static Map<String, String> kafkaConfig; | ||
private static String TOPIC_NAME; | ||
|
||
|
||
private static Properties KafkaProperties = new Properties() { | ||
{ | ||
put(ProducerConfig.ACKS_CONFIG, "-1"); | ||
put(ProducerConfig.RETRIES_CONFIG, 3); | ||
put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer"); | ||
put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer"); | ||
put(ProducerConfig.COMPRESSION_TYPE_CONFIG, "lz4"); | ||
put(ProducerConfig.LINGER_MS_CONFIG, 100); | ||
put(ProducerConfig.BATCH_SIZE_CONFIG, 10000); | ||
put("security.protocol", "SASL_PLAINTEXT"); | ||
put("sasl.mechanism", "PLAIN"); | ||
} | ||
}; | ||
|
||
|
||
private KafkaMsgProducer() { | ||
} | ||
|
||
private static class BasicProducerHolder { | ||
private static final KafkaMsgProducer INSTANCE = new KafkaMsgProducer(); | ||
} | ||
|
||
public static final KafkaMsgProducer getInstance() { | ||
return BasicProducerHolder.INSTANCE; | ||
} | ||
|
||
public void init() { | ||
if (null == kafkaConfig) { | ||
kafkaConfig = KylinConfig.getInstanceFromEnv().getJobStatusKafkaConfig(); | ||
} | ||
if (null == producer) { | ||
KafkaProperties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaConfig.get("bootstrap.servers")); | ||
String format = "org.apache.kafka.common.security.plain.PlainLoginModule required username=\"%s.%s\" password=\"%s\";"; | ||
String jaasConfig = String.format(Locale.ROOT, format, kafkaConfig.get("clusterId"), kafkaConfig.get("appId"), kafkaConfig.get("password")); | ||
KafkaProperties.put("sasl.jaas.config", jaasConfig); | ||
producer = new KafkaProducer<>(KafkaProperties); | ||
} | ||
if (null == TOPIC_NAME) { | ||
TOPIC_NAME = kafkaConfig.get("topic.name"); | ||
} | ||
} | ||
|
||
public void sendJobStatusMessage(String message) { | ||
sendMessage(message); | ||
} | ||
|
||
public void sendEventMessage(String message) { | ||
sendMessage(message); | ||
} | ||
|
||
private void sendMessage(final String message) { | ||
init(); | ||
ProducerRecord<String, String> record = new ProducerRecord<>(TOPIC_NAME, message); | ||
producer.send(record, (recordMetadata, exception) -> { | ||
if (null != exception) { | ||
logger.error("kafka send message error.", exception); | ||
} | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters