불필요한 자원 제거

This commit is contained in:
yunjy-hp
2025-12-11 10:04:41 +09:00
parent 8e7f1cf0e6
commit d6c2ef4cd6
@@ -1,425 +0,0 @@
package com.eactive.eai.custom.inflow;
import java.time.Duration;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.lang3.StringUtils;
import com.eactive.eai.common.dao.DAOFactory;
import com.eactive.eai.common.exception.ExceptionUtil;
import com.eactive.eai.common.inflow.CustomBucket;
import com.eactive.eai.common.inflow.InflowControlDAO;
import com.eactive.eai.common.inflow.InflowControlManager;
import com.eactive.eai.common.inflow.InflowTargetVO;
import com.eactive.eai.common.lifecycle.Lifecycle;
import com.eactive.eai.common.lifecycle.LifecycleException;
import com.eactive.eai.common.lifecycle.LifecycleListener;
import com.eactive.eai.common.lifecycle.LifecycleSupport;
import com.eactive.eai.common.message.EAIMessage;
import com.eactive.eai.common.property.PropManager;
import com.eactive.eai.common.server.EAIServerManager;
import com.eactive.eai.common.util.Logger;
import com.eactive.eai.common.util.MessageUtil;
import com.eactive.eai.message.service.InterfaceMapper;
import com.ext.eai.common.stdmessage.STDMessageKeys;
import io.github.bucket4j.Bandwidth;
import io.github.bucket4j.Bucket4j;
import io.github.bucket4j.local.LocalBucketBuilder;
public class InflowControlManagerSBI implements Lifecycle, com.eactive.eai.common.inflow.Bucket
{
static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
private static InflowControlManagerSBI manager;
private HashMap<String,CustomBucket> adapterBucketList;
private HashMap<String,CustomBucket> interfaceBucketList;
private boolean started;
private LifecycleSupport lifecycle = new LifecycleSupport(this);
/**
* 1. 기능 : Default Constructor
* 2. 처리 개요 : C2RServiceVO Rule 정보를 저장하기 위한 HashMap을 초기화한다.
* 3. 주의사항
*
**/
private InflowControlManagerSBI() {
adapterBucketList = new HashMap<String, CustomBucket>();
interfaceBucketList = new HashMap<String, CustomBucket>();
}
/**
* 1. 기능 : InflowControlManagerSBI Singleton Object를 반환하는 getter method
* 2. 처리 개요 : C2RServiceManager Singleton Object를 반환한다.
* 3. 주의사항
*
* @return InflowControlManagerSBI Singleton object
**/
public static InflowControlManagerSBI getInstance() {
if(manager == null) {
synchronized(InflowControlManagerSBI.class) {
if(manager == null) {
manager = new InflowControlManagerSBI();
}
}
}
return manager;
}
public static com.eactive.eai.common.inflow.Bucket getBucket() {
if(manager == null) {
synchronized(InflowControlManagerSBI.class) {
if(manager == null) {
manager = new InflowControlManagerSBI();
}
}
}
return manager;
}
/**
* 유량제어 대상인 거래인지 체크한다.(사이트에 맞게 구성)
* @param eaiServerManager
* @param eaiMessage
* @return
*/
public static Boolean isTargetOfInflowControl(EAIServerManager eaiServerManager, EAIMessage eaiMessage) {
// 1. 모든 요청거래
InterfaceMapper mapper = eaiMessage.getMapper();
String returnType = mapper.getSendRecvDivision(eaiMessage.getStandardMessage()); // S|R
if (STDMessageKeys.SEND_RECV_CD_SEND.equals(returnType)) {
return new Boolean(true);
}
return new Boolean(false);
}
public void start() throws LifecycleException {
if (started)
throw new LifecycleException("RECEAICMM201");
// Notify our interested LifecycleListeners
lifecycle.fireLifecycleEvent(STARTING_EVENT, this);
// C2RServiceVO Table Initializing ....
try {
init();
} catch(Exception e) {
//throw new LifecycleException("RECEAICMM202");
throw new LifecycleException(ExceptionUtil.getErrorCode(e,"RECEAICMM202"));
}
started = true;
// Notify our interested LifecycleListeners
lifecycle.fireLifecycleEvent(STARTED_EVENT, this);
}
private void init() throws Exception {
initAdapter();
initInterface();
}
private void initAdapter() throws Exception {
// Initializing ....
adapterBucketList = new HashMap<String, CustomBucket>();
InflowControlDAO dao = null;
dao = (InflowControlDAO) DAOFactory.newInstance().create(InflowControlDAO.class);
List<InflowTargetVO> inflowAdapterVoList = dao.getInflowAdapterList();
for (InflowTargetVO inflowVo : inflowAdapterVoList) {
if (InflowControlManagerSBI.isInflowTarget(inflowVo)) {
CustomBucket bucket = makeBucketUsingInflowVO(inflowVo);
if (bucket != null) {
adapterBucketList.put(inflowVo.getName(), bucket);
}
}
}
}
private void initInterface() throws Exception {
// Initializing ....
interfaceBucketList = new HashMap<String, CustomBucket>();
InflowControlDAO dao = null;
dao = (InflowControlDAO) DAOFactory.newInstance().create(InflowControlDAO.class);
List<InflowTargetVO> inflowInterfaceVoList = dao.getInflowInterfaceList();
for (InflowTargetVO inflowVo : inflowInterfaceVoList) {
if (InflowControlManagerSBI.isInflowTarget(inflowVo)) {
CustomBucket bucket = makeBucketUsingInflowVO(inflowVo);
if (bucket != null) {
interfaceBucketList.put(inflowVo.getName(), bucket);
}
}
}
}
public synchronized void reloadAdapter() throws Exception {
if(logger.isWarn()) {
logger.warn("InflowControlManagerSBI] reload all Started ...");
}
initAdapter();
if(logger.isWarn()) {
logger.warn("InflowControlManagerSBI] reload all finished ...");
}
}
public synchronized void reloadInterface() throws Exception {
if(logger.isWarn()) {
logger.warn("InflowControlManagerSBI] reload all Started ...");
}
initInterface();
if(logger.isWarn()) {
logger.warn("InflowControlManagerSBI] reload all finished ...");
}
}
public synchronized void reloadAdapter(String adapter) throws Exception {
if (logger.isWarn()) {
logger.warn("InflowControlManagerSBI] reload Started ...");
}
InflowControlDAO dao = null;
dao = (InflowControlDAO) DAOFactory.newInstance().create(InflowControlDAO.class);
Map<String, InflowTargetVO> map = dao.getInflowTargetByAdater(adapter);
if (map != null) {
Iterator it = map.keySet().iterator();
String key = "";
for (int i = 0; it.hasNext(); i++) {
key = (String) it.next();
InflowTargetVO inflowTarget = map.get(key);
if (InflowControlManagerSBI.isInflowTarget(inflowTarget)) {
CustomBucket bucket = makeBucketUsingInflowVO(inflowTarget);
if (bucket != null) {
adapterBucketList.put(inflowTarget.getName(), bucket);
} else {
adapterBucketList.remove(inflowTarget.getName());
}
} else {
adapterBucketList.remove(inflowTarget.getName());
}
}
}
if (logger.isWarn()) {
logger.warn("InflowControlManagerSBI] reload finished ...");
}
}
public synchronized void reloadInterface(String inter) throws Exception {
if (logger.isWarn()) {
logger.warn("InflowControlManagerSBI] reload Started ...");
}
InflowControlDAO dao = null;
dao = (InflowControlDAO) DAOFactory.newInstance().create(InflowControlDAO.class);
Map<String, InflowTargetVO> map = dao.getInflowTargetByInterface(inter);
if (map != null) {
Iterator it = map.keySet().iterator();
String key = "";
for (int i = 0; it.hasNext(); i++) {
key = (String) it.next();
InflowTargetVO inflowTarget = map.get(key);
if (InflowControlManagerSBI.isInflowTarget(inflowTarget)) {
CustomBucket bucket = makeBucketUsingInflowVO(inflowTarget);
if (bucket != null) {
interfaceBucketList.put(inflowTarget.getName(), bucket);
} else {
interfaceBucketList.remove(inflowTarget.getName());
}
} else {
interfaceBucketList.remove(inflowTarget.getName());
}
}
}
if (logger.isWarn()) {
logger.warn("InflowControlManagerSBI] reload finished ...");
}
}
/**
* 1. 기능 : Lifecycle의 stop 메서드로 C2RServiceManager를 종료하는 메서드
* 2. 처리 개요 : 멤버에 캐싱한 C2RServiceVO Rule 정보를 clear한다.
* 3. 주의사항
*
* @exception LifecycleException 이미 종료된 경우 발생(RECEAICMM203)
**/
public void stop() throws LifecycleException {
// Validate and update our current component state
if (!started)
throw new LifecycleException("RECEAICMM203");
// Notify our interested LifecycleListeners
lifecycle.fireLifecycleEvent(STOPING_EVENT, this);
adapterBucketList.clear();
interfaceBucketList.clear();
started = false;
// Notify our interested LifecycleListeners
lifecycle.fireLifecycleEvent(STOPPED_EVENT, this);
}
/**
* 1. 기능 : LifecycleListener를 등록하는 메서드
* 2. 처리 개요 : LifecycleListener를 등록한다.
* 3. 주의사항
*
* @param listener LifecycleEvent를 수신한 LifecycleListener
**/
public void addLifecycleListener(LifecycleListener listener)
{
lifecycle.addLifecycleListener(listener);
}
public LifecycleListener[] findLifecycleListeners()
{
return lifecycle.findLifecycleListeners();
}
public void removeLifecycleListener(LifecycleListener listener)
{
lifecycle.removeLifecycleListener(listener);
}
public boolean isStarted() {
return this.started;
}
public String[] getAdapterAllKeys() {
Iterator it = this.adapterBucketList.keySet().iterator();
String[] svcCd = new String[this.adapterBucketList.size()];
for(int i = 0; it.hasNext(); i++){
svcCd[i] = (String)it.next();
}
Arrays.sort(svcCd);
return svcCd;
}
public String[] getInterfaceAllKeys() {
Iterator it = this.interfaceBucketList.keySet().iterator();
String[] svcCd = new String[this.interfaceBucketList.size()];
for(int i = 0; it.hasNext(); i++){
svcCd[i] = (String)it.next();
}
Arrays.sort(svcCd);
return svcCd;
}
public void removeAdapter(String adapter) {
adapterBucketList.remove(adapter);
}
public void removeInterface(String inter) {
interfaceBucketList.remove(inter);
}
private CustomBucket makeBucketUsingInflowVO(InflowTargetVO inflowTarget) {
if (InflowControlManagerSBI.isInflowTarget(inflowTarget)) {
LocalBucketBuilder builder = Bucket4j.builder();
if (inflowTarget.getThresholdPerSecond() > 0) { // TPS
builder.addLimit(Bandwidth.simple(inflowTarget.getThresholdPerSecond(), Duration.ofSeconds(1)));
}
if (inflowTarget.getThreshold() > 0 && StringUtils.isNotBlank(inflowTarget.getThresholdTimeUnit())) {
builder.addLimit(Bandwidth.simple(inflowTarget.getThreshold(),
InflowControlManagerSBI.getPeriod(inflowTarget.getThresholdTimeUnit())));
}
return new CustomBucket(builder.build(), inflowTarget);
}
return null;
}
@Override
public boolean isAdapterPass(String adapter) {
CustomBucket b = adapterBucketList.get(adapter);
if ( b == null ) return true;
return b.getLocalBucket().tryConsume(1);
}
@Override
public boolean isInterfacePass(String inter) {
CustomBucket b = interfaceBucketList.get(inter);
if ( b == null ) return true;
return b.getLocalBucket().tryConsume(1);
}
@Override
public InflowTargetVO getAdapterInflowThreashold(String adapter) {
CustomBucket b = adapterBucketList.get(adapter);
if (b == null) return null;
return b.getInflowTargetVo();
}
@Override
public InflowTargetVO getInterfaceInflowThreashold(String inter) {
CustomBucket b = interfaceBucketList.get(inter);
if (b == null) return null;
return b.getInflowTargetVo();
}
@Override
public InflowTargetVO getInflowThreashold(String inter) {
return null;
}
public InflowTargetVO getAdapterInflow(String adapter) {
CustomBucket b = adapterBucketList.get(adapter);
if (b == null) return null;
return b.getInflowTargetVo();
}
public InflowTargetVO getInterfaceInflow(String inter) {
CustomBucket b = interfaceBucketList.get(inter);
if (b == null) return null;
return b.getInflowTargetVo();
}
public static boolean isInflowTarget(InflowTargetVO inflowTarget) {
if (inflowTarget == null || !inflowTarget.isActivate()) {
return false;
}
if (inflowTarget.getThresholdPerSecond() > 0) {
return true;
}
if (inflowTarget.getThreshold() > 0 && StringUtils.equalsAnyIgnoreCase(inflowTarget.getThresholdTimeUnit(),
InflowControlManager.QUOTA_TIMEUNIT_DAY, InflowControlManager.QUOTA_TIMEUNIT_SECOND,
InflowControlManager.QUOTA_TIMEUNIT_MINUTE, InflowControlManager.QUOTA_TIMEUNIT_HOUR,
InflowControlManager.QUOTA_TIMEUNIT_DAY, InflowControlManager.QUOTA_TIMEUNIT_MONTH)) {
return true;
}
return false;
}
public static Duration getPeriod(String timeUnit) {
if (StringUtils.equalsIgnoreCase(timeUnit, InflowControlManager.QUOTA_TIMEUNIT_SECOND)) {
return Duration.ofSeconds(1);
} else if (StringUtils.equalsIgnoreCase(timeUnit, InflowControlManager.QUOTA_TIMEUNIT_MINUTE)) {
return Duration.ofMinutes(1);
} else if (StringUtils.equalsIgnoreCase(timeUnit, InflowControlManager.QUOTA_TIMEUNIT_HOUR)) {
return Duration.ofHours(1);
} else if (StringUtils.equalsIgnoreCase(timeUnit, InflowControlManager.QUOTA_TIMEUNIT_DAY)) {
return Duration.ofDays(1);
} else if (StringUtils.equalsIgnoreCase(timeUnit, InflowControlManager.QUOTA_TIMEUNIT_MONTH)) {
Calendar calendar = Calendar.getInstance();
return Duration.ofDays(calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
} else {
return null;
}
}
}