| | |
| | | package com.zy.core; |
| | | |
| | | import com.core.common.SpringUtils; |
| | | import com.core.exception.CoolException; |
| | | import com.zy.core.plugin.MainProcessPluginApi; |
| | | import com.zy.core.plugin.api.MainProcessPluginApi; |
| | | import com.zy.core.properties.SystemProperties; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | |
| | | private String mainProcessPlugin; |
| | | private MainProcessPluginApi mainProcessPluginApi; |
| | | // 所属线程 |
| | | private Thread thread; |
| | | private volatile Thread thread; |
| | | |
| | | /** |
| | | * =====>> 开始工作 |
| | | */ |
| | | public void start(){ |
| | | public void start() { |
| | | if (thread != null && thread.isAlive()) { |
| | | log.warn("MainProcess is already running."); |
| | | return; |
| | | } |
| | | |
| | | thread = new Thread(() -> { |
| | | while (!Thread.currentThread().isInterrupted()) { |
| | | try { |
| | | // 1. 加载插件 |
| | | if (mainProcessPluginApi == null) { |
| | | String className = mainProcessPlugin.contains(".") ? mainProcessPlugin : "com.zy.core.plugin." + mainProcessPlugin; |
| | | Class<? extends MainProcessPluginApi> clazz = Class.forName(className).asSubclass(MainProcessPluginApi.class); |
| | | try { |
| | | String className = mainProcessPlugin.contains(".") ? mainProcessPlugin : "com.zy.core.plugin." + mainProcessPlugin; |
| | | Class<? extends MainProcessPluginApi> clazz = Class.forName(className).asSubclass(MainProcessPluginApi.class); |
| | | mainProcessPluginApi = SpringUtils.getBean(clazz); |
| | | } catch (CoolException coolException) { |
| | | continue; |
| | | } catch (Exception e) { |
| | | log.error("Failed to load mainProcessPlugin: {}", mainProcessPlugin, e); |
| | | } |
| | | } |
| | | |
| | | // 系统运行状态判断 |
| | | if (!SystemProperties.WCS_RUNNING_STATUS.get()) { |
| | | // 如果加载失败,等待后重试,防止空指针 |
| | | if (mainProcessPluginApi == null) { |
| | | Thread.sleep(3000); |
| | | continue; |
| | | } |
| | | |
| | | // 2. 系统运行状态判断 |
| | | if (!SystemProperties.WCS_RUNNING_STATUS.get()) { |
| | | // 防止忙等 (Busy-wait optimization) |
| | | Thread.sleep(1000); |
| | | continue; |
| | | } |
| | | |
| | | // 3. 执行主流程 |
| | | mainProcessPluginApi.run(); |
| | | // 间隔 |
| | | |
| | | // 4. 间隔 |
| | | Thread.sleep(200); |
| | | } catch (InterruptedException ie) { |
| | | Thread.currentThread().interrupt(); |
| | | break; |
| | | |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | log.error("Error in MainProcess execution loop", e); |
| | | } |
| | | } |
| | | }); |