LogTrackingClient.java 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package client;
  2. import Utils.PostHelper;
  3. import com.nju.edu.logTracking.dto.LogConfigDto;
  4. import com.nju.edu.logTracking.enums.Level;
  5. import com.nju.edu.logTracking.enums.PublishMode;
  6. import com.nju.edu.logTracking.kafka.Config;
  7. import lombok.extern.slf4j.Slf4j;
  8. import java.io.IOException;
  9. import java.util.concurrent.ArrayBlockingQueue;
  10. import java.util.concurrent.ThreadPoolExecutor;
  11. import java.util.concurrent.TimeUnit;
  12. @Slf4j
  13. public class LogTrackingClient implements logTrackingApi {
  14. private static final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 20,
  15. 10000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(50));
  16. private final LogConfigDto logConfigDto = new LogConfigDto(
  17. Level.MEDIUM.getValue(),
  18. PublishMode.SINGLE.getValue(),
  19. "");
  20. public static LogTrackingClient getClient() {
  21. return new LogTrackingClient();
  22. }
  23. public LogTrackingClient lowLevel() {
  24. logConfigDto.setLevel(Level.LOW.getValue());
  25. return this;
  26. }
  27. public LogTrackingClient mediumLevel() {
  28. logConfigDto.setLevel(Level.MEDIUM.getValue());
  29. return this;
  30. }
  31. public LogTrackingClient highLevel() {
  32. logConfigDto.setLevel(Level.HIGH.getValue());
  33. return this;
  34. }
  35. public LogTrackingClient singleMode() {
  36. logConfigDto.setPublishMode(PublishMode.SINGLE.getValue());
  37. return this;
  38. }
  39. public LogTrackingClient batchMode() {
  40. logConfigDto.setPublishMode(PublishMode.BATCH.getValue());
  41. return this;
  42. }
  43. public LogTrackingClient log(String log) {
  44. logConfigDto.setLog(log);
  45. return this;
  46. }
  47. @Override
  48. public void publish() {
  49. threadPoolExecutor.execute(() -> {
  50. try {
  51. PostHelper.post(Config.SERVER + "/api/publish", logConfigDto);
  52. } catch (IOException e) {
  53. log.error(e.getMessage());
  54. }
  55. });
  56. }
  57. }