use of core.framework.util.StopWatch in project core-ng-project by neowu.
the class KafkaMessagePublisher method publish.
@Override
public void publish(String topic, String key, T value) {
if (topic == null)
throw new Error("topic must not be null");
// if key is null, kafka will pick random partition which breaks determinacy
if (key == null)
throw new Error("key must not be null");
validator.validate(value);
StopWatch watch = new StopWatch();
byte[] message = writer.toJSON(value);
try {
ProducerRecord<String, byte[]> record = new ProducerRecord<>(topic, key, message);
Headers headers = record.headers();
headers.add(KafkaHeaders.HEADER_CLIENT_IP, Strings.bytes(Network.localHostAddress()));
if (logManager.appName != null)
headers.add(KafkaHeaders.HEADER_CLIENT, Strings.bytes(logManager.appName));
linkContext(headers);
producer.send(record);
} finally {
long elapsedTime = watch.elapsedTime();
// kafka producer send message in background, the main purpose of track is to count how many message sent in action
ActionLogContext.track("kafka", elapsedTime);
logger.debug("publish, topic={}, key={}, message={}, elapsedTime={}", topic, key, new BytesParam(message), elapsedTime);
}
}
use of core.framework.util.StopWatch in project core-ng-project by neowu.
the class HTMLTemplateEngine method add.
private void add(TemplateSource source, Class<?> modelClass) {
StopWatch watch = new StopWatch();
String name = source.name();
try {
HTMLTemplate previous = templates.putIfAbsent(name, new HTMLTemplateBuilder(source, modelClass).build());
if (previous != null)
throw Exceptions.error("template is already added, name={}", name);
} finally {
logger.info("add, name={}, modelClass={}, elapsedTime={}", name, modelClass.getCanonicalName(), watch.elapsedTime());
}
}
use of core.framework.util.StopWatch in project core-ng-project by neowu.
the class MessageProcessor method consume.
private <T> void consume(String topic, ConsumerRecords<String, byte[]> records, JSONReader<T> reader, java.util.function.Consumer<List<T>> consumer) {
int messageSize = 0;
List<T> messages = new ArrayList<>();
for (ConsumerRecord<String, byte[]> record : records.records(topic)) {
byte[] body = record.value();
messages.add(reader.fromJSON(body));
messageSize += body.length;
}
if (messages.isEmpty())
return;
StopWatch watch = new StopWatch();
try {
consumer.accept(messages);
} finally {
long elapsedTime = watch.elapsedTime();
logger.info("consume messages, topic={}, count={}, size={}, elapsedTime={}", topic, messages.size(), messageSize, elapsedTime);
}
}
use of core.framework.util.StopWatch in project core-ng-project by neowu.
the class TemplateManager method add.
public void add(String templatePath, Class<?> modelClass) {
StopWatch watch = new StopWatch();
try {
Map<String, HTMLTemplate> previous = templates.putIfAbsent(templatePath, load(templatePath, modelClass));
if (previous != null)
throw Exceptions.error("template was registered, templatePath={}", templatePath);
if (webDirectory.localEnv) {
Path path = webDirectory.path(templatePath);
templateLastModifiedTimes.put(templatePath, Files.lastModified(path));
}
} finally {
logger.info("add, templatePath={}, modelClass={}, elapsedTime={}", templatePath, modelClass.getCanonicalName(), watch.elapsedTime());
}
}
use of core.framework.util.StopWatch in project core-ng-project by neowu.
the class ElasticSearchTypeImpl method get.
@Override
public Optional<T> get(GetRequest request) {
StopWatch watch = new StopWatch();
String index = request.index == null ? this.index : request.index;
int hits = 0;
try {
GetResponse response = client().prepareGet(index, type, request.id).get();
if (!response.isExists())
return Optional.empty();
hits = 1;
return Optional.of(reader.fromJSON(response.getSourceAsBytes()));
} catch (ElasticsearchException e) {
// due to elastic search uses async executor to run, we have to wrap the exception to retain the original place caused the exception
throw new SearchException(e);
} finally {
long elapsedTime = watch.elapsedTime();
ActionLogContext.track("elasticsearch", elapsedTime, hits, 0);
logger.debug("get, index={}, type={}, id={}, elapsedTime={}", index, type, request.id, elapsedTime);
checkSlowOperation(elapsedTime);
}
}
Aggregations