From e695b1cbd45714a6f96ccf159c68fcf825fe7aa2 Mon Sep 17 00:00:00 2001 From: kamiljarmusik Date: Tue, 28 Mar 2023 19:53:26 +0200 Subject: [PATCH] #2487 Fixed problem an HTTP request processing performance --- WebContent/WEB-INF/web.xml | 3 ++ .../serotonin/mango/MangoContextListener.java | 4 +++ .../serotonin/mango/db/BasePooledAccess.java | 2 +- src/com/serotonin/mango/rt/EventManager.java | 34 +++++++++++-------- .../mango/rt/dataImage/DataPointRT.java | 10 +++--- .../scada_lts/web/beans/ApplicationBeans.java | 3 +- .../web/beans/ScadaContextClosedEvent.java | 4 +-- .../web/beans/ScadaContextRefreshedEvent.java | 26 -------------- 8 files changed, 35 insertions(+), 51 deletions(-) delete mode 100644 src/org/scada_lts/web/beans/ScadaContextRefreshedEvent.java diff --git a/WebContent/WEB-INF/web.xml b/WebContent/WEB-INF/web.xml index a447774a99..86990ba329 100644 --- a/WebContent/WEB-INF/web.xml +++ b/WebContent/WEB-INF/web.xml @@ -386,6 +386,9 @@ org.springframework.web.context.ContextLoaderListener + + com.serotonin.mango.MangoContextListener + org.springframework.security.web.session.HttpSessionEventPublisher diff --git a/src/com/serotonin/mango/MangoContextListener.java b/src/com/serotonin/mango/MangoContextListener.java index ca9a57ba52..9be49e7400 100644 --- a/src/com/serotonin/mango/MangoContextListener.java +++ b/src/com/serotonin/mango/MangoContextListener.java @@ -170,6 +170,10 @@ public void contextInitialized(ServletContextEvent evt) { } public void contextDestroyed(ServletContextEvent evt) { + if(Common.ctx == null) { + log.warn("Scada-LTS context terminated"); + return; + } log.info("Scada-LTS context terminating"); if (Common.ctx.getEventManager() != null) { diff --git a/src/com/serotonin/mango/db/BasePooledAccess.java b/src/com/serotonin/mango/db/BasePooledAccess.java index 419d384737..97cf86c680 100644 --- a/src/com/serotonin/mango/db/BasePooledAccess.java +++ b/src/com/serotonin/mango/db/BasePooledAccess.java @@ -164,7 +164,7 @@ public void terminate() log.info("Stopping database"); try { - if(dataSourceFound) + if(dataSourceFound && (dataSource instanceof BasicDataSource)) ((BasicDataSource) dataSource).close(); } catch(SQLException e) diff --git a/src/com/serotonin/mango/rt/EventManager.java b/src/com/serotonin/mango/rt/EventManager.java index 66320f0eb4..c8e99e4112 100644 --- a/src/com/serotonin/mango/rt/EventManager.java +++ b/src/com/serotonin/mango/rt/EventManager.java @@ -61,9 +61,6 @@ public class EventManager implements ILifecycle, ScadaWebSockets { private long lastAlarmTimestamp = 0; private int highestActiveAlarmLevel = 0; private static final String WS_MESSAGE = "Event Raised"; - - private UserEventServiceWebSocket userEventServiceWebsocket; - private EventsServiceWebSocket eventsServiceWebSocket; private IHighestAlarmLevelService highestAlarmLevelService; // @@ -318,8 +315,6 @@ private LocalizableMessage getAlarmLevelChangeMessage(String key, public void initialize() { eventService = new EventService(); userService = new UserService(); - userEventServiceWebsocket = ApplicationBeans.getUserEventServiceWebsocketBean(); - eventsServiceWebSocket = ApplicationBeans.getEventsServiceWebSocketBean(); highestAlarmLevelService = ApplicationBeans.getHighestAlarmLevelServiceBean(); // Get all active events from the database. @@ -457,7 +452,9 @@ public void notifyAlarmTimestampChange(long alarmTimestamp) { } public void resetHighestAlarmLevels() { - highestAlarmLevelService.doResetAlarmLevels(userEventServiceWebsocket::sendAlarmLevel); + ApplicationBeans.Lazy.getUserEventServiceWebsocketBean().ifPresent(websocket -> { + highestAlarmLevelService.doResetAlarmLevels(websocket::sendAlarmLevel); + }); notifyEventReset(); } @@ -475,14 +472,17 @@ public void notifyEventRaise(int eventId, int userId) { public void notifyEventRaise(EventInstance evt, User user) { if(evt.getAlarmLevel() > AlarmLevels.NONE) { - highestAlarmLevelService.doUpdateAlarmLevel(user, evt, userEventServiceWebsocket::sendAlarmLevel); - notifyEventUpdate(user, WsEventMessage.create(evt)); + ApplicationBeans.Lazy.getUserEventServiceWebsocketBean().ifPresent(userEventService -> { + highestAlarmLevelService.doUpdateAlarmLevel(user, evt, userEventService::sendAlarmLevel); + }); } } public void notifyEventAck(EventInstance evt, User user) { if(evt.getAlarmLevel() > AlarmLevels.NONE) { - highestAlarmLevelService.doRemoveAlarmLevel(user, evt, userEventServiceWebsocket::sendAlarmLevel); + ApplicationBeans.Lazy.getUserEventServiceWebsocketBean().ifPresent(userEventService -> { + highestAlarmLevelService.doRemoveAlarmLevel(user, evt, userEventService::sendAlarmLevel); + }); notifyEventUpdate(user, WsEventMessage.delete(evt)); } } @@ -503,7 +503,9 @@ public void notifyEventAck(int eventId) { public void notifyEventToggle(EventInstance evt, User user) { if(evt.getAlarmLevel() > AlarmLevels.NONE) { - highestAlarmLevelService.doRemoveAlarmLevel(user, evt, userEventServiceWebsocket::sendAlarmLevel); + ApplicationBeans.Lazy.getUserEventServiceWebsocketBean().ifPresent(userEventService -> { + highestAlarmLevelService.doRemoveAlarmLevel(user, evt, userEventService::sendAlarmLevel); + }); notifyEventUpdate(user, WsEventMessage.update(evt)); } } @@ -524,16 +526,20 @@ public void notifyEventToggle(int eventId, int userId) { } public void notifyEventUpdate(User user, WsEventMessage message) { - userEventServiceWebsocket.sendEventUpdate(user, message); + ApplicationBeans.Lazy.getUserEventServiceWebsocketBean().ifPresent(websocket -> { + websocket.sendEventUpdate(user, message); + }); } public void notifyEventReset() { - for(User user: userService.getActiveUsers()) - userEventServiceWebsocket.sendEventUpdate(user, WsEventMessage.reset()); + ApplicationBeans.Lazy.getUserEventServiceWebsocketBean().ifPresent(websocket -> { + for(User user: userService.getActiveUsers()) + websocket.sendEventUpdate(user, WsEventMessage.reset()); + }); } @Override public void notifyWebSocketSubscribers(String message) { - eventsServiceWebSocket.notifyEventsSubscribers(message); + ApplicationBeans.Lazy.getEventsServiceWebSocketBean().ifPresent(ws -> ws.notifyEventsSubscribers(message)); } } diff --git a/src/com/serotonin/mango/rt/dataImage/DataPointRT.java b/src/com/serotonin/mango/rt/dataImage/DataPointRT.java index 237fc79452..5f3b7c0187 100644 --- a/src/com/serotonin/mango/rt/dataImage/DataPointRT.java +++ b/src/com/serotonin/mango/rt/dataImage/DataPointRT.java @@ -77,14 +77,12 @@ public class DataPointRT implements IDataPoint, ILifecycle, TimeoutClient, Scada * determining whether to log numeric values. */ private double toleranceOrigin; - private final DataPointServiceWebSocket dataPointServiceWebSocket; private final PointValueService pointValueService; public DataPointRT(DataPointVO vo, PointLocatorRT pointLocator) { this.vo = vo; this.pointLocator = pointLocator; valueCache = new PointValueCache(vo.getId(), vo.getDefaultCacheSize()); - dataPointServiceWebSocket = ApplicationBeans.getDataPointServiceWebSocketBean(); pointValueService = new PointValueService(); } public DataPointRT(DataPointVO vo, PointLocatorRT pointLocator,int cacheSize,int maxSize) { @@ -92,7 +90,6 @@ public DataPointRT(DataPointVO vo, PointLocatorRT pointLocator,int cacheSize,int this.pointLocator = pointLocator; valueCache = new PointValueCache(cacheSize); valueCache.setMaxSize(maxSize); - dataPointServiceWebSocket = ApplicationBeans.getDataPointServiceWebSocketBean(); pointValueService = new PointValueService(); } @@ -100,7 +97,6 @@ public DataPointRT(DataPointVO vo) { this.vo = vo; this.pointLocator = null; valueCache = new PointValueCache(); - dataPointServiceWebSocket = ApplicationBeans.getDataPointServiceWebSocketBean(); pointValueService = new PointValueService(); } public PointValueCache getPointValueCache(){ @@ -494,11 +490,13 @@ protected void fireEvents(PointValueTime oldValue, PointValueTime newValue, @Override public void notifyWebSocketSubscribers(MangoValue message) { - dataPointServiceWebSocket.notifyValueSubscribers(message, this.vo.getId()); + ApplicationBeans.Lazy.getDataPointServiceWebSocketBean() + .ifPresent(ws -> ws.notifyValueSubscribers(message, this.vo.getId())); } public void notifyWebSocketStateSubscribers(boolean enabled) { - dataPointServiceWebSocket.notifyStateSubscribers(enabled, this.vo.getId()); + ApplicationBeans.Lazy.getDataPointServiceWebSocketBean() + .ifPresent(ws -> ws.notifyStateSubscribers(enabled, this.vo.getId())); } static class EventNotifyWorkItem extends AbstractBeforeAfterWorkItem { diff --git a/src/org/scada_lts/web/beans/ApplicationBeans.java b/src/org/scada_lts/web/beans/ApplicationBeans.java index 7d148bde98..caa2904903 100644 --- a/src/org/scada_lts/web/beans/ApplicationBeans.java +++ b/src/org/scada_lts/web/beans/ApplicationBeans.java @@ -153,7 +153,6 @@ public static EventsServiceWebSocket getEventsServiceWebSocketBean() { return getBeanFromContext("eventsServiceWebSocket", EventsServiceWebSocket.class); } - @Deprecated public static class Lazy { private Lazy() {} @@ -174,7 +173,7 @@ private static Optional getBeanFromContext(String beanName, Class claz try { return Optional.ofNullable(get(beanName, clazz)); } catch (NoSuchBeanDefinitionException ex) { - LOG.error(ex); + LOG.warn(ex); return Optional.empty(); } catch (Exception ex) { LOG.error(ex.getMessage(), ex); diff --git a/src/org/scada_lts/web/beans/ScadaContextClosedEvent.java b/src/org/scada_lts/web/beans/ScadaContextClosedEvent.java index b0b2673609..c2c1a0a06e 100644 --- a/src/org/scada_lts/web/beans/ScadaContextClosedEvent.java +++ b/src/org/scada_lts/web/beans/ScadaContextClosedEvent.java @@ -18,8 +18,8 @@ public ScadaContextClosedEvent(MangoContextListener mangoContextListener) { } @Override - public void onApplicationEvent(ContextClosedEvent contextRefreshedEvent) { - WebApplicationContext webApplicationContext = (WebApplicationContext)contextRefreshedEvent.getSource(); + public void onApplicationEvent(ContextClosedEvent contextClosedEvent) { + WebApplicationContext webApplicationContext = (WebApplicationContext)contextClosedEvent.getSource(); ServletContextEvent servletContextEvent = new ServletContextEvent(webApplicationContext.getServletContext()); mangoContextListener.contextDestroyed(servletContextEvent); } diff --git a/src/org/scada_lts/web/beans/ScadaContextRefreshedEvent.java b/src/org/scada_lts/web/beans/ScadaContextRefreshedEvent.java deleted file mode 100644 index eaf9b62a6d..0000000000 --- a/src/org/scada_lts/web/beans/ScadaContextRefreshedEvent.java +++ /dev/null @@ -1,26 +0,0 @@ -package org.scada_lts.web.beans; - -import com.serotonin.mango.MangoContextListener; -import org.springframework.context.ApplicationListener; -import org.springframework.context.event.ContextRefreshedEvent; -import org.springframework.stereotype.Component; -import org.springframework.web.context.WebApplicationContext; - -import javax.servlet.ServletContextEvent; - -@Component -public class ScadaContextRefreshedEvent implements ApplicationListener { - - private final MangoContextListener mangoContextListener; - - public ScadaContextRefreshedEvent(MangoContextListener mangoContextListener) { - this.mangoContextListener = mangoContextListener; - } - - @Override - public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) { - WebApplicationContext webApplicationContext = (WebApplicationContext)contextRefreshedEvent.getSource(); - ServletContextEvent servletContextEvent = new ServletContextEvent(webApplicationContext.getServletContext()); - mangoContextListener.contextInitialized(servletContextEvent); - } -}