diff --git a/acs/nladmin-system/src/main/java/org/nl/acs/AcsConfig.java b/acs/nladmin-system/src/main/java/org/nl/acs/AcsConfig.java index 6b05bc5..b80befc 100644 --- a/acs/nladmin-system/src/main/java/org/nl/acs/AcsConfig.java +++ b/acs/nladmin-system/src/main/java/org/nl/acs/AcsConfig.java @@ -50,4 +50,7 @@ public interface AcsConfig { String AutoCleanDays = "AutoCleanDays"; //最大任务下发时间 String MAXSENDTASKTIME = "maxSendTaskTime"; + + //是否模拟在线 + String fork_online = "fork_online"; } diff --git a/acs/nladmin-system/src/main/java/org/nl/acs/auto/run/SystemConfig.java b/acs/nladmin-system/src/main/java/org/nl/acs/auto/run/SystemConfig.java index 93653e3..59ee889 100644 --- a/acs/nladmin-system/src/main/java/org/nl/acs/auto/run/SystemConfig.java +++ b/acs/nladmin-system/src/main/java/org/nl/acs/auto/run/SystemConfig.java @@ -9,4 +9,6 @@ public class SystemConfig { public static Integer sequence_initial_value = Integer.valueOf(1); //@DictionaryItem(description = "主线程是否自动开启选项") public static Boolean thread_auto_run = Boolean.valueOf(true); + // description = "不启用心跳功能" + public static Boolean heartbeat_no_use= Boolean.valueOf(false); } diff --git a/acs/nladmin-system/src/main/java/org/nl/acs/device.xls b/acs/nladmin-system/src/main/java/org/nl/acs/device.xls index 427b90b..7e2a183 100644 Binary files a/acs/nladmin-system/src/main/java/org/nl/acs/device.xls and b/acs/nladmin-system/src/main/java/org/nl/acs/device.xls differ diff --git a/acs/nladmin-system/src/main/java/org/nl/acs/device_driver/HeartbeatableDeviceDriver.java b/acs/nladmin-system/src/main/java/org/nl/acs/device_driver/HeartbeatableDeviceDriver.java new file mode 100644 index 0000000..e223e30 --- /dev/null +++ b/acs/nladmin-system/src/main/java/org/nl/acs/device_driver/HeartbeatableDeviceDriver.java @@ -0,0 +1,10 @@ +package org.nl.acs.device_driver; + +public interface HeartbeatableDeviceDriver extends DeviceDriver { + default void checkHeartbeat() { + } + + default boolean isOnline() { + return false; + } +} diff --git a/acs/nladmin-system/src/main/java/org/nl/acs/device_driver/lnsh/conveyor_press_station/ConveyorPressStationDeviceDriver.java b/acs/nladmin-system/src/main/java/org/nl/acs/device_driver/lnsh/conveyor_press_station/ConveyorPressStationDeviceDriver.java index 107ac95..dcba32a 100644 --- a/acs/nladmin-system/src/main/java/org/nl/acs/device_driver/lnsh/conveyor_press_station/ConveyorPressStationDeviceDriver.java +++ b/acs/nladmin-system/src/main/java/org/nl/acs/device_driver/lnsh/conveyor_press_station/ConveyorPressStationDeviceDriver.java @@ -8,27 +8,32 @@ import lombok.Getter; import lombok.RequiredArgsConstructor; import lombok.Setter; import lombok.extern.slf4j.Slf4j; +import org.nl.acs.AcsConfig; import org.nl.acs.device.device_driver.standard_inspect.ReadUtil; import org.nl.acs.device.service.DeviceService; -import org.nl.acs.device_driver.DeviceDriver; -import org.nl.acs.device_driver.RequestMethodEnum; -import org.nl.acs.device_driver.RouteableDeviceDriver; -import org.nl.acs.device_driver.StandardRequestMethod; +import org.nl.acs.device_driver.*; import org.nl.acs.device_driver.driver.AbstractOpcDeviceDriver; import org.nl.acs.device_driver.driver.ExecutableDeviceDriver; import org.nl.acs.ext.wms.data.AcsToWmsData.applyTask.ApplyTaskRequest; import org.nl.acs.ext.wms.data.AcsToWmsData.applyTask.ApplyTaskResponse; import org.nl.acs.ext.wms.data.Resp; import org.nl.acs.ext.wms.service.AcsToWmsService; +import org.nl.acs.heartbeat.HeartbeatEvent; +import org.nl.acs.heartbeat.dto.HeartbeatManageDto; +import org.nl.acs.heartbeat.enums.HeartbeatType; +import org.nl.acs.heartbeat.service.HeartbeatUnifiedService; import org.nl.acs.instruction.service.InstructionService; import org.nl.acs.instruction.service.dto.Instruction; import org.nl.acs.log.service.DeviceExecuteLogService; import org.nl.acs.monitor.DeviceStageMonitor; import org.nl.acs.opc.Device; +import org.nl.acs.opc.ObjectUtl; import org.nl.acs.route.service.RouteLineService; import org.nl.acs.task.service.TaskService; import org.nl.modules.lucene.service.LuceneExecuteLogService; import org.nl.modules.lucene.service.dto.LuceneLogDto; +import org.nl.modules.system.service.ParamService; +import org.nl.modules.system.service.impl.ParamServiceImpl; import org.nl.modules.wql.util.SpringContextHolder; import org.openscada.opc.lib.da.Server; @@ -45,7 +50,7 @@ import java.util.Map; @Getter @Setter @RequiredArgsConstructor -public class ConveyorPressStationDeviceDriver extends AbstractOpcDeviceDriver implements DeviceDriver, ExecutableDeviceDriver, RouteableDeviceDriver, DeviceStageMonitor, StandardRequestMethod { +public class ConveyorPressStationDeviceDriver extends AbstractOpcDeviceDriver implements DeviceDriver, ExecutableDeviceDriver, RouteableDeviceDriver, DeviceStageMonitor, StandardRequestMethod, HeartbeatableDeviceDriver { protected ItemProtocol itemProtocol = new ItemProtocol(this); LuceneExecuteLogService lucene = SpringContextHolder.getBean("luceneExecuteLogServiceImpl"); @@ -62,6 +67,12 @@ public class ConveyorPressStationDeviceDriver extends AbstractOpcDeviceDriver im AcsToWmsService acsToWmsService = SpringContextHolder.getBean("acsToWmsServiceImpl"); + HeartbeatUnifiedService heartbeatUnifiedService = SpringContextHolder.getBean(HeartbeatUnifiedService.class); + + ParamService paramService = SpringContextHolder.getBean(ParamServiceImpl.class); + + String fork_online = paramService.findByCode(AcsConfig.fork_online).getValue(); + String device_code; int mode = 0; int error = 0; @@ -435,7 +446,7 @@ public class ConveyorPressStationDeviceDriver extends AbstractOpcDeviceDriver im jo.put("weight", weight); jo.put("barcode", barcode); jo.put("isError", iserror); - jo.put("isOnline", isonline); + jo.put("isOnline", isOnline()); jo.put("hasGoods", hasGoods); jo.put("message", message); return jo; @@ -808,4 +819,52 @@ public class ConveyorPressStationDeviceDriver extends AbstractOpcDeviceDriver im return false; } } + + public void checkHeartbeat() { + Integer heartbeat_temp = null; + + try { + heartbeat_temp = this.itemProtocol.getHeartbeat(); + } catch (Exception var3) { + } + + if (!ObjectUtl.isEquals(this.heartbeat_tag, heartbeat_temp)) { + if (heartbeat_temp != null && this.heartbeat_tag != null) { + System.out.println("check heartbeaet!"); + HeartbeatEvent event = new HeartbeatEvent(); + event.setResource_code(this.getDeviceCode()); + event.setResource_name(this.getDevice().getDevice_name()); +// heartbeatUnifiedService.onlineChecker(event); +// DeviceHeartbeatEvent event = new DeviceHeartbeatEvent(); +// event.setResource_code(this.getDeviceCode()); +// if (this.getDevice() != null) { +// event.setResource_name(this.getDevice().getName()); +// } +// ApplicationEventPublisher.trigger(event); + + + } + + this.heartbeat_tag = heartbeat_temp; + } + + } + + public boolean isOnline() { +// long begin = System.currentTimeMillis(); + if (StrUtil.equals(paramService.findByCode(AcsConfig.fork_online).getValue(),"1")) { +// long end = System.currentTimeMillis(); +// long duration = end - begin; +// System.out.println("设备:"+device_code+",耗时:"+ duration); + return true; + } else { + HeartbeatManageDto heartbeat = heartbeatUnifiedService.findByResourceCode(this.getDeviceCode()); +// long end = System.currentTimeMillis(); +// long duration = end - begin; +// System.out.println("设备:"+device_code+",耗时:"+ duration); + return heartbeat != null && ObjectUtl.isEquals(heartbeat.getStatus(), HeartbeatType.online); + } + + + } } diff --git a/acs/nladmin-system/src/main/java/org/nl/acs/heartbeat/DeviceHeartbeatExecuteAutoRun.java b/acs/nladmin-system/src/main/java/org/nl/acs/heartbeat/DeviceHeartbeatExecuteAutoRun.java new file mode 100644 index 0000000..846d986 --- /dev/null +++ b/acs/nladmin-system/src/main/java/org/nl/acs/heartbeat/DeviceHeartbeatExecuteAutoRun.java @@ -0,0 +1,92 @@ +package org.nl.acs.heartbeat; + +import lombok.extern.slf4j.Slf4j; +import org.nl.acs.auto.run.AbstractAutoRunnable; +import org.nl.acs.auto.run.SystemConfig; +import org.nl.acs.device_driver.HeartbeatableDeviceDriver; +import org.nl.acs.opc.*; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.*; +import java.util.concurrent.ExecutorService; + +@Slf4j +@Component +public class DeviceHeartbeatExecuteAutoRun extends AbstractAutoRunnable { + + @Autowired + DeviceAppService deviceAppService; + int initial_thread = 2; + int max_thread = 10; + int cache_thread = 5; + int loop_time_millions = 300; + ExecutorService executorService; + Map runs; + + public DeviceHeartbeatExecuteAutoRun() { + this.executorService = ThreadPoolUtl.newIncreasedBlockedThreadPool(this.initial_thread, this.max_thread, this.cache_thread); + this.runs = new LinkedHashMap(); + this.runs = Collections.synchronizedMap(this.runs); + } + + public String getCode() { + return DeviceHeartbeatExecuteAutoRun.class.getSimpleName(); + } + + public String getName() { + return "设备在线监听"; + } + + public void autoRun() { + while(true) { + ThreadUtl.sleep((long)this.loop_time_millions); + if (SystemConfig.heartbeat_no_use) { + return; + } + if(!DeviceOpcSynchronizeAutoRun.isRun){ + return; + } + + List deviceDrivers = this.deviceAppService.findDeviceDriver(HeartbeatableDeviceDriver.class); + Iterator var2 = deviceDrivers.iterator(); + + while(var2.hasNext()) { + final HeartbeatableDeviceDriver deviceDriver = (HeartbeatableDeviceDriver)var2.next(); + if (deviceDriver.getDevice().getIs_config() != null && deviceDriver.getDevice().getIs_config() && !this.runs.keySet().contains(deviceDriver.getDeviceCode())) { + BlockedRunable runable = new BlockedRunable() { + public void subRun() { + deviceDriver.checkHeartbeat(); + } + + public String getCode() { + return deviceDriver.getDeviceCode(); + } + }; + if (!this.runs.keySet().contains(deviceDriver.getDeviceCode())) { + this.runs.put(deviceDriver.getDeviceCode(), runable); + } + + runable.setIndex(this.runs); + this.executorService.submit(runable); + } + } + } + } + + public void after() { + this.executorService.shutdownNow(); + this.executorService = ThreadPoolUtl.newIncreasedBlockedThreadPool(this.initial_thread, this.max_thread, this.cache_thread); + this.runs = new LinkedHashMap(); + this.runs = Collections.synchronizedMap(this.runs); + } + + public ExecutorService getExecutorService() { + return this.executorService; + } + + public Map getRuns() { + return this.runs; + } + +} diff --git a/acs/nladmin-system/src/main/java/org/nl/acs/heartbeat/HeartbeatConfig.java b/acs/nladmin-system/src/main/java/org/nl/acs/heartbeat/HeartbeatConfig.java new file mode 100644 index 0000000..b9d0016 --- /dev/null +++ b/acs/nladmin-system/src/main/java/org/nl/acs/heartbeat/HeartbeatConfig.java @@ -0,0 +1,10 @@ +package org.nl.acs.heartbeat; + +public class HeartbeatConfig { + + /** + * 最大心跳时常 + */ + public static Integer max_alive_time_millions = 1000 * 30; + +} diff --git a/acs/nladmin-system/src/main/java/org/nl/acs/heartbeat/HeartbeatOfflineCheckerAutoRun.java b/acs/nladmin-system/src/main/java/org/nl/acs/heartbeat/HeartbeatOfflineCheckerAutoRun.java new file mode 100644 index 0000000..0b3fb06 --- /dev/null +++ b/acs/nladmin-system/src/main/java/org/nl/acs/heartbeat/HeartbeatOfflineCheckerAutoRun.java @@ -0,0 +1,56 @@ +package org.nl.acs.heartbeat; + +import lombok.extern.slf4j.Slf4j; +import org.nl.acs.auto.run.AbstractAutoRunnable; +import org.nl.acs.auto.run.AutoRunService; +import org.nl.acs.heartbeat.service.HeartbeatUnifiedService; +import org.nl.acs.heartbeat.service_impl.HeartbeatUnifiedServiceimpl; +import org.nl.acs.opc.ThreadUtl; +import org.nl.modules.wql.util.SpringContextHolder; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.Date; + +@Slf4j +@Component +public class HeartbeatOfflineCheckerAutoRun extends AbstractAutoRunnable { + @Autowired + AutoRunService autoRunService; + + private int recordTimeOut = 10000; + private Date recordTime; + int offline_loop_time_millions = 1000; + boolean heartbeat_no_use =false; + public HeartbeatOfflineCheckerAutoRun() { + this.recordTime = new Date((new Date()).getTime() - (long)this.recordTimeOut); + } + + public String getCode() { + return HeartbeatOfflineCheckerAutoRun.class.getSimpleName(); + } + + public String getName() { + return "在线监听器"; + } + + public void autoRun() { + HeartbeatUnifiedService heartbeatUnifiedService = SpringContextHolder.getBean(HeartbeatUnifiedServiceimpl.class); + + for(; !heartbeat_no_use; ThreadUtl.sleep((long)offline_loop_time_millions)) { + try { + heartbeatUnifiedService.offlineChecker(); + } catch (Exception var3) { + Date date = new Date(); + if (date.getTime() - this.recordTime.getTime() < (long)this.recordTimeOut) { + log.trace("离线事件异常发生时间因为小于{}毫秒,而被无视", this.recordTime); + return; + } + + this.recordTime = date; + log.warn("", var3); + } + } + + } +} diff --git a/acs/nladmin-system/src/main/java/org/nl/acs/heartbeat/dto/HeartbeatManageDto.java b/acs/nladmin-system/src/main/java/org/nl/acs/heartbeat/dto/HeartbeatManageDto.java index bd6e46a..10805fa 100644 --- a/acs/nladmin-system/src/main/java/org/nl/acs/heartbeat/dto/HeartbeatManageDto.java +++ b/acs/nladmin-system/src/main/java/org/nl/acs/heartbeat/dto/HeartbeatManageDto.java @@ -1,19 +1,45 @@ package org.nl.acs.heartbeat.dto; +import lombok.Data; import org.nl.acs.heartbeat.enums.HeartbeatType; import java.util.Date; - -public class HeartbeatManageDto extends Dto { +@Data +public class HeartbeatManageDto { private static final long serialVersionUID = 1L; + /** + * 资源号 + */ private String resource_code; + /** + * 资源名 + */ private String resource_name; + /** + * 状态 在线 离线 + */ private HeartbeatType status; private long duration; + /** + * 在线时间 + */ private Date online_datetime; + /** + * 离线时间 + */ private Date offline_datetime; + /** + * 上次接收时间 + */ private Date lastreceivetime; + private String id; + + private String is_active; + + private String create_by; + + private Date create_time; public HeartbeatManageDto() { } @@ -25,6 +51,14 @@ public class HeartbeatManageDto extends Dto { } + public String getId() { + return this.id; + } + + public String setId(String id) { + return this.id; + } + public String getResource_code() { return this.resource_code; } diff --git a/acs/nladmin-system/src/main/java/org/nl/acs/heartbeat/service_impl/HeartbeatManageServiceimpl.java b/acs/nladmin-system/src/main/java/org/nl/acs/heartbeat/service_impl/HeartbeatManageServiceimpl.java index 7557f97..7fd01e7 100644 --- a/acs/nladmin-system/src/main/java/org/nl/acs/heartbeat/service_impl/HeartbeatManageServiceimpl.java +++ b/acs/nladmin-system/src/main/java/org/nl/acs/heartbeat/service_impl/HeartbeatManageServiceimpl.java @@ -1,12 +1,15 @@ package org.nl.acs.heartbeat.service_impl; +import com.alibaba.fastjson.JSONArray; import org.nl.acs.heartbeat.domain.HeartbeatDomain; import org.nl.acs.heartbeat.dto.HeartbeatManageDto; import org.nl.acs.heartbeat.service.HeartbeatManageService; +import org.nl.modules.wql.core.bean.WQLObject; +import org.springframework.stereotype.Service; import java.util.List; import java.util.UUID; - +@Service public class HeartbeatManageServiceimpl implements HeartbeatManageService { public HeartbeatManageServiceimpl() { @@ -40,7 +43,10 @@ public class HeartbeatManageServiceimpl implements HeartbeatManageService { } public List queryAllActive() { - return null; + WQLObject wo = WQLObject.getWQLObject("acs_heartbeat"); + JSONArray arr = wo.query().getResultJSONArray(0); + List list = arr.toJavaList(HeartbeatManageDto.class); + return list; } public HeartbeatManageDto queryById(UUID id) { diff --git a/acs/nladmin-system/src/main/java/org/nl/acs/heartbeat/service_impl/HeartbeatUnifiedServiceimpl.java b/acs/nladmin-system/src/main/java/org/nl/acs/heartbeat/service_impl/HeartbeatUnifiedServiceimpl.java index 42ec13d..7d9e503 100644 --- a/acs/nladmin-system/src/main/java/org/nl/acs/heartbeat/service_impl/HeartbeatUnifiedServiceimpl.java +++ b/acs/nladmin-system/src/main/java/org/nl/acs/heartbeat/service_impl/HeartbeatUnifiedServiceimpl.java @@ -1,9 +1,11 @@ package org.nl.acs.heartbeat.service_impl; +import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import org.nl.acs.auto.initial.ApplicationAutoInitial; +import org.nl.acs.heartbeat.HeartbeatConfig; import org.nl.acs.heartbeat.HeartbeatEvent; import org.nl.acs.heartbeat.dto.HeartbeatManageDto; import org.nl.acs.heartbeat.enums.HeartbeatType; @@ -12,42 +14,47 @@ import org.nl.acs.heartbeat.service.HeartbeatManageService; import org.nl.acs.heartbeat.service.HeartbeatUnifiedService; import org.nl.modules.wql.util.SpringContextHolder; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.stereotype.Service; import java.util.*; - +@Service public class HeartbeatUnifiedServiceimpl implements HeartbeatUnifiedService, ApplicationAutoInitial { + @Autowired + private ApplicationEventPublisher eventPublisher; + private List heartbeats = new ArrayList(); + @Autowired private HeartbeatManageService heartbeatManageService; - public HeartbeatUnifiedServiceimpl() { - } @Override public void autoInitial() throws Exception { - heartbeatManageService = SpringContextHolder.getBean("heartbeatManageServiceImpl"); - this.reload(); - Iterator iterator = this.heartbeats.iterator(); - - while (iterator.hasNext()) { - HeartbeatManageDto dto = (HeartbeatManageDto) iterator.next(); - if (dto.getOnline_datetime() == null) { - dto.setOnline_datetime(new Date()); - } - - if (dto.getLastreceivetime() == null) { - dto.setLastreceivetime(new Date()); - } - - dto.setOffline_datetime(new Date()); - dto.calculateDuration(); - dto.setStatus(HeartbeatType.offline); - this.heartbeatManageService.saveById(dto); - iterator.remove(); - } +// Iterator iterator = this.heartbeats.iterator(); +// +// while (iterator.hasNext()) { +// HeartbeatManageDto dto = (HeartbeatManageDto) iterator.next(); +// if (dto.getOnline_datetime() == null) { +// dto.setOnline_datetime(new Date()); +// } +// +// if (dto.getLastreceivetime() == null) { +// dto.setLastreceivetime(new Date()); +// } +// +// dto.setOffline_datetime(new Date()); +// dto.calculateDuration(); +// dto.setStatus(HeartbeatType.offline); +// dto.setId(IdUtil.simpleUUID()); +// dto.setIs_active("1"); +// dto.setCreate_by(dto.getResource_code()); +// this.heartbeatManageService.saveById(dto); +// iterator.remove(); +// } } @@ -68,7 +75,11 @@ public class HeartbeatUnifiedServiceimpl implements HeartbeatUnifiedService, App if (StrUtil.equals(dto.getResource_code(), heartbeatEvent.getResource_code())) { dto.setLastreceivetime(new Date()); dto.calculateDuration(); - isonline = true; + if(ObjectUtil.equals(dto.getStatus(),HeartbeatType.online)){ + isonline = true; + } else { + this.heartbeats.remove(dto); + } } } @@ -78,7 +89,13 @@ public class HeartbeatUnifiedServiceimpl implements HeartbeatUnifiedService, App heartbeatManageDto.setResource_name(heartbeatEvent.getResource_name()); heartbeatManageDto.setOnline_datetime(new Date()); heartbeatManageDto.setLastreceivetime(new Date()); + heartbeatManageDto.setCreate_time(new Date()); heartbeatManageDto.setStatus(HeartbeatType.online); + heartbeatManageDto.setId(IdUtil.simpleUUID()); + heartbeatManageDto.setIs_active("1"); + heartbeatManageDto.setCreate_by(heartbeatEvent.getResource_code()); +// this.heartbeatManageService.saveById(heartbeatManageDto); + this.heartbeats.add(heartbeatManageDto); // HeartbeatOnlineEvent heartbeatOnlineEvent = new HeartbeatOnlineEvent(); // heartbeatOnlineEvent.setHeartbeatManageDto(heartbeatManageDto); //ApplicationEventPublisher.trigger(heartbeatOnlineEvent); @@ -113,13 +130,19 @@ public class HeartbeatUnifiedServiceimpl implements HeartbeatUnifiedService, App long time = (new Date()).getTime() - last_time; if (time > (long) 500) { -// if (time > (long)HeartbeatConfig.max_alive_time_millions) { - dto.setOffline_datetime(new Date()); - dto.setStatus(HeartbeatType.offline); - HeartbeatOfflineEvent heartbeatOfflineEvent = new HeartbeatOfflineEvent(); - heartbeatOfflineEvent.setHeartbeatManageDto(dto); - //ApplicationEventPublisher.trigger(heartbeatOfflineEvent); - //log.trace("编号:{},名称:{}, 该资源已离线", dto.getResource_code(), dto.getResource_name()); + if (time > (long) HeartbeatConfig.max_alive_time_millions) { + if(!ObjectUtil.equal(dto.getStatus(),HeartbeatType.offline)){ + this.heartbeats.remove(dto); + dto.setOffline_datetime(new Date()); + dto.setStatus(HeartbeatType.offline); + this.heartbeats.add(dto); + } +// HeartbeatOfflineEvent heartbeatOfflineEvent = new HeartbeatOfflineEvent(); +// heartbeatOfflineEvent.setHeartbeatManageDto(dto); +// eventPublisher.publishEvent(heartbeatOfflineEvent); +// ApplicationEventPublisher.trigger(heartbeatOfflineEvent); + //log.trace("编号:{},名称:{}, 该资源已离线", dto.getResource_code(), dto.getResource_name()); + } } } diff --git a/acs/nladmin-system/src/main/java/org/nl/acs/opc/BlockedThreadPoolPolicy.java b/acs/nladmin-system/src/main/java/org/nl/acs/opc/BlockedThreadPoolPolicy.java new file mode 100644 index 0000000..feeb96b --- /dev/null +++ b/acs/nladmin-system/src/main/java/org/nl/acs/opc/BlockedThreadPoolPolicy.java @@ -0,0 +1,25 @@ +package org.nl.acs.opc; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.concurrent.RejectedExecutionHandler; +import java.util.concurrent.ThreadPoolExecutor; + +public class BlockedThreadPoolPolicy implements RejectedExecutionHandler { + private static final Logger log = LoggerFactory.getLogger(BlockedThreadPoolPolicy.class); + + public BlockedThreadPoolPolicy() { + } + + public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { + if (!e.isShutdown()) { + try { + e.getQueue().put(r); + } catch (InterruptedException var4) { + log.debug("", var4); + } + } + + } +} diff --git a/acs/nladmin-system/src/main/java/org/nl/acs/opc/DeviceManageDto.java b/acs/nladmin-system/src/main/java/org/nl/acs/opc/DeviceManageDto.java index 1647976..bbcd560 100644 --- a/acs/nladmin-system/src/main/java/org/nl/acs/opc/DeviceManageDto.java +++ b/acs/nladmin-system/src/main/java/org/nl/acs/opc/DeviceManageDto.java @@ -43,7 +43,7 @@ public class DeviceManageDto { private String opc_server_code; private String opc_plc_id; private String opc_plc_code; - private Boolean is_configed; + private Boolean is_config; private Boolean is_exist; private Boolean is_entrance; /* 粉桶号 */ diff --git a/acs/nladmin-system/src/main/java/org/nl/acs/opc/IncreasedBlokdedThreadPoolPolicy.java b/acs/nladmin-system/src/main/java/org/nl/acs/opc/IncreasedBlokdedThreadPoolPolicy.java new file mode 100644 index 0000000..04aee5f --- /dev/null +++ b/acs/nladmin-system/src/main/java/org/nl/acs/opc/IncreasedBlokdedThreadPoolPolicy.java @@ -0,0 +1,22 @@ +package org.nl.acs.opc; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class IncreasedBlokdedThreadPoolPolicy implements IncreasedRejectedExecutionHandler { + private static final Logger log = LoggerFactory.getLogger(IncreasedBlokdedThreadPoolPolicy.class); + + public IncreasedBlokdedThreadPoolPolicy() { + } + + public void rejectedExecution(Runnable r, IncreasedThreadPoolExecutor e) { + if (!e.isShutdown()) { + try { + e.getQueue().put(r); + } catch (InterruptedException var4) { + log.debug("", var4); + } + } + + } +} diff --git a/acs/nladmin-system/src/main/java/org/nl/acs/opc/IncreasedRejectedExecutionHandler.java b/acs/nladmin-system/src/main/java/org/nl/acs/opc/IncreasedRejectedExecutionHandler.java new file mode 100644 index 0000000..0dcee2c --- /dev/null +++ b/acs/nladmin-system/src/main/java/org/nl/acs/opc/IncreasedRejectedExecutionHandler.java @@ -0,0 +1,5 @@ +package org.nl.acs.opc; + +public interface IncreasedRejectedExecutionHandler { + void rejectedExecution(Runnable var1, IncreasedThreadPoolExecutor var2); +} diff --git a/acs/nladmin-system/src/main/java/org/nl/acs/opc/IncreasedThreadPoolExecutor.java b/acs/nladmin-system/src/main/java/org/nl/acs/opc/IncreasedThreadPoolExecutor.java new file mode 100644 index 0000000..5124e98 --- /dev/null +++ b/acs/nladmin-system/src/main/java/org/nl/acs/opc/IncreasedThreadPoolExecutor.java @@ -0,0 +1,943 @@ +package org.nl.acs.opc; + +import java.util.*; +import java.util.concurrent.*; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.locks.AbstractQueuedSynchronizer; +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.ReentrantLock; + +public class IncreasedThreadPoolExecutor extends AbstractExecutorService { + private final AtomicInteger ctl; + private static final int COUNT_BITS = 29; + private static final int CAPACITY = 536870911; + private static final int RUNNING = -536870912; + private static final int SHUTDOWN = 0; + private static final int STOP = 536870912; + private static final int TIDYING = 1073741824; + private static final int TERMINATED = 1610612736; + private final BlockingQueue workQueue; + private final ReentrantLock mainLock; + private final HashSet workers; + private final Condition termination; + private int largestPoolSize; + private long completedTaskCount; + private volatile ThreadFactory threadFactory; + private volatile IncreasedRejectedExecutionHandler handler; + private volatile long keepAliveTime; + private volatile boolean allowCoreThreadTimeOut; + private volatile int corePoolSize; + private volatile int maximumPoolSize; + private volatile int multiple; + private static final IncreasedRejectedExecutionHandler defaultHandler = new AbortPolicy(); + private static final RuntimePermission shutdownPerm = new RuntimePermission("modifyThread"); + private static final boolean ONLY_ONE = true; + + private static int runStateOf(int c) { + return c & -536870912; + } + + private static int workerCountOf(int c) { + return c & 536870911; + } + + private static int ctlOf(int rs, int wc) { + return rs | wc; + } + + private static boolean runStateLessThan(int c, int s) { + return c < s; + } + + private static boolean runStateAtLeast(int c, int s) { + return c >= s; + } + + private static boolean isRunning(int c) { + return c < 0; + } + + private boolean compareAndIncrementWorkerCount(int expect) { + return this.ctl.compareAndSet(expect, expect + 1); + } + + private boolean compareAndDecrementWorkerCount(int expect) { + return this.ctl.compareAndSet(expect, expect - 1); + } + + private void decrementWorkerCount() { + while(!this.compareAndDecrementWorkerCount(this.ctl.get())) { + } + + } + + private void advanceRunState(int targetState) { + int c; + do { + c = this.ctl.get(); + } while(!runStateAtLeast(c, targetState) && !this.ctl.compareAndSet(c, ctlOf(targetState, workerCountOf(c)))); + + } + + final void tryTerminate() { + while(true) { + int c = this.ctl.get(); + if (isRunning(c) || runStateAtLeast(c, 1073741824) || runStateOf(c) == 0 && !this.workQueue.isEmpty()) { + return; + } + + if (workerCountOf(c) != 0) { + this.interruptIdleWorkers(true); + return; + } + + ReentrantLock mainLock = this.mainLock; + mainLock.lock(); + + try { + if (!this.ctl.compareAndSet(c, ctlOf(1073741824, 0))) { + continue; + } + + try { + this.terminated(); + } finally { + this.ctl.set(ctlOf(1610612736, 0)); + this.termination.signalAll(); + } + } finally { + mainLock.unlock(); + } + + return; + } + } + + private void checkShutdownAccess() { + SecurityManager security = System.getSecurityManager(); + if (security != null) { + security.checkPermission(shutdownPerm); + ReentrantLock mainLock = this.mainLock; + mainLock.lock(); + + try { + Iterator var3 = this.workers.iterator(); + + while(var3.hasNext()) { + Worker w = (Worker)var3.next(); + security.checkAccess(w.thread); + } + } finally { + mainLock.unlock(); + } + } + + } + + private void interruptWorkers() { + ReentrantLock mainLock = this.mainLock; + mainLock.lock(); + + try { + Iterator var2 = this.workers.iterator(); + + while(var2.hasNext()) { + Worker w = (Worker)var2.next(); + w.interruptIfStarted(); + } + } finally { + mainLock.unlock(); + } + + } + + private void interruptIdleWorkers(boolean onlyOne) { + ReentrantLock mainLock = this.mainLock; + mainLock.lock(); + + try { + Iterator var3 = this.workers.iterator(); + + while(var3.hasNext()) { + Worker w = (Worker)var3.next(); + Thread t = w.thread; + if (!t.isInterrupted() && w.tryLock()) { + try { + t.interrupt(); + } catch (SecurityException var15) { + } finally { + w.unlock(); + } + } + + if (onlyOne) { + break; + } + } + } finally { + mainLock.unlock(); + } + + } + + private void interruptIdleWorkers() { + this.interruptIdleWorkers(false); + } + + final void reject(Runnable command) { + this.handler.rejectedExecution(command, this); + } + + void onShutdown() { + } + + final boolean isRunningOrShutdown(boolean shutdownOK) { + int rs = runStateOf(this.ctl.get()); + return rs == -536870912 || rs == 0 && shutdownOK; + } + + private List drainQueue() { + BlockingQueue q = this.workQueue; + ArrayList taskList = new ArrayList(); + q.drainTo(taskList); + if (!q.isEmpty()) { + Runnable[] var3 = (Runnable[])q.toArray(new Runnable[0]); + int var4 = var3.length; + + for(int var5 = 0; var5 < var4; ++var5) { + Runnable r = var3[var5]; + if (q.remove(r)) { + taskList.add(r); + } + } + } + + return taskList; + } + + private boolean addWorker(Runnable firstTask, boolean core) { + while(true) { + int c = this.ctl.get(); + int rs = runStateOf(c); + if (rs >= 0 && (rs != 0 || firstTask != null || this.workQueue.isEmpty())) { + return false; + } + + while(true) { + int wc = workerCountOf(c); + if (wc >= 536870911 || wc >= (core ? this.corePoolSize : this.maximumPoolSize)) { + return false; + } + + if (this.compareAndIncrementWorkerCount(c)) { + boolean workerStarted = false; + boolean workerAdded = false; + Worker w = null; + + try { + w = new Worker(firstTask); + Thread t = w.thread; + if (t != null) { + ReentrantLock mainLock = this.mainLock; + mainLock.lock(); + + try { + int rs1 = runStateOf(this.ctl.get()); + if (rs1 < 0 || rs1 == 0 && firstTask == null) { + if (t.isAlive()) { + throw new IllegalThreadStateException(); + } + + this.workers.add(w); + int s = this.workers.size(); + if (s > this.largestPoolSize) { + this.largestPoolSize = s; + } + + workerAdded = true; + } + } finally { + mainLock.unlock(); + } + + if (workerAdded) { + t.start(); + workerStarted = true; + } + } + } finally { + if (!workerStarted) { + this.addWorkerFailed(w); + } + + } + + return workerStarted; + } + + c = this.ctl.get(); + if (runStateOf(c) != rs) { + break; + } + } + } + } + + private void addWorkerFailed(Worker w) { + ReentrantLock mainLock = this.mainLock; + mainLock.lock(); + + try { + if (w != null) { + this.workers.remove(w); + } + + this.decrementWorkerCount(); + this.tryTerminate(); + } finally { + mainLock.unlock(); + } + + } + + private void processWorkerExit(Worker w, boolean completedAbruptly) { + if (completedAbruptly) { + this.decrementWorkerCount(); + } + + ReentrantLock mainLock = this.mainLock; + mainLock.lock(); + + try { + this.completedTaskCount += w.completedTasks; + this.workers.remove(w); + } finally { + mainLock.unlock(); + } + + this.tryTerminate(); + int c = this.ctl.get(); + if (runStateLessThan(c, 536870912)) { + if (!completedAbruptly) { + int min = this.allowCoreThreadTimeOut ? 0 : this.corePoolSize; + if (min == 0 && !this.workQueue.isEmpty()) { + min = 1; + } + + if (workerCountOf(c) >= min) { + return; + } + } + + this.addWorker((Runnable)null, false); + } + + } + + private Runnable getTask() { + boolean timedOut = false; + + while(true) { + int c = this.ctl.get(); + int rs = runStateOf(c); + if (rs >= 0 && (rs >= 536870912 || this.workQueue.isEmpty())) { + this.decrementWorkerCount(); + return null; + } + + int wc = workerCountOf(c); + boolean timed = this.allowCoreThreadTimeOut || wc > this.corePoolSize; + if (wc <= this.maximumPoolSize && (!timed || !timedOut) || wc <= 1 && !this.workQueue.isEmpty()) { + try { + Runnable r = timed ? (Runnable)this.workQueue.poll(this.keepAliveTime, TimeUnit.NANOSECONDS) : (Runnable)this.workQueue.take(); + if (r != null) { + return r; + } + + timedOut = true; + } catch (InterruptedException var7) { + timedOut = false; + } + } else if (this.compareAndDecrementWorkerCount(c)) { + return null; + } + } + } + + final void runWorker(Worker w) { + Thread wt = Thread.currentThread(); + Runnable task = w.firstTask; + w.firstTask = null; + w.unlock(); + boolean completedAbruptly = true; + + try { + while(task != null || (task = this.getTask()) != null) { + w.lock(); + if ((runStateAtLeast(this.ctl.get(), 536870912) || Thread.interrupted() && runStateAtLeast(this.ctl.get(), 536870912)) && !wt.isInterrupted()) { + wt.interrupt(); + } + + try { + this.beforeExecute(wt, task); + Throwable thrown = null; + + try { + task.run(); + } catch (RuntimeException var28) { + thrown = var28; + throw var28; + } catch (Error var29) { + thrown = var29; + throw var29; + } catch (Throwable var30) { + thrown = var30; + throw new Error(var30); + } finally { + this.afterExecute(task, (Throwable)thrown); + } + } finally { + task = null; + ++w.completedTasks; + w.unlock(); + } + } + + completedAbruptly = false; + } finally { + this.processWorkerExit(w, completedAbruptly); + } + + } + + public IncreasedThreadPoolExecutor(int corePoolSize, int maximumPoolSize, int multiple, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue) { + this(corePoolSize, maximumPoolSize, multiple, keepAliveTime, unit, workQueue, Executors.defaultThreadFactory(), defaultHandler); + } + + public IncreasedThreadPoolExecutor(int corePoolSize, int maximumPoolSize, int multiple, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, ThreadFactory threadFactory) { + this(corePoolSize, maximumPoolSize, multiple, keepAliveTime, unit, workQueue, threadFactory, defaultHandler); + } + + public IncreasedThreadPoolExecutor(int corePoolSize, int maximumPoolSize, int multiple, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, IncreasedRejectedExecutionHandler handler) { + this(corePoolSize, maximumPoolSize, multiple, keepAliveTime, unit, workQueue, Executors.defaultThreadFactory(), handler); + } + + public IncreasedThreadPoolExecutor(int corePoolSize, int maximumPoolSize, int multiple, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, ThreadFactory threadFactory, IncreasedRejectedExecutionHandler handler) { + this.ctl = new AtomicInteger(ctlOf(-536870912, 0)); + this.mainLock = new ReentrantLock(); + this.workers = new HashSet(); + this.termination = this.mainLock.newCondition(); + if (corePoolSize >= 0 && maximumPoolSize > 0 && maximumPoolSize >= corePoolSize && keepAliveTime >= 0L) { + if (workQueue != null && threadFactory != null && handler != null) { + this.corePoolSize = corePoolSize; + this.maximumPoolSize = maximumPoolSize; + this.workQueue = workQueue; + this.keepAliveTime = unit.toNanos(keepAliveTime); + this.threadFactory = threadFactory; + this.handler = handler; + this.multiple = multiple; + } else { + throw new NullPointerException(); + } + } else { + throw new IllegalArgumentException(); + } + } + + public void execute(Runnable command) { + if (command == null) { + throw new NullPointerException(); + } else { + int c = this.ctl.get(); + if (workerCountOf(c) < this.corePoolSize) { + if (this.addWorker(command, true)) { + return; + } + + c = this.ctl.get(); + } + + if (isRunning(c) && this.workQueue.size() < workerCountOf(c) * this.multiple && this.workQueue.offer(command)) { + int recheck = this.ctl.get(); + if (!isRunning(recheck) && this.remove(command)) { + this.reject(command); + } else if (workerCountOf(recheck) == 0) { + this.addWorker((Runnable)null, false); + } + } else if (!this.addWorker(command, false)) { + this.reject(command); + } + + } + } + + public void shutdown() { + ReentrantLock mainLock = this.mainLock; + mainLock.lock(); + + try { + this.checkShutdownAccess(); + this.advanceRunState(0); + this.interruptIdleWorkers(); + this.onShutdown(); + } finally { + mainLock.unlock(); + } + + this.tryTerminate(); + } + + public List shutdownNow() { + ReentrantLock mainLock = this.mainLock; + mainLock.lock(); + + List tasks; + try { + this.checkShutdownAccess(); + this.advanceRunState(536870912); + this.interruptWorkers(); + tasks = this.drainQueue(); + } finally { + mainLock.unlock(); + } + + this.tryTerminate(); + return tasks; + } + + public boolean isShutdown() { + return !isRunning(this.ctl.get()); + } + + public boolean isTerminating() { + int c = this.ctl.get(); + return !isRunning(c) && runStateLessThan(c, 1610612736); + } + + public boolean isTerminated() { + return runStateAtLeast(this.ctl.get(), 1610612736); + } + + public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException { + long nanos = unit.toNanos(timeout); + ReentrantLock mainLock = this.mainLock; + mainLock.lock(); + + boolean var7; + try { + while(!runStateAtLeast(this.ctl.get(), 1610612736)) { + if (nanos <= 0L) { + var7 = false; + return var7; + } + + nanos = this.termination.awaitNanos(nanos); + } + + var7 = true; + } finally { + mainLock.unlock(); + } + + return var7; + } + + protected void finalize() { + this.shutdown(); + } + + public void setThreadFactory(ThreadFactory threadFactory) { + if (threadFactory == null) { + throw new NullPointerException(); + } else { + this.threadFactory = threadFactory; + } + } + + public ThreadFactory getThreadFactory() { + return this.threadFactory; + } + + public void setRejectedExecutionHandler(IncreasedRejectedExecutionHandler handler) { + if (handler == null) { + throw new NullPointerException(); + } else { + this.handler = handler; + } + } + + public IncreasedRejectedExecutionHandler getRejectedExecutionHandler() { + return this.handler; + } + + public void setCorePoolSize(int corePoolSize) { + if (corePoolSize < 0) { + throw new IllegalArgumentException(); + } else { + int delta = corePoolSize - this.corePoolSize; + this.corePoolSize = corePoolSize; + if (workerCountOf(this.ctl.get()) > corePoolSize) { + this.interruptIdleWorkers(); + } else if (delta > 0) { + int k = Math.min(delta, this.workQueue.size()); + + while(k-- > 0 && this.addWorker((Runnable)null, true) && !this.workQueue.isEmpty()) { + } + } + + } + } + + public int getCorePoolSize() { + return this.corePoolSize; + } + + public boolean prestartCoreThread() { + return workerCountOf(this.ctl.get()) < this.corePoolSize && this.addWorker((Runnable)null, true); + } + + void ensurePrestart() { + int wc = workerCountOf(this.ctl.get()); + if (wc < this.corePoolSize) { + this.addWorker((Runnable)null, true); + } else if (wc == 0) { + this.addWorker((Runnable)null, false); + } + + } + + public int prestartAllCoreThreads() { + int n; + for(n = 0; this.addWorker((Runnable)null, true); ++n) { + } + + return n; + } + + public boolean allowsCoreThreadTimeOut() { + return this.allowCoreThreadTimeOut; + } + + public void allowCoreThreadTimeOut(boolean value) { + if (value && this.keepAliveTime <= 0L) { + throw new IllegalArgumentException("Core threads must have nonzero keep alive times"); + } else { + if (value != this.allowCoreThreadTimeOut) { + this.allowCoreThreadTimeOut = value; + if (value) { + this.interruptIdleWorkers(); + } + } + + } + } + + public void setMaximumPoolSize(int maximumPoolSize) { + if (maximumPoolSize > 0 && maximumPoolSize >= this.corePoolSize) { + this.maximumPoolSize = maximumPoolSize; + if (workerCountOf(this.ctl.get()) > maximumPoolSize) { + this.interruptIdleWorkers(); + } + + } else { + throw new IllegalArgumentException(); + } + } + + public int getMaximumPoolSize() { + return this.maximumPoolSize; + } + + public void setKeepAliveTime(long time, TimeUnit unit) { + if (time < 0L) { + throw new IllegalArgumentException(); + } else if (time == 0L && this.allowsCoreThreadTimeOut()) { + throw new IllegalArgumentException("Core threads must have nonzero keep alive times"); + } else { + long keepAliveTime = unit.toNanos(time); + long delta = keepAliveTime - this.keepAliveTime; + this.keepAliveTime = keepAliveTime; + if (delta < 0L) { + this.interruptIdleWorkers(); + } + + } + } + + public long getKeepAliveTime(TimeUnit unit) { + return unit.convert(this.keepAliveTime, TimeUnit.NANOSECONDS); + } + + public BlockingQueue getQueue() { + return this.workQueue; + } + + public boolean remove(Runnable task) { + boolean removed = this.workQueue.remove(task); + this.tryTerminate(); + return removed; + } + + public void purge() { + BlockingQueue q = this.workQueue; + + try { + Iterator it = q.iterator(); + + while(it.hasNext()) { + Runnable r = (Runnable)it.next(); + if (r instanceof Future && ((Future)r).isCancelled()) { + it.remove(); + } + } + } catch (ConcurrentModificationException var7) { + Object[] var3 = q.toArray(); + int var4 = var3.length; + + for(int var5 = 0; var5 < var4; ++var5) { + Object r = var3[var5]; + if (r instanceof Future && ((Future)r).isCancelled()) { + q.remove(r); + } + } + } + + this.tryTerminate(); + } + + public int getPoolSize() { + ReentrantLock mainLock = this.mainLock; + mainLock.lock(); + + int var2; + try { + var2 = runStateAtLeast(this.ctl.get(), 1073741824) ? 0 : this.workers.size(); + } finally { + mainLock.unlock(); + } + + return var2; + } + + public int getActiveCount() { + ReentrantLock mainLock = this.mainLock; + mainLock.lock(); + + try { + int n = 0; + Iterator var3 = this.workers.iterator(); + + while(var3.hasNext()) { + Worker w = (Worker)var3.next(); + if (w.isLocked()) { + ++n; + } + } + + int var8 = n; + return var8; + } finally { + mainLock.unlock(); + } + } + + public int getLargestPoolSize() { + ReentrantLock mainLock = this.mainLock; + mainLock.lock(); + + int var2; + try { + var2 = this.largestPoolSize; + } finally { + mainLock.unlock(); + } + + return var2; + } + + public long getTaskCount() { + ReentrantLock mainLock = this.mainLock; + mainLock.lock(); + + try { + long n = this.completedTaskCount; + Iterator var4 = this.workers.iterator(); + + while(var4.hasNext()) { + Worker w = (Worker)var4.next(); + n += w.completedTasks; + if (w.isLocked()) { + ++n; + } + } + + long var9 = n + (long)this.workQueue.size(); + return var9; + } finally { + mainLock.unlock(); + } + } + + public long getCompletedTaskCount() { + ReentrantLock mainLock = this.mainLock; + mainLock.lock(); + + try { + long n = this.completedTaskCount; + + Worker w; + for(Iterator var4 = this.workers.iterator(); var4.hasNext(); n += w.completedTasks) { + w = (Worker)var4.next(); + } + + long var9 = n; + return var9; + } finally { + mainLock.unlock(); + } + } + + public String toString() { + ReentrantLock mainLock = this.mainLock; + mainLock.lock(); + + long ncompleted; + int nworkers; + int nactive; + try { + ncompleted = this.completedTaskCount; + nactive = 0; + nworkers = this.workers.size(); + Iterator var6 = this.workers.iterator(); + + while(var6.hasNext()) { + Worker w = (Worker)var6.next(); + ncompleted += w.completedTasks; + if (w.isLocked()) { + ++nactive; + } + } + } finally { + mainLock.unlock(); + } + + int c = this.ctl.get(); + String rs = runStateLessThan(c, 0) ? "Running" : (runStateAtLeast(c, 1610612736) ? "Terminated" : "Shutting down"); + return super.toString() + "[" + rs + ", pool size = " + nworkers + ", active threads = " + nactive + ", queued tasks = " + this.workQueue.size() + ", completed tasks = " + ncompleted + "]"; + } + + protected void beforeExecute(Thread t, Runnable r) { + } + + protected void afterExecute(Runnable r, Throwable t) { + } + + protected void terminated() { + } + + public static class DiscardOldestPolicy implements IncreasedRejectedExecutionHandler { + public DiscardOldestPolicy() { + } + + public void rejectedExecution(Runnable r, IncreasedThreadPoolExecutor e) { + if (!e.isShutdown()) { + e.getQueue().poll(); + e.execute(r); + } + + } + } + + public static class DiscardPolicy implements IncreasedRejectedExecutionHandler { + public DiscardPolicy() { + } + + public void rejectedExecution(Runnable r, IncreasedThreadPoolExecutor e) { + } + } + + public static class AbortPolicy implements IncreasedRejectedExecutionHandler { + public AbortPolicy() { + } + + public void rejectedExecution(Runnable r, IncreasedThreadPoolExecutor e) { + throw new RejectedExecutionException("Task " + r.toString() + " rejected from " + e.toString()); + } + } + + public static class CallerRunsPolicy implements IncreasedRejectedExecutionHandler { + public CallerRunsPolicy() { + } + + public void rejectedExecution(Runnable r, IncreasedThreadPoolExecutor e) { + if (!e.isShutdown()) { + r.run(); + } + + } + } + + private final class Worker extends AbstractQueuedSynchronizer implements Runnable { + private static final long serialVersionUID = 6138294804551838833L; + final Thread thread; + Runnable firstTask; + volatile long completedTasks; + + Worker(Runnable firstTask) { + this.setState(-1); + this.firstTask = firstTask; + this.thread = IncreasedThreadPoolExecutor.this.getThreadFactory().newThread(this); + } + + public void run() { + IncreasedThreadPoolExecutor.this.runWorker(this); + } + + protected boolean isHeldExclusively() { + return this.getState() != 0; + } + + protected boolean tryAcquire(int unused) { + if (this.compareAndSetState(0, 1)) { + this.setExclusiveOwnerThread(Thread.currentThread()); + return true; + } else { + return false; + } + } + + protected boolean tryRelease(int unused) { + this.setExclusiveOwnerThread((Thread)null); + this.setState(0); + return true; + } + + public void lock() { + this.acquire(1); + } + + public boolean tryLock() { + return this.tryAcquire(1); + } + + public void unlock() { + this.release(1); + } + + public boolean isLocked() { + return this.isHeldExclusively(); + } + + void interruptIfStarted() { + Thread t; + if (this.getState() >= 0 && (t = this.thread) != null && !t.isInterrupted()) { + try { + t.interrupt(); + } catch (SecurityException var3) { + } + } + + } + } +} + diff --git a/acs/nladmin-system/src/main/java/org/nl/acs/opc/ThreadPoolUtl.java b/acs/nladmin-system/src/main/java/org/nl/acs/opc/ThreadPoolUtl.java new file mode 100644 index 0000000..b6d7f08 --- /dev/null +++ b/acs/nladmin-system/src/main/java/org/nl/acs/opc/ThreadPoolUtl.java @@ -0,0 +1,71 @@ +package org.nl.acs.opc; + +import java.util.concurrent.*; + +public class ThreadPoolUtl { + private static ExecutorService eventExecutorService = null; + private static ExecutorService cycleExecutorService = null; + + private ThreadPoolUtl() { + } + + public static ExecutorService getEventSingleInstanse() { + if (eventExecutorService == null) { + Class var0 = ThreadPoolUtl.class; + synchronized(ThreadPoolUtl.class) { + if (eventExecutorService == null) { + eventExecutorService = newIncreasedBlockedThreadPool(50, 200, 5); + } + } + } + + return eventExecutorService; + } + + public static ExecutorService getCycleSingleInstanse() { + if (cycleExecutorService == null) { + Class var0 = ThreadPoolUtl.class; + synchronized(ThreadPoolUtl.class) { + if (cycleExecutorService == null) { + cycleExecutorService = newIncreasedBlockedThreadPool(5, 200, 5); + } + } + } + + return cycleExecutorService; + } + + public static ExecutorService newIncreasedThreadPool() { + return newIncreasedThreadPool(50, 200, 5); + } + + public static ExecutorService newIncreasedBlockedThreadPool() { + return newIncreasedBlockedThreadPool(50, 200, 5); + } + + public static ExecutorService newIncreasedThreadPool(int corePoolSize, int maximumPoolSize, int multiple) { + return new IncreasedThreadPoolExecutor(corePoolSize, maximumPoolSize, multiple, 1L, TimeUnit.HOURS, new LinkedBlockingDeque(), new IncreasedBlokdedThreadPoolPolicy()); + } + + public static ExecutorService newIncreasedBlockedThreadPool(int corePoolSize, int maximumPoolSize, int multiple) { + int queueLength = maximumPoolSize * multiple; + return new IncreasedThreadPoolExecutor(corePoolSize, maximumPoolSize, multiple, 1L, TimeUnit.HOURS, new ArrayBlockingQueue(queueLength), new IncreasedBlokdedThreadPoolPolicy()); + } + + public static ExecutorService newBlockedThreadPool() { + return newBlockedThreadPool(50, 300, 200); + } + + public static ExecutorService newBlockedThreadPool(int coreNum, int maxNum, int cacheNum) { + return new ThreadPoolExecutor(coreNum, maxNum, 1L, TimeUnit.HOURS, new ArrayBlockingQueue(cacheNum), new BlockedThreadPoolPolicy()); + } + + public static ExecutorService newCachedThreadPool() { + return Executors.newCachedThreadPool(); + } + + public static ExecutorService newFixedThreadPool(int maxNum) { + return Executors.newFixedThreadPool(maxNum); + } + +}