test: add example for get plugin info

pull/836/head
huangchengxing 3 years ago
parent 90afc2369c
commit bcda493860

@ -148,6 +148,17 @@ public class DefaultThreadPoolPluginRegistry implements ThreadPoolPluginRegistry
}
}
@Override
public Collection<ThreadPoolPlugin> getAllPlugins() {
Lock readLock = instanceLock.readLock();
readLock.lock();
try {
return registeredPlugins.values();
} finally {
readLock.unlock();
}
}
/**
* Whether the {@link ThreadPoolPlugin} has been registered.
*

@ -36,6 +36,13 @@ public interface ThreadPoolPluginRegistry {
*/
void clear();
/**
* Get all registered plugins.
*
* @return plugins
*/
Collection<ThreadPoolPlugin> getAllPlugins();
/**
* Register a {@link ThreadPoolPlugin}
*

@ -59,6 +59,16 @@ public interface ThreadPoolPluginRegistryDelegate extends ThreadPoolPluginRegist
getThreadPoolPluginRegistry().unregister(pluginId);
}
/**
* Get all registered plugins.
*
* @return plugins
*/
@Override
default Collection<ThreadPoolPlugin> getAllPlugins() {
return getThreadPoolPluginRegistry().getAllPlugins();
}
/**
* Get {@link ThreadPoolPlugin}
*

@ -19,6 +19,7 @@ package cn.hippo4j.core.plugin.impl;
import cn.hippo4j.core.executor.ExtensibleThreadPoolExecutor;
import cn.hippo4j.core.plugin.ExecuteAwarePlugin;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.NonNull;
import org.springframework.core.task.TaskDecorator;
@ -40,6 +41,7 @@ public class TaskDecoratorPlugin implements ExecuteAwarePlugin {
*
* @return id
*/
@JsonProperty("pluginId")
@Override
public String getId() {
return PLUGIN_NAME;
@ -48,6 +50,7 @@ public class TaskDecoratorPlugin implements ExecuteAwarePlugin {
/**
* decorators
*/
@JsonProperty("decorators")
@Getter
private final List<TaskDecorator> decorators = new ArrayList<>();

@ -18,6 +18,7 @@
package cn.hippo4j.core.plugin.impl;
import cn.hippo4j.core.plugin.RejectedAwarePlugin;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;
@ -38,6 +39,7 @@ public class TaskRejectCountRecordPlugin implements RejectedAwarePlugin {
*
* @return id
*/
@JsonProperty("pluginId")
@Override
public String getId() {
return PLUGIN_NAME;
@ -66,6 +68,7 @@ public class TaskRejectCountRecordPlugin implements RejectedAwarePlugin {
*
* @return reject count num
*/
@JsonProperty("rejectCount")
public Long getRejectCountNum() {
return rejectCount.get();
}

@ -21,6 +21,7 @@ import cn.hippo4j.common.config.ApplicationContextHolder;
import cn.hippo4j.core.executor.ExtensibleThreadPoolExecutor;
import cn.hippo4j.core.executor.ThreadPoolNotifyAlarmHandler;
import cn.hippo4j.core.plugin.RejectedAwarePlugin;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Optional;
import java.util.concurrent.ThreadPoolExecutor;
@ -39,6 +40,7 @@ public class TaskRejectNotifyAlarmPlugin implements RejectedAwarePlugin {
*
* @return id
*/
@JsonProperty("pluginId")
@Override
public String getId() {
return PLUGIN_NAME;

@ -21,6 +21,7 @@ import cn.hippo4j.common.toolkit.SyncTimeRecorder;
import cn.hippo4j.core.executor.ExtensibleThreadPoolExecutor;
import cn.hippo4j.core.plugin.ExecuteAwarePlugin;
import cn.hippo4j.core.toolkit.SystemClock;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.RequiredArgsConstructor;
import java.util.Objects;
@ -41,11 +42,17 @@ public class TaskTimeRecordPlugin extends SyncTimeRecorder implements ExecuteAwa
*
* @return id
*/
@JsonProperty("pluginId")
@Override
public String getId() {
return PLUGIN_NAME;
}
@JsonProperty("taskTimeInfo")
public Summary getInfo() {
return summarize();
}
/**
* start times of executed tasks
*/

@ -20,6 +20,7 @@ package cn.hippo4j.core.plugin.impl;
import cn.hippo4j.common.config.ApplicationContextHolder;
import cn.hippo4j.core.executor.ExtensibleThreadPoolExecutor;
import cn.hippo4j.core.executor.ThreadPoolNotifyAlarmHandler;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
@ -42,6 +43,7 @@ public class TaskTimeoutNotifyAlarmPlugin extends TaskTimeRecordPlugin {
*
* @return id
*/
@JsonProperty("pluginId")
@Override
public String getId() {
return PLUGIN_NAME;

@ -20,6 +20,7 @@ package cn.hippo4j.core.plugin.impl;
import cn.hippo4j.common.toolkit.CollectionUtil;
import cn.hippo4j.core.executor.ExtensibleThreadPoolExecutor;
import cn.hippo4j.core.plugin.ShutdownAwarePlugin;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
@ -50,6 +51,7 @@ public class ThreadPoolExecutorShutdownPlugin implements ShutdownAwarePlugin {
*
* @return id
*/
@JsonProperty("pluginId")
@Override
public String getId() {
return PLUGIN_NAME;

@ -0,0 +1,63 @@
package cn.hippo4j.core.plugin;
import cn.hippo4j.common.toolkit.ThreadUtil;
import cn.hippo4j.core.executor.ExtensibleThreadPoolExecutor;
import cn.hippo4j.core.plugin.impl.TaskDecoratorPlugin;
import cn.hippo4j.core.plugin.impl.TaskRejectCountRecordPlugin;
import cn.hippo4j.core.plugin.impl.TaskTimeRecordPlugin;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import org.junit.Test;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
/**
* test {@link ThreadPoolPlugin}'s info to json
*
* @author huangchengxing
*/
public class PluginInfoToJsonTest {
private final ObjectMapper objectMapper = new ObjectMapper();
private final AtomicInteger taskExecuteCount = new AtomicInteger(0);
@SneakyThrows
@Test
public void testToJson() {
ExtensibleThreadPoolExecutor executor = new ExtensibleThreadPoolExecutor(
"test", new DefaultThreadPoolPluginRegistry(),
1, 1, 1000L, TimeUnit.MILLISECONDS,
new ArrayBlockingQueue<>(1), Thread::new, new ThreadPoolExecutor.DiscardPolicy()
);
// TaskDecoratorPlugin
TaskDecoratorPlugin taskDecoratorPlugin = new TaskDecoratorPlugin();
taskDecoratorPlugin.addDecorator(runnable -> () -> {
taskExecuteCount.incrementAndGet();
runnable.run();
});
taskDecoratorPlugin.addDecorator(runnable -> () -> {
taskExecuteCount.incrementAndGet();
runnable.run();
});
// TaskRejectCountRecordPlugin
TaskRejectCountRecordPlugin taskRejectCountRecordPlugin = new TaskRejectCountRecordPlugin();
executor.register(taskRejectCountRecordPlugin);
// TaskRejectCountRecordPlugin
TaskTimeRecordPlugin taskTimeRecordPlugin = new TaskTimeRecordPlugin();
executor.register(taskTimeRecordPlugin);
executor.submit(() -> ThreadUtil.sleep(100L));
executor.submit(() -> ThreadUtil.sleep(300L));
executor.submit(() -> ThreadUtil.sleep(200L));
ThreadUtil.sleep(1000L);
System.out.println(objectMapper.writeValueAsString(executor.getThreadPoolPluginRegistry().getAllPlugins()));
}
}
Loading…
Cancel
Save