This commit is contained in:
Rinjae
2025-09-05 18:57:45 +09:00
commit aacc1a389e
1229 changed files with 167963 additions and 0 deletions
@@ -0,0 +1,9 @@
package com.eactive.eai.common.inflow;
public interface Bucket {
public boolean isAdapterPass(String adapter);
public boolean isInterfacePass(String inter);
public InflowTargetVO getAdapterInflowThreashold(String adapter);
public InflowTargetVO getInterfaceInflowThreashold(String inter);
public InflowTargetVO getInflowThreashold(String inter);
}
@@ -0,0 +1,31 @@
package com.eactive.eai.common.inflow;
import io.github.bucket4j.local.LocalBucket;
public class CustomBucket {
private InflowTargetVO inflowTargetVo;
private LocalBucket localBucket;
public CustomBucket(LocalBucket localBucket, InflowTargetVO inflowTargetVo) {
super();
this.inflowTargetVo = inflowTargetVo;
this.localBucket = localBucket;
}
public InflowTargetVO getInflowTargetVo() {
return inflowTargetVo;
}
public void setInflowTargetVo(InflowTargetVO inflowTargetVo) {
this.inflowTargetVo = inflowTargetVo;
}
public LocalBucket getLocalBucket() {
return localBucket;
}
public void setLocalBucket(LocalBucket localBucket) {
this.localBucket = localBucket;
}
}
@@ -0,0 +1,135 @@
package com.eactive.eai.common.inflow;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.eactive.eai.common.dao.BaseDAO;
import com.eactive.eai.common.dao.DAOException;
import com.eactive.eai.common.exception.ExceptionUtil;
import com.eactive.eai.common.inflow.loader.InflowControlLoader;
import com.eactive.eai.common.inflow.loader.InflowControlLogLogger;
import com.eactive.eai.common.util.Logger;
import com.eactive.eai.data.entity.onl.inflow.InflowControl;
import com.eactive.eai.data.entity.onl.inflow.InflowControlLog;
import com.eactive.eai.data.entity.onl.inflow.InflowControlLogId;
@Service
@Transactional
public class InflowControlDAO extends BaseDAO {
private static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
@Autowired
private InflowControlLoader inflowControlLoader;
@Autowired
private InflowControlLogLogger inflowControlLogLogger;
public List<InflowTargetVO> getInflowAdapterList() throws DAOException {
return getInflowList("01");
}
public List<InflowTargetVO> getInflowInterfaceList() throws DAOException {
return getInflowList("02");
}
public Map<String, InflowTargetVO> getInflowTargetByAdater(String name) throws DAOException {
return getInflowMap("01", name);
}
public Map<String, InflowTargetVO> getInflowTargetByInterface(String name) throws DAOException {
return getInflowMap("02", name);
}
private List<InflowTargetVO> getInflowList(String type) throws DAOException {
try {
Iterable<InflowControl> inflowControls = inflowControlLoader.findByConditions(type, "");
List<InflowTargetVO> targetList = new ArrayList<>();
logger.info("[ @@ Load InflowControl Configuration - starting >>>>>>>>>>>>>>>>> ]");
for (InflowControl inflowControl : inflowControls) {
InflowTargetVO vo = new InflowTargetVO();
vo.setName(inflowControl.getId().getName());
vo.setThreshold(inflowControl.getThreshold());
vo.setThresholdPerSecond(inflowControl.getThresholdpersecond());
vo.setThresholdTimeUnit(inflowControl.getThresholdtimeunit());
vo.setActivate(!"0".equals(inflowControl.getUseyn()));
targetList.add(vo);
}
logger.info("[>>Load InflowControl Configuration - ended ]");
return targetList;
} catch (Exception e) {
throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAICMM101"));
}
}
private Map<String, InflowTargetVO> getInflowMap(String type, String name) throws DAOException {
try {
Iterable<InflowControl> inflowControls = inflowControlLoader.findByConditions(type, name);
Map<String, InflowTargetVO> targetList = new HashMap<>();
logger.info("[ @@ Load InflowControl getInflowMap Configuration - starting >>>>>>>>>>>>>>>>> ]");
for (InflowControl inflowControl : inflowControls) {
InflowTargetVO vo = new InflowTargetVO();
vo.setName(inflowControl.getId().getName());
vo.setThreshold(inflowControl.getThreshold());
vo.setThresholdPerSecond(inflowControl.getThresholdpersecond());
vo.setThresholdTimeUnit(inflowControl.getThresholdtimeunit());
vo.setActivate(!"0".equals(inflowControl.getUseyn()));
targetList.put(vo.getName(), vo);
}
logger.info("[>>Load InflowControl getInflowMap Configuration - ended ]");
return targetList;
} catch (Exception e) {
throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAICMM101"));
}
}
/**
* 1. 기능 : 유량제어 로그를 INSERT하는 메서드 2. 처리 개요 : 파라미터의 코드, 메시지를 테이블에 INSERT한다. - 3.
* 주의사항
*
* @param EAIBzwkDstcd EAI업무구분코드
* @param MsgDpstYMS 메시지수신일시
* @param EAISvcSerno EAI서비스일련번호
* @param EAISevrInstncName EAI서버인스턴스명
* @param EAISvcName EAI서비스명
* @param EAIOsidInstiCd EAI대외기관코드
* @exception DAOException JDBC 관련 오류(SQLException)
**/
// bzwkDstcd + msgDpstYMS + eAISvcSerno
// String bzwkDstcd, String msgDpstYMS, String eAISvcSerno,
// String eAISevrInstncName, String eAISvcName, String adapterGroupName,
// long limitPerSecond, long limit, String timeUnit
public void addInflowServicetLog(String bzwkDstcd, String msgDpstYMS, String eAISvcSerno, String eAISevrInstncName,
String eAISvcName, String adapterGroupName, long limitPerSecond, long limit, String timeUnit)
throws DAOException {
try {
InflowControlLog inflowControlLog = new InflowControlLog();
InflowControlLogId id = new InflowControlLogId();
id.setEaibzwkdstcd(bzwkDstcd);
id.setMsgdpstyms(msgDpstYMS);
id.setEaisvcserno(eAISvcSerno);
inflowControlLog.setId(id);
inflowControlLog.setEaisevrinstncname(eAISevrInstncName);
inflowControlLog.setEaisvcname(eAISvcName);
inflowControlLog.setAdptrbzwkgroupname(adapterGroupName);
inflowControlLog.setThresholdpersecond((int) limitPerSecond);
inflowControlLog.setThreshold((int) limit);
inflowControlLog.setThresholdtimeunit(timeUnit);
inflowControlLogLogger.save(inflowControlLog);
} catch (Exception e) {
throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAICMM101"));
}
}
}
@@ -0,0 +1,357 @@
package com.eactive.eai.common.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 org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.eactive.eai.common.exception.ExceptionUtil;
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.server.EAIServerManager;
import com.eactive.eai.common.util.ApplicationContextProvider;
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;
@Component
public class InflowControlManager implements Lifecycle, Bucket {
static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
public static final String QUOTA_TIMEUNIT_SECOND = "SEC";
public static final String QUOTA_TIMEUNIT_MINUTE = "MIN";
public static final String QUOTA_TIMEUNIT_HOUR = "HOU";
public static final String QUOTA_TIMEUNIT_DAY = "DAY";
public static final String QUOTA_TIMEUNIT_MONTH = "MON";
private Map<String, CustomBucket> adapterBucketList = new HashMap<>();
private Map<String, CustomBucket> interfaceBucketList = new HashMap<>();
private boolean started;
private LifecycleSupport lifecycle = new LifecycleSupport(this);
@Autowired
private InflowControlDAO inflowControlDAO;
public static synchronized InflowControlManager getInstance() {
return ApplicationContextProvider.getContext().getBean(InflowControlManager.class);
}
public static synchronized Bucket getBucket() {
return ApplicationContextProvider.getContext().getBean(InflowControlManager.class);
}
/**
* 유량제어 대상인 거래인지 체크한다.(사이트에 맞게 구성)
*
* @param eaiServerManager
* @param eaiMessage
* @return
*/
public static Boolean isTargetOfInflowControl(EAIServerManager eaiServerManager, EAIMessage eaiMessage) {
InterfaceMapper mapper = eaiMessage.getMapper();
String inExDiv = MessageUtil.getInExDivision(mapper.getInExDivision(eaiMessage.getStandardMessage()),
eaiMessage.getInternalExternalDvcd()); // 1|2
String returnType = mapper.getSendRecvDivision(eaiMessage.getStandardMessage()); // S|R
if (STDMessageKeys.DIRECTION_IN.equals(inExDiv) && STDMessageKeys.SEND_RECV_CD_SEND.equals(returnType)) {
return Boolean.valueOf(true);
}
return Boolean.valueOf(false);
}
public void start() throws LifecycleException {
if (started)
throw new LifecycleException("RECEAICMM201");
lifecycle.fireLifecycleEvent(STARTING_EVENT, this);
try {
init();
} catch (Exception e) {
throw new LifecycleException(ExceptionUtil.getErrorCode(e, "RECEAICMM202"));
}
started = true;
lifecycle.fireLifecycleEvent(STARTED_EVENT, this);
}
private void init() throws Exception {
initAdapter();
initInterface();
}
private void initAdapter() throws Exception {
adapterBucketList = new HashMap<>();
List<InflowTargetVO> inflowAdapterVoList = inflowControlDAO.getInflowAdapterList();
for (InflowTargetVO inflowVo : inflowAdapterVoList) {
if (InflowControlManager.isInflowTarget(inflowVo)) {
CustomBucket bucket = makeBucketUsingInflowVO(inflowVo);
if (bucket != null) {
adapterBucketList.put(inflowVo.getName(), bucket);
}
}
}
}
private void initInterface() throws Exception {
interfaceBucketList = new HashMap<>();
List<InflowTargetVO> inflowInterfaceVoList = inflowControlDAO.getInflowInterfaceList();
for (InflowTargetVO inflowVo : inflowInterfaceVoList) {
if (InflowControlManager.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("InflowControlManager] reload all Started ...");
}
initAdapter();
if (logger.isWarn()) {
logger.warn("InflowControlManager] reload all finished ...");
}
}
public synchronized void reloadInterface() throws Exception {
if (logger.isWarn()) {
logger.warn("InflowControlManager] reload all Started ...");
}
initInterface();
if (logger.isWarn()) {
logger.warn("InflowControlManager] reload all finished ...");
}
}
public synchronized void reloadAdapter(String adapter) throws Exception {
if (logger.isWarn()) {
logger.warn("InflowControlManager] reload Started ...");
}
Map<String, InflowTargetVO> map = inflowControlDAO.getInflowTargetByAdater(adapter);
if (map != null) {
Iterator<String> it = map.keySet().iterator();
String key = "";
while (it.hasNext()) {
key = it.next();
InflowTargetVO inflowTarget = map.get(key);
if (InflowControlManager.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("InflowControlManager] reload finished ...");
}
}
public synchronized void reloadInterface(String inter) throws Exception {
if (logger.isWarn()) {
logger.warn("InflowControlManager] reload Started ...");
}
Map<String, InflowTargetVO> map = inflowControlDAO.getInflowTargetByInterface(inter);
if (map != null) {
Iterator<String> it = map.keySet().iterator();
String key = "";
while (it.hasNext()) {
key = it.next();
InflowTargetVO inflowTarget = map.get(key);
if (InflowControlManager.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("InflowControlManager] reload finished ...");
}
}
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);
}
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<String> it = this.adapterBucketList.keySet().iterator();
String[] svcCd = new String[this.adapterBucketList.size()];
for (int i = 0; it.hasNext(); i++) {
svcCd[i] = it.next();
}
Arrays.sort(svcCd);
return svcCd;
}
public String[] getInterfaceAllKeys() {
Iterator<String> it = this.interfaceBucketList.keySet().iterator();
String[] svcCd = new String[this.interfaceBucketList.size()];
for (int i = 0; it.hasNext(); i++) {
svcCd[i] = 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 (InflowControlManager.isInflowTarget(inflowTarget)) {
LocalBucketBuilder builder = Bucket4j.builder();
if (inflowTarget.getThresholdPerSecond() > 0) {
builder.addLimit(Bandwidth.simple(inflowTarget.getThresholdPerSecond(), Duration.ofSeconds(1)));
}
if (inflowTarget.getThreshold() > 0 && StringUtils.isNotBlank(inflowTarget.getThresholdTimeUnit())) {
builder.addLimit(Bandwidth.simple(inflowTarget.getThreshold(),
InflowControlManager.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) {
return getAdapterInflowThreashold(adapter);
}
public InflowTargetVO getInterfaceInflow(String inter) {
return getInterfaceInflowThreashold(inter);
}
public static boolean isInflowTarget(InflowTargetVO inflowTarget) {
if (inflowTarget == null || !inflowTarget.isActivate()) {
return false;
}
return inflowTarget.getThresholdPerSecond() > 0 || (inflowTarget.getThreshold() > 0 && StringUtils
.equalsAnyIgnoreCase(inflowTarget.getThresholdTimeUnit(), QUOTA_TIMEUNIT_DAY, QUOTA_TIMEUNIT_SECOND,
QUOTA_TIMEUNIT_MINUTE, QUOTA_TIMEUNIT_HOUR, QUOTA_TIMEUNIT_DAY, QUOTA_TIMEUNIT_MONTH));
}
public static Duration getPeriod(String timeUnit) {
if (StringUtils.equalsIgnoreCase(timeUnit, QUOTA_TIMEUNIT_SECOND)) {
return Duration.ofSeconds(1);
} else if (StringUtils.equalsIgnoreCase(timeUnit, QUOTA_TIMEUNIT_MINUTE)) {
return Duration.ofMinutes(1);
} else if (StringUtils.equalsIgnoreCase(timeUnit, QUOTA_TIMEUNIT_HOUR)) {
return Duration.ofHours(1);
} else if (StringUtils.equalsIgnoreCase(timeUnit, QUOTA_TIMEUNIT_DAY)) {
return Duration.ofDays(1);
} else if (StringUtils.equalsIgnoreCase(timeUnit, QUOTA_TIMEUNIT_MONTH)) {
Calendar calendar = Calendar.getInstance();
return Duration.ofDays(calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
} else {
return null;
}
}
}
@@ -0,0 +1,67 @@
package com.eactive.eai.common.inflow;
import java.lang.reflect.Method;
import java.util.Properties;
import org.apache.commons.lang3.StringUtils;
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;
public class InflowControlUtil {
private InflowControlUtil() {
}
static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Bucket getBucket() {
Properties inFlowProp = PropManager.getInstance().getProperties("INFLOW");
String className = inFlowProp.getProperty("inflow.control.bucket.className");
if (StringUtils.isBlank(className)
|| StringUtils.equals(className, "com.eactive.eai.common.inflow.InflowControlManager")) {
return InflowControlManager.getBucket();
} else {
try {
Class clazz = Class.forName(className);
Method method = clazz.getMethod("getBucket", (Class<?>[]) null);
return (Bucket) method.invoke(null, (Object[]) null);
} catch (Exception e) {
logger.error("Method call error class= {}, method= getBucket", className);
logger.error(e.getMessage());
}
}
return null;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static boolean isTargetOfInflowControl(EAIServerManager eaiServerManager, EAIMessage eaiMessage) {
Properties inFlowProp = PropManager.getInstance().getProperties("INFLOW");
String controlInflow = inFlowProp.getProperty("inflow.control.yn", "N");
if (!"Y".equalsIgnoreCase(controlInflow)) {
return false;
}
String className = inFlowProp.getProperty("inflow.control.bucket.className");
if (StringUtils.isBlank(className)
|| StringUtils.equals(className, "com.eactive.eai.common.inflow.InflowControlManager")) {
return InflowControlManager.isTargetOfInflowControl(eaiServerManager, eaiMessage).booleanValue();
} else {
try {
Class clazz = Class.forName(className);
Method method = clazz.getMethod("isTargetOfInflowControl", EAIServerManager.class, EAIMessage.class);
Boolean returnBoolean = (Boolean) method.invoke(null, eaiServerManager, eaiMessage);
return returnBoolean.booleanValue();
} catch (Exception e) {
logger.error("Method call error class= {}, method= isTargetOfInflowControl", className);
logger.error(e.getMessage());
}
}
return false;
}
}
@@ -0,0 +1,68 @@
package com.eactive.eai.common.inflow;
import java.io.Serializable;
@SuppressWarnings("serial")
public class InflowTargetVO implements Serializable {
String name;
long thresholdPerSecond;
long threshold;
String thresholdTimeUnit;
boolean activate;
@Override
public boolean equals(Object obj) {
return name.equals(obj);
}
@Override
public int hashCode() {
return name.hashCode();
}
public boolean isActivate() {
return activate;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getThreshold() {
return threshold;
}
public void setThreshold(long threshold) {
this.threshold = threshold;
}
public void setActivate(boolean activate) {
this.activate = activate;
}
public long getThresholdPerSecond() {
return thresholdPerSecond;
}
public void setThresholdPerSecond(long thresholdPerSecond) {
this.thresholdPerSecond = thresholdPerSecond;
}
public String getThresholdTimeUnit() {
return thresholdTimeUnit;
}
public void setThresholdTimeUnit(String thresholdTimeUnit) {
this.thresholdTimeUnit = thresholdTimeUnit;
}
@Override
public String toString() {
return "InflowTargetVO [name=" + name + ", thresholdPerSecond=" + thresholdPerSecond + ", threshold="
+ threshold + ", thresholdTimeUnit=" + thresholdTimeUnit + ", activate=" + activate + "]";
}
}