mirror of https://github.com/longtai-cn/hippo4j
parent
ded7622277
commit
0a315a0537
@ -1,4 +1,4 @@
|
||||
package io.dynamic.threadpool.starter.config;
|
||||
package io.dynamic.threadpool.common.config;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
@ -1,4 +1,4 @@
|
||||
package io.dynamic.threadpool.starter.config;
|
||||
package io.dynamic.threadpool.common.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
@ -1,4 +1,4 @@
|
||||
package io.dynamict.hreadpool.common.executor;
|
||||
package io.dynamic.threadpool.common.executor;
|
||||
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
@ -1,4 +1,4 @@
|
||||
package io.dynamict.hreadpool.common.model;
|
||||
package io.dynamic.threadpool.common.model;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
@ -0,0 +1,33 @@
|
||||
package io.dynamic.threadpool.common.model;
|
||||
|
||||
/**
|
||||
* Pool Parameter.
|
||||
*
|
||||
* @author chen.ma
|
||||
* @date 2021/6/24 16:04
|
||||
*/
|
||||
public interface PoolParameter {
|
||||
|
||||
String getNamespace();
|
||||
|
||||
String getItemId();
|
||||
|
||||
String getTpId();
|
||||
|
||||
Integer getCoreSize();
|
||||
|
||||
Integer getMaxSize();
|
||||
|
||||
Integer getQueueType();
|
||||
|
||||
Integer getCapacity();
|
||||
|
||||
Integer getKeepAliveTime();
|
||||
|
||||
Integer getIsAlarm();
|
||||
|
||||
Integer getCapacityAlarm();
|
||||
|
||||
Integer getLivenessAlarm();
|
||||
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package io.dynamic.threadpool.common.toolkit;
|
||||
|
||||
import io.dynamic.threadpool.common.model.PoolParameter;
|
||||
|
||||
/**
|
||||
* Content Util.
|
||||
*
|
||||
* @author chen.ma
|
||||
* @date 2021/6/24 16:13
|
||||
*/
|
||||
public class ContentUtil {
|
||||
|
||||
public static String getPoolContent(PoolParameter parameter) {
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
String targetStr = stringBuilder.append(parameter.getCoreSize())
|
||||
.append(parameter.getMaxSize())
|
||||
.append(parameter.getQueueType())
|
||||
.append(parameter.getCapacity())
|
||||
.append(parameter.getKeepAliveTime())
|
||||
.append(parameter.getIsAlarm())
|
||||
.append(parameter.getCapacityAlarm())
|
||||
.append(parameter.getLivenessAlarm())
|
||||
.toString();
|
||||
return targetStr;
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package io.dynamict.hreadpool.common.web.base;
|
||||
package io.dynamic.threadpool.common.web.base;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
@ -1,7 +1,7 @@
|
||||
package io.dynamict.hreadpool.common.web.base;
|
||||
package io.dynamic.threadpool.common.web.base;
|
||||
|
||||
import io.dynamict.hreadpool.common.web.exception.ErrorCode;
|
||||
import io.dynamict.hreadpool.common.web.exception.ServiceException;
|
||||
import io.dynamic.threadpool.common.web.exception.ErrorCode;
|
||||
import io.dynamic.threadpool.common.web.exception.ServiceException;
|
||||
|
||||
/**
|
||||
* Result 工具类
|
@ -1,4 +1,4 @@
|
||||
package io.dynamict.hreadpool.common.web.exception;
|
||||
package io.dynamic.threadpool.common.web.exception;
|
||||
|
||||
/**
|
||||
* 异常码
|
@ -1,4 +1,4 @@
|
||||
package io.dynamict.hreadpool.common.web.exception;
|
||||
package io.dynamic.threadpool.common.web.exception;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
@ -1,3 +1,3 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=io.dynamic.threadpool.starter.config.CommonConfiguration, \
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=io.dynamic.threadpool.common.config.CommonConfiguration, \
|
||||
io.dynamic.threadpool.starter.config.OkHttpClientConfig,\
|
||||
io.dynamic.threadpool.starter.config.DynamicThreadPoolAutoConfiguration
|
@ -0,0 +1,31 @@
|
||||
package io.dynamic.threadpool.server.event;
|
||||
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Config Data Change Event.
|
||||
*
|
||||
* @author chen.ma
|
||||
* @date 2021/6/24 23:35
|
||||
*/
|
||||
public class ConfigDataChangeEvent extends Event {
|
||||
|
||||
public final String namespace;
|
||||
|
||||
public final String itemId;
|
||||
|
||||
public final String tpId;
|
||||
|
||||
public final long lastModifiedTs;
|
||||
|
||||
public ConfigDataChangeEvent(String namespace, String itemId, String tpId, Long gmtModified) {
|
||||
if (StringUtils.isEmpty(namespace) || StringUtils.isEmpty(itemId) || StringUtils.isEmpty(tpId)) {
|
||||
throw new IllegalArgumentException("dataId is null or group is null");
|
||||
}
|
||||
|
||||
this.namespace = namespace;
|
||||
this.itemId = itemId;
|
||||
this.tpId = tpId;
|
||||
this.lastModifiedTs = gmtModified;
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package io.dynamic.threadpool.server.notify;
|
||||
package io.dynamic.threadpool.server.event;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
@ -1,4 +1,4 @@
|
||||
package io.dynamic.threadpool.server.notify;
|
||||
package io.dynamic.threadpool.server.event;
|
||||
|
||||
/**
|
||||
* Slow Event.
|
@ -0,0 +1,35 @@
|
||||
package io.dynamic.threadpool.server.model;
|
||||
|
||||
import io.dynamic.threadpool.server.constant.Constants;
|
||||
import io.dynamic.threadpool.server.toolkit.SimpleReadWriteLock;
|
||||
import io.dynamic.threadpool.server.toolkit.SingletonRepository;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
* Cache Item.
|
||||
*
|
||||
* @author chen.ma
|
||||
* @date 2021/6/24 21:23
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
public class CacheItem {
|
||||
|
||||
final String groupKey;
|
||||
|
||||
public volatile String md5 = Constants.NULL;
|
||||
|
||||
public volatile long lastModifiedTs;
|
||||
|
||||
public SimpleReadWriteLock rwLock = new SimpleReadWriteLock();
|
||||
|
||||
public CacheItem(String groupKey) {
|
||||
this.groupKey = SingletonRepository.DataIdGroupIdCache.getSingleton(groupKey);
|
||||
}
|
||||
|
||||
public CacheItem(String groupKey, String md5) {
|
||||
this.md5 = md5;
|
||||
this.groupKey = SingletonRepository.DataIdGroupIdCache.getSingleton(groupKey);
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package io.dynamic.threadpool.server.service;
|
||||
|
||||
import io.dynamic.threadpool.server.event.ConfigDataChangeEvent;
|
||||
import io.dynamic.threadpool.server.notify.NotifyCenter;
|
||||
|
||||
/**
|
||||
* Config Change Publisher.
|
||||
*
|
||||
* @author chen.ma
|
||||
* @date 2021/6/24 23:34
|
||||
*/
|
||||
public class ConfigChangePublisher {
|
||||
|
||||
/**
|
||||
* Notify ConfigChange.
|
||||
*
|
||||
* @param event ConfigDataChangeEvent instance.
|
||||
*/
|
||||
public static void notifyConfigChange(ConfigDataChangeEvent event) {
|
||||
NotifyCenter.publishEvent(event);
|
||||
}
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
package io.dynamic.threadpool.server.toolkit;
|
||||
|
||||
/**
|
||||
* 简单读写锁.
|
||||
*
|
||||
* @author chen.ma
|
||||
* @date 2021/6/24 21:26
|
||||
*/
|
||||
public class SimpleReadWriteLock {
|
||||
|
||||
/**
|
||||
* Try read lock.
|
||||
*/
|
||||
public synchronized boolean tryReadLock() {
|
||||
if (isWriteLocked()) {
|
||||
return false;
|
||||
} else {
|
||||
status++;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Release the read lock.
|
||||
*/
|
||||
public synchronized void releaseReadLock() {
|
||||
status--;
|
||||
}
|
||||
|
||||
/**
|
||||
* Try write lock.
|
||||
*/
|
||||
public synchronized boolean tryWriteLock() {
|
||||
if (!isFree()) {
|
||||
return false;
|
||||
} else {
|
||||
status = -1;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void releaseWriteLock() {
|
||||
status = 0;
|
||||
}
|
||||
|
||||
private boolean isWriteLocked() {
|
||||
return status < 0;
|
||||
}
|
||||
|
||||
private boolean isFree() {
|
||||
return status == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Zero means no lock; Negative Numbers mean write locks; Positive Numbers mean read locks, and the numeric value
|
||||
* represents the number of read locks.
|
||||
*/
|
||||
private int status = 0;
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
package io.dynamic.threadpool.server.toolkit;
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* Singleton Repository.
|
||||
*
|
||||
* @author chen.ma
|
||||
* @date 2021/6/24 21:28
|
||||
*/
|
||||
public class SingletonRepository<T> {
|
||||
|
||||
public SingletonRepository() {
|
||||
// Initializing size 2^16, the container itself use about 50K of memory, avoiding constant expansion
|
||||
shared = new ConcurrentHashMap(1 << 16);
|
||||
}
|
||||
|
||||
public T getSingleton(T obj) {
|
||||
T previous = shared.putIfAbsent(obj, obj);
|
||||
return (null == previous) ? obj : previous;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return shared.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Be careful use.
|
||||
*/
|
||||
public void remove(Object obj) {
|
||||
shared.remove(obj);
|
||||
}
|
||||
|
||||
private final ConcurrentHashMap<T, T> shared;
|
||||
|
||||
|
||||
/**
|
||||
* Cache of DataId and Group.
|
||||
*/
|
||||
public static class DataIdGroupIdCache {
|
||||
|
||||
public static String getSingleton(String str) {
|
||||
return cache.getSingleton(str);
|
||||
}
|
||||
|
||||
static SingletonRepository<String> cache = new SingletonRepository<String>();
|
||||
}
|
||||
}
|
Loading…
Reference in new issue