| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- package client;
- import Utils.PostHelper;
- import com.nju.edu.logTracking.dto.LogConfigDto;
- import com.nju.edu.logTracking.enums.Level;
- import com.nju.edu.logTracking.enums.PublishMode;
- import com.nju.edu.logTracking.kafka.Config;
- import lombok.extern.slf4j.Slf4j;
- import java.io.IOException;
- import java.util.concurrent.ArrayBlockingQueue;
- import java.util.concurrent.ThreadPoolExecutor;
- import java.util.concurrent.TimeUnit;
- @Slf4j
- public class LogTrackingClient implements logTrackingApi {
- private static final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 20,
- 10000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(50));
- private final LogConfigDto logConfigDto = new LogConfigDto(
- Level.MEDIUM.getValue(),
- PublishMode.SINGLE.getValue(),
- "");
- public static LogTrackingClient getClient() {
- return new LogTrackingClient();
- }
- public LogTrackingClient lowLevel() {
- logConfigDto.setLevel(Level.LOW.getValue());
- return this;
- }
- public LogTrackingClient mediumLevel() {
- logConfigDto.setLevel(Level.MEDIUM.getValue());
- return this;
- }
- public LogTrackingClient highLevel() {
- logConfigDto.setLevel(Level.HIGH.getValue());
- return this;
- }
- public LogTrackingClient singleMode() {
- logConfigDto.setPublishMode(PublishMode.SINGLE.getValue());
- return this;
- }
- public LogTrackingClient batchMode() {
- logConfigDto.setPublishMode(PublishMode.BATCH.getValue());
- return this;
- }
- public LogTrackingClient log(String log) {
- logConfigDto.setLog(log);
- return this;
- }
- @Override
- public void publish() {
- threadPoolExecutor.execute(() -> {
- try {
- PostHelper.post(Config.SERVER + "/api/publish", logConfigDto);
- } catch (IOException e) {
- log.error(e.getMessage());
- }
- });
- }
- }
|