Commit e7db5fc0 authored by Quxl's avatar Quxl

x

parent 04b53662
package com.egolm.shop.aliyunLog;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLong;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.aliyun.openservices.aliyun.log.producer.Producer;
import com.aliyun.openservices.aliyun.log.producer.Result;
import com.aliyun.openservices.aliyun.log.producer.errors.ResultFailedException;
import com.aliyun.openservices.log.common.LogItem;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
public class AliyunLogProducerWithFuture {
private static final ExecutorService EXECUTOR_SERVICE = Executors.newCachedThreadPool();
private static final Producer producer = AliyunLogUtil.createProducer();
private static final String project = System.getenv("PROJECT");
private static final String logStore = System.getenv("LOG_STORE");
public static void sendLog(String logText) {
try {
final AtomicLong completed = new AtomicLong(0);
List<LogItem> logItems = new ArrayList<LogItem>();
ListenableFuture<Result> future = producer.send(project, logStore, logItems);
Futures.addCallback(future, new SampleFutureCallback(project, logStore, completed), EXECUTOR_SERVICE);
} catch (Exception e) {
e.printStackTrace();
}
}
private static final class SampleFutureCallback implements FutureCallback<Result> {
private static final Logger LOGGER = LoggerFactory.getLogger(SampleFutureCallback.class);
private final String project;
private final String logStore;
private final AtomicLong completed;
SampleFutureCallback(String project, String logStore, AtomicLong completed) {
this.project = project;
this.logStore = logStore;
this.completed = completed;
}
@Override
public void onSuccess(Result result) {
LOGGER.info("Send logs successfully.");
completed.getAndIncrement();
}
@Override
public void onFailure(Throwable t) {
if (t instanceof ResultFailedException) {
Result result = ((ResultFailedException) t).getResult();
LOGGER.error("Failed to send logs, project={}, logStore={}, result={}", project, logStore, result);
} else {
LOGGER.error("Failed to send log, e=", t);
}
completed.getAndIncrement();
}
}
}
package com.egolm.shop.aliyunLog;
import com.aliyun.openservices.aliyun.log.producer.LogProducer;
import com.aliyun.openservices.aliyun.log.producer.Producer;
import com.aliyun.openservices.aliyun.log.producer.ProducerConfig;
import com.aliyun.openservices.aliyun.log.producer.ProjectConfig;
public class AliyunLogUtil {
public static Producer createProducer() {
ProducerConfig producerConfig = new ProducerConfig();
Producer producer = new LogProducer(producerConfig);
producer.putProjectConfig(buildProjectConfig());
return producer;
}
private static ProjectConfig buildProjectConfig() {
String project = System.getenv("PROJECT");
String endpoint = System.getenv("ENDPOINT");
String accessKeyId = System.getenv("ACCESS_KEY_ID");
String accessKeySecret = System.getenv("ACCESS_KEY_SECRET");
return new ProjectConfig(project, endpoint, accessKeyId, accessKeySecret);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment