From 5fec064021840e40f06eb49b25d628008049d205 Mon Sep 17 00:00:00 2001 From: mynecker Date: Wed, 23 Oct 2024 17:35:13 +0200 Subject: [PATCH] added DefaultSolacePersistentMessageHandlerObserver to avoid redundant null checks --- ...olacePersistentMessageHandlerObserver.java | 31 +++++++++++++++++++ .../SolacePersistentMessageHandler.java | 29 ++++++++--------- 2 files changed, 44 insertions(+), 16 deletions(-) create mode 100644 service/application/src/main/java/com/solace/maas/ep/event/management/agent/subscriber/DefaultSolacePersistentMessageHandlerObserver.java diff --git a/service/application/src/main/java/com/solace/maas/ep/event/management/agent/subscriber/DefaultSolacePersistentMessageHandlerObserver.java b/service/application/src/main/java/com/solace/maas/ep/event/management/agent/subscriber/DefaultSolacePersistentMessageHandlerObserver.java new file mode 100644 index 00000000..da2bb32a --- /dev/null +++ b/service/application/src/main/java/com/solace/maas/ep/event/management/agent/subscriber/DefaultSolacePersistentMessageHandlerObserver.java @@ -0,0 +1,31 @@ +package com.solace.maas.ep.event.management.agent.subscriber; + +import com.solace.messaging.receiver.InboundMessage; + +public class DefaultSolacePersistentMessageHandlerObserver implements SolacePersistentMessageHandlerObserver { + + @Override + public void onMessageReceived(InboundMessage message) { + // Do nothing + } + + @Override + public void onMessageProcessingInitiated(InboundMessage message) { + // Do nothing + } + + @Override + public void onMessageProcessingCompleted(InboundMessage message) { + // Do nothing + } + + @Override + public void onMessageProcessingAcknowledged(InboundMessage message) { + // Do nothing + } + + @Override + public void onMessageProcessingFailed(InboundMessage message) { + // Do nothing + } +} diff --git a/service/application/src/main/java/com/solace/maas/ep/event/management/agent/subscriber/SolacePersistentMessageHandler.java b/service/application/src/main/java/com/solace/maas/ep/event/management/agent/subscriber/SolacePersistentMessageHandler.java index 3acd56ae..a34f5d8b 100644 --- a/service/application/src/main/java/com/solace/maas/ep/event/management/agent/subscriber/SolacePersistentMessageHandler.java +++ b/service/application/src/main/java/com/solace/maas/ep/event/management/agent/subscriber/SolacePersistentMessageHandler.java @@ -41,7 +41,6 @@ public class SolacePersistentMessageHandler extends BaseSolaceMessageHandler imp private PersistentMessageReceiver persistentMessageReceiver; // only used for testing - @Setter private SolacePersistentMessageHandlerObserver messageHandlerObserver; protected SolacePersistentMessageHandler(MessagingService messagingService, @@ -49,6 +48,7 @@ protected SolacePersistentMessageHandler(MessagingService messagingService, List messageProcessorList) { super(); + this.messageHandlerObserver = new DefaultSolacePersistentMessageHandlerObserver(); this.messagingService = messagingService; this.eventPortalProperties = eventPortalProperties; messageProcessorsByClassType = messageProcessorList.stream() @@ -63,19 +63,22 @@ protected SolacePersistentMessageHandler(MessagingService messagingService, } + public void setMessageHandlerObserver(SolacePersistentMessageHandlerObserver observer) { + this.messageHandlerObserver = observer; + if (this.messageHandlerObserver==null){ + this.messageHandlerObserver = new DefaultSolacePersistentMessageHandlerObserver(); + } + } + @Override public void onMessage(InboundMessage inboundMessage) { - if (messageHandlerObserver != null) { - messageHandlerObserver.onMessageReceived(inboundMessage); - } + messageHandlerObserver.onMessageReceived(inboundMessage); executor.submit(() -> processMessage(inboundMessage)); } private void processMessage(InboundMessage inboundMessage) { - if (messageHandlerObserver != null) { - messageHandlerObserver.onMessageProcessingInitiated(inboundMessage); - } + messageHandlerObserver.onMessageProcessingInitiated(inboundMessage); String mopMessageSubclass = ""; MessageProcessor processor = null; Object message = null; @@ -91,19 +94,13 @@ private void processMessage(InboundMessage inboundMessage) { log.trace("onMessage: {}\n{}", messageClass, messageAsString); message = toMessage(messageAsString, messageClass); processor.processMessage(processor.castToMessageClass(message)); - if (messageHandlerObserver != null) { - messageHandlerObserver.onMessageProcessingCompleted(inboundMessage); - } + messageHandlerObserver.onMessageProcessingCompleted(inboundMessage); } catch (Exception e) { handleProcessingError(mopMessageSubclass, processor, message, e); - if (messageHandlerObserver != null) { - messageHandlerObserver.onMessageProcessingFailed(inboundMessage); - } + messageHandlerObserver.onMessageProcessingFailed(inboundMessage); } finally { acknowledgeMessage(inboundMessage); - if (messageHandlerObserver != null) { - messageHandlerObserver.onMessageProcessingAcknowledged(inboundMessage); - } + messageHandlerObserver.onMessageProcessingAcknowledged(inboundMessage); } }