|
@@ -0,0 +1,59 @@
|
|
|
+package com.tidecloud.dataacceptance.service;
|
|
|
+
|
|
|
+import io.netty.util.internal.PlatformDependent;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.ReflectionUtils;
|
|
|
+
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.Timer;
|
|
|
+import java.util.TimerTask;
|
|
|
+import java.util.concurrent.atomic.AtomicLong;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * 日志更新
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class DirectMemoryReporterImpl {
|
|
|
+
|
|
|
+ private static final int _ik = 1024;
|
|
|
+
|
|
|
+ private static final String BUSINESS_KEY = "netty_direct_memory";
|
|
|
+
|
|
|
+ private AtomicLong directMemory;
|
|
|
+ private Long maxDirectMemory;
|
|
|
+
|
|
|
+ private static Logger logger = LoggerFactory.getLogger(DirectMemoryReporterImpl.class);
|
|
|
+
|
|
|
+ @PostConstruct
|
|
|
+ public void init() {
|
|
|
+
|
|
|
+ try {
|
|
|
+ Field field = ReflectionUtils.findField(PlatformDependent.class, "DIRECT_MEMORY_COUNTER");
|
|
|
+ field.setAccessible(true);
|
|
|
+ directMemory = (AtomicLong) field.get(PlatformDependent.class);
|
|
|
+ Field field1 = ReflectionUtils.findField(PlatformDependent.class, "DIRECT_MEMORY_LIMIT");
|
|
|
+ field1.setAccessible(true);
|
|
|
+ maxDirectMemory = (Long) field1.get(PlatformDependent.class);
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error(e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ TimerTask timerTask = new TimerTask() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ System.out.println("task run:" + new Date());
|
|
|
+ long m = directMemory.get() / _ik;
|
|
|
+ logger.error(BUSINESS_KEY + "maxDirectMemory==={}:{}K", maxDirectMemory/_ik, m);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ Timer timer = new Timer();
|
|
|
+ //安排指定的任务在指定的时间开始进行重复的固定延迟执行。这里是每3秒执行一次
|
|
|
+ timer.schedule(timerTask, 10, 2000);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|