-
Notifications
You must be signed in to change notification settings - Fork 41
Add message tracking information in VMRouter #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -9,6 +9,14 @@ | |||||||||||||
|
|
||||||||||||||
| package com.mirth.connect.server.userutil; | ||||||||||||||
|
|
||||||||||||||
| import java.util.ArrayList; | ||||||||||||||
| import java.util.Arrays; | ||||||||||||||
| import java.util.Collection; | ||||||||||||||
| import java.util.Collections; | ||||||||||||||
| import java.util.HashMap; | ||||||||||||||
| import java.util.List; | ||||||||||||||
| import java.util.Map; | ||||||||||||||
|
|
||||||||||||||
| import org.apache.logging.log4j.LogManager; | ||||||||||||||
| import org.apache.logging.log4j.Logger; | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -28,77 +36,132 @@ public class VMRouter { | |||||||||||||
| private ChannelController channelController = ControllerFactory.getFactory().createChannelController(); | ||||||||||||||
| private EngineController engineController = ControllerFactory.getFactory().createEngineController(); | ||||||||||||||
|
|
||||||||||||||
| private TrackingEnhancer trackingEnhancer; | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Instantiates a VMRouter object. | ||||||||||||||
| */ | ||||||||||||||
| public VMRouter() {} | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Dispatches a message to a channel, specified by the deployed channel name. If the dispatch | ||||||||||||||
| * fails for any reason (for example, if the target channel is not started), a Response object | ||||||||||||||
| * with the ERROR status and the error message will be returned. | ||||||||||||||
| * | ||||||||||||||
| * @param channelName | ||||||||||||||
| * The name of the deployed channel to dispatch the message to. | ||||||||||||||
| * @param message | ||||||||||||||
| * The message to dispatch to the channel. | ||||||||||||||
| * @return The Response object returned by the channel, if its source connector is configured to | ||||||||||||||
| * return one. | ||||||||||||||
| * Instantiates a VMRouter object with additional message tracking enhancements. | ||||||||||||||
| * | ||||||||||||||
| * @param channelId channel ID or "NONE" if null | ||||||||||||||
| * @param messageId message ID or -1L if null | ||||||||||||||
| * @param sourceMap the message's source map | ||||||||||||||
| */ | ||||||||||||||
| public VMRouter(String channelId, Long messageId, SourceMap sourceMap) { | ||||||||||||||
| this.trackingEnhancer = new TrackingEnhancer(channelId, messageId, sourceMap); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Dispatches a message to a channel, specified by the deployed channel name. If | ||||||||||||||
| * the dispatch fails for any reason (for example, if the target channel is not | ||||||||||||||
| * started), a {@link Response} object with the {@link Status#ERROR} status and | ||||||||||||||
| * the error message will be returned. | ||||||||||||||
| * | ||||||||||||||
| * @param channelName The name of the deployed channel to dispatch the message | ||||||||||||||
| * to. | ||||||||||||||
| * @param message The message to dispatch to the channel. | ||||||||||||||
| * @return The {@link Response} object returned by the channel, if its source | ||||||||||||||
| * connector is configured to return one. | ||||||||||||||
| */ | ||||||||||||||
| public Response routeMessage(String channelName, String message) { | ||||||||||||||
| return routeMessage(channelName, new RawMessage(message)); | ||||||||||||||
| return routeMessage(channelName, createRawMessage(message, null, null)); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Dispatches a message to a channel, specified by the deployed channel name. If the dispatch | ||||||||||||||
| * fails for any reason (for example, if the target channel is not started), a Response object | ||||||||||||||
| * with the ERROR status and the error message will be returned. | ||||||||||||||
| * | ||||||||||||||
| * @param channelName | ||||||||||||||
| * The name of the deployed channel to dispatch the message to. | ||||||||||||||
| * @param rawMessage | ||||||||||||||
| * A RawMessage object to dispatch to the channel. | ||||||||||||||
| * @return The Response object returned by the channel, if its source connector is configured to | ||||||||||||||
| * return one. | ||||||||||||||
| * Dispatches a message to a channel, specified by the deployed channel name. If | ||||||||||||||
| * the dispatch fails for any reason (for example, if the target channel is not | ||||||||||||||
| * started), a {@link Response} object with the {@link Status#ERROR} status and | ||||||||||||||
| * the error message will be returned. | ||||||||||||||
| * | ||||||||||||||
| * @param channelName The name of the deployed channel to dispatch the message | ||||||||||||||
| * to. | ||||||||||||||
| * @param rawMessage A {@link RawMessage} object to dispatch to the channel. | ||||||||||||||
| * @return The {@link Response} object returned by the channel, if its source | ||||||||||||||
| * connector is configured to return one. | ||||||||||||||
| */ | ||||||||||||||
| public Response routeMessage(String channelName, RawMessage rawMessage) { | ||||||||||||||
| com.mirth.connect.model.Channel channel = channelController.getDeployedChannelByName(channelName); | ||||||||||||||
|
|
||||||||||||||
| if (channel == null) { | ||||||||||||||
| logger.error("Could not find channel to route to for channel name: " + channelName); | ||||||||||||||
| return new Response(Status.ERROR, "Could not find channel to route to for channel name: " + channelName); | ||||||||||||||
| String message = "Could not find channel to route to for channel name: " + channelName; | ||||||||||||||
| logger.error(message); | ||||||||||||||
| return new Response(Status.ERROR, message); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| return routeMessageByChannelId(channel.getId(), rawMessage); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Dispatches a message to a channel, specified by the deployed channel ID. If the dispatch | ||||||||||||||
| * fails for any reason (for example, if the target channel is not started), a Response object | ||||||||||||||
| * with the ERROR status and the error message will be returned. | ||||||||||||||
| * | ||||||||||||||
| * @param channelId | ||||||||||||||
| * The ID of the deployed channel to dispatch the message to. | ||||||||||||||
| * @param message | ||||||||||||||
| * The message to dispatch to the channel. | ||||||||||||||
| * @return The Response object returned by the channel, if its source connector is configured to | ||||||||||||||
| * return one. | ||||||||||||||
| * Route a message to the specified channelName. Information about the chain of | ||||||||||||||
| * source channel Ids and source message Ids will be included in the sourceMap | ||||||||||||||
| * of the downstream message automatically in a similar manner as if a Channel | ||||||||||||||
| * Writer was being used. | ||||||||||||||
| * | ||||||||||||||
| * @param channelName The name of the channel to which to route the message. | ||||||||||||||
| * @param message The content of the message to be sent, textual or binary. | ||||||||||||||
| * As String or byte[]. | ||||||||||||||
| * @param sourceMap A map containing entries to include in the sourceMap of | ||||||||||||||
| * the sent message. | ||||||||||||||
| * @return The {@link Response} object returned by the channel. | ||||||||||||||
| * | ||||||||||||||
| * @see #routeMessage(String, Object, Map, Collection) | ||||||||||||||
| */ | ||||||||||||||
| public Response routeMessage(String channelName, Object message, Map<String, Object> sourceMap) { | ||||||||||||||
| return routeMessage(channelName, message, sourceMap, null); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Route a message to the specified channelName. Information about the chain of | ||||||||||||||
| * source channel Ids and source message Ids will be included in the sourceMap | ||||||||||||||
| * of the downstream message automatically in a similar manner as if a Channel | ||||||||||||||
| * Writer was being used. | ||||||||||||||
| * | ||||||||||||||
| * @param channelName The name of the channel to which to route the message. | ||||||||||||||
| * @param message The content of the message to be sent, textual or | ||||||||||||||
| * binary. As String or byte[]. | ||||||||||||||
| * @param sourceMap A map containing entries to include in the sourceMap of | ||||||||||||||
| * the sent message. | ||||||||||||||
| * @param destinationSet A collection of integers (metadata IDs) representing | ||||||||||||||
| * which destinations to dispatch the message to. Null may | ||||||||||||||
| * be passed to indicate all destinations. If unspecified, | ||||||||||||||
| * all destinations is the default. | ||||||||||||||
| * @return The {@link Response} object returned by the channel. | ||||||||||||||
| * | ||||||||||||||
| * @see #routeMessage(String, RawMessage) | ||||||||||||||
| */ | ||||||||||||||
| public Response routeMessage(String channelName, Object message, Map<String, Object> sourceMap, | ||||||||||||||
| Collection<Number> destinationSet) { | ||||||||||||||
| return routeMessage(channelName, createRawMessage(message, sourceMap, destinationSet)); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Dispatches a message to a channel, specified by the deployed channel ID. If | ||||||||||||||
| * the dispatch fails for any reason (for example, if the target channel is not | ||||||||||||||
| * started), a {@link Response} object with the {@link Status#ERROR} status and | ||||||||||||||
| * the error message will be returned. | ||||||||||||||
| * | ||||||||||||||
| * @param channelId The ID of the deployed channel to dispatch the message to. | ||||||||||||||
| * @param message The message to dispatch to the channel. | ||||||||||||||
| * @return The {@link Response} object returned by the channel, if its source | ||||||||||||||
| * connector is configured to return one. | ||||||||||||||
| */ | ||||||||||||||
| public Response routeMessageByChannelId(String channelId, String message) { | ||||||||||||||
| return routeMessageByChannelId(channelId, new RawMessage(message)); | ||||||||||||||
| return routeMessageByChannelId(channelId, createRawMessage(message, null, null)); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Dispatches a message to a channel, specified by the deployed channel ID. If the dispatch | ||||||||||||||
| * fails for any reason (for example, if the target channel is not started), a Response object | ||||||||||||||
| * with the ERROR status and the error message will be returned. | ||||||||||||||
| * | ||||||||||||||
| * @param channelId | ||||||||||||||
| * The ID of the deployed channel to dispatch the message to. | ||||||||||||||
| * @param rawMessage | ||||||||||||||
| * A RawMessage object to dispatch to the channel. | ||||||||||||||
| * @return The Response object returned by the channel, if its source connector is configured to | ||||||||||||||
| * return one. | ||||||||||||||
| * Dispatches a message to a channel, specified by the deployed channel ID. If | ||||||||||||||
| * the dispatch fails for any reason (for example, if the target channel is not | ||||||||||||||
| * started), a {@link Response} object with the {@link Status#ERROR} status and | ||||||||||||||
| * the error message will be returned. | ||||||||||||||
| * | ||||||||||||||
| * @param channelId The ID of the deployed channel to dispatch the message to. | ||||||||||||||
| * @param rawMessage A {@link RawMessage} object to dispatch to the channel. | ||||||||||||||
| * @return The {@link Response} object returned by the channel, if its source | ||||||||||||||
| * connector is configured to return one. | ||||||||||||||
| */ | ||||||||||||||
| public Response routeMessageByChannelId(String channelId, RawMessage rawMessage) { | ||||||||||||||
| try { | ||||||||||||||
|
|
@@ -119,11 +182,198 @@ public Response routeMessageByChannelId(String channelId, RawMessage rawMessage) | |||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Route a message to the specified channelId. Information about the chain of | ||||||||||||||
| * source channel Ids and source message Ids will be included in the sourceMap | ||||||||||||||
| * of the downstream message automatically in a similar manner as if a Channel | ||||||||||||||
| * Writer was being used. | ||||||||||||||
| * | ||||||||||||||
| * @param channelId The unique identifier of the channel to which to route the | ||||||||||||||
| * message. | ||||||||||||||
| * @param message The content of the message to be sent, textual or binary. As | ||||||||||||||
| * String or byte[]. | ||||||||||||||
| * @return The {@link Response} object returned by the channel. | ||||||||||||||
| * | ||||||||||||||
| * @see #routeMessageByChannelId(String, Object, Map, Collection) | ||||||||||||||
| */ | ||||||||||||||
| public Response routeMessageByChannelId(String channelId, Object message) { | ||||||||||||||
|
||||||||||||||
| return routeMessageByChannelId(channelId, message, null, null); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Route a message to the specified channelId. Information about the chain of | ||||||||||||||
| * source channel Ids and source message Ids will be included in the sourceMap | ||||||||||||||
| * of the downstream message automatically in a similar manner as if a Channel | ||||||||||||||
| * Writer was being used. | ||||||||||||||
| * | ||||||||||||||
| * @param channelId The unique identifier of the channel to which to route the | ||||||||||||||
| * message. | ||||||||||||||
| * @param message The content of the message to be sent, textual or binary. As | ||||||||||||||
| * String or byte[]. | ||||||||||||||
| * @param sourceMap A map containing entries to include in the sourceMap of the | ||||||||||||||
| * sent message. | ||||||||||||||
| * @return The {@link Response} object returned by the channel. | ||||||||||||||
| * | ||||||||||||||
| * @see #routeMessageByChannelId(String, Object, Map, Collection) | ||||||||||||||
| */ | ||||||||||||||
| public Response routeMessageByChannelId(String channelId, Object message, Map<String, Object> sourceMap) { | ||||||||||||||
| return routeMessageByChannelId(channelId, message, sourceMap, null); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Route a message to the specified channelId. Information about the chain of | ||||||||||||||
| * source channel Ids and source message Ids will be included in the sourceMap | ||||||||||||||
| * of the downstream message automatically in a similar manner as if a Channel | ||||||||||||||
| * Writer was being used. | ||||||||||||||
| * | ||||||||||||||
| * @param channelId The unique identifier of the channel to which to route | ||||||||||||||
| * the message. | ||||||||||||||
| * @param message The content of the message to be sent, textual or | ||||||||||||||
| * binary. As String or byte[]. | ||||||||||||||
| * @param sourceMap A map containing entries to include in the sourceMap of | ||||||||||||||
| * the sent message. | ||||||||||||||
| * @param destinationSet A collection of integers (metadata IDs) representing | ||||||||||||||
| * which destinations to dispatch the message to. Null may | ||||||||||||||
| * be passed to indicate all destinations. If unspecified, | ||||||||||||||
| * all destinations is the default. | ||||||||||||||
| * @return The {@link Response} object returned by the channel. | ||||||||||||||
| * | ||||||||||||||
| * @see #routeMessageByChannelId(String, RawMessage) | ||||||||||||||
| */ | ||||||||||||||
| public Response routeMessageByChannelId(String channelId, Object message, Map<String, Object> sourceMap, | ||||||||||||||
| Collection<Number> destinationSet) { | ||||||||||||||
| return routeMessageByChannelId(channelId, createRawMessage(message, sourceMap, destinationSet)); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| private com.mirth.connect.donkey.model.message.RawMessage convertRawMessage(RawMessage message) { | ||||||||||||||
| if (message.isBinary()) { | ||||||||||||||
| return new com.mirth.connect.donkey.model.message.RawMessage(message.getRawBytes(), message.getDestinationMetaDataIds(), message.getSourceMap()); | ||||||||||||||
| return new com.mirth.connect.donkey.model.message.RawMessage(message.getRawBytes(), | ||||||||||||||
| message.getDestinationMetaDataIds(), message.getSourceMap()); | ||||||||||||||
| } else { | ||||||||||||||
| return new com.mirth.connect.donkey.model.message.RawMessage(message.getRawData(), | ||||||||||||||
| message.getDestinationMetaDataIds(), message.getSourceMap()); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Create a {@link RawMessage} with the specified content, sourceMap, and | ||||||||||||||
| * destinationSet. | ||||||||||||||
| * | ||||||||||||||
| * @param message The content of the message to be sent, textual or | ||||||||||||||
| * binary. As String or byte[]. | ||||||||||||||
| * @param sourceMap A map containing entries to include in the sourceMap of | ||||||||||||||
| * the {@link RawMessage} (optional). | ||||||||||||||
| * @param destinationSet A collection of integers (metadata IDs) representing | ||||||||||||||
| * which destinations to dispatch the message to. Null may | ||||||||||||||
| * be passed to indicate all destinations. If unspecified, | ||||||||||||||
| * all destinations is the default (optional). | ||||||||||||||
| * @return A {@link RawMessage} object containing the message, source, and | ||||||||||||||
| * destination information. | ||||||||||||||
| */ | ||||||||||||||
| public RawMessage createRawMessage(Object message, Map<String, Object> sourceMap, | ||||||||||||||
| Collection<Number> destinationSet) { | ||||||||||||||
| if (trackingEnhancer != null) { | ||||||||||||||
| sourceMap = trackingEnhancer.enrich(sourceMap); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| if (message instanceof byte[]) { | ||||||||||||||
| return new RawMessage((byte[]) message, destinationSet, sourceMap); | ||||||||||||||
| } else { | ||||||||||||||
| return new com.mirth.connect.donkey.model.message.RawMessage(message.getRawData(), message.getDestinationMetaDataIds(), message.getSourceMap()); | ||||||||||||||
| return new RawMessage(message.toString(), destinationSet, sourceMap); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
Comment on lines
+273
to
+284
|
||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Adds additional message tracking data. | ||||||||||||||
| * | ||||||||||||||
| * TrackingEnhancer | ||||||||||||||
|
Comment on lines
+287
to
+289
|
||||||||||||||
| * Adds additional message tracking data. | |
| * | |
| * TrackingEnhancer | |
| * Inner class that enriches source maps with message tracking information for routing chains. | |
| * Used to add channel and message identifiers to the source map for tracking purposes. |
Copilot
AI
Dec 12, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TrackingEnhancer should be made static, since the enclosing instance is not used.
| private class TrackingEnhancer { | |
| private static class TrackingEnhancer { |
Copilot
AI
Dec 12, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The @param documentation for messageSourceMap is missing. While parameters in private methods don't strictly require documentation, it would improve maintainability to document what this parameter represents (e.g., "the source map provided by the caller, may be null").
| * @param messageSourceMap | |
| * @param messageSourceMap the source map provided by the caller, may be null |
Copilot
AI
Dec 12, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The HashMap is constructed with an initial capacity based on messageSourceMap size, but then 4 additional entries are added. To avoid potential resizing, consider using new HashMap<>(messageSourceMap.size() + 4) or similar to ensure adequate initial capacity.
| HashMap<String, Object> newSourceMap = new HashMap<String, Object>(messageSourceMap); | |
| HashMap<String, Object> newSourceMap = new HashMap<String, Object>(messageSourceMap.size() + 4); | |
| newSourceMap.putAll(messageSourceMap); |
Copilot
AI
Dec 12, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The lambda expression i -> i.toString() can be simplified to a method reference: Object::toString. Similarly on line 357. This improves code readability and is a common Java best practice.
| ((Collection<?>) primaryValue).stream().map(i -> i.toString()).forEach(result::add); | |
| } else if (primaryValue instanceof Object[]) { | |
| Arrays.stream((Object[]) primaryValue).map(i -> i.toString()).forEach(result::add); | |
| ((Collection<?>) primaryValue).stream().map(Object::toString).forEach(result::add); | |
| } else if (primaryValue instanceof Object[]) { | |
| Arrays.stream((Object[]) primaryValue).map(Object::toString).forEach(result::add); |
Copilot
AI
Dec 12, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The @param documentation for key is missing. Adding a brief description (e.g., "the key to look up in the source map") would improve code documentation consistency.
| * @param key | |
| * @param key the key to look up in the source map |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Method VMRouter.routeMessageByChannelId(..) could be confused with overloaded method routeMessageByChannelId, since dispatch depends on static types.