Skip to content

Add message tracking information in VMRouter - #49

Open
rogin wants to merge 1 commit into
OpenIntegrationEngine:mainfrom
rogin:enhance-vmrouter
Open

Add message tracking information in VMRouter#49
rogin wants to merge 1 commit into
OpenIntegrationEngine:mainfrom
rogin:enhance-vmrouter

Conversation

@rogin

Copy link
Copy Markdown
Contributor

Enhance the VMRouter using @tonygermano's existing code template.

Initial PR with @jonbartels feedback is here.

Note that I removed the boolean property that toggled its functionality, so it's always on now.

@pacmano1

Copy link
Copy Markdown
Contributor

Revised nextgenhealthcare/connect#5293, closed issue and renamed appending (fixed in OIE fork) to issue.

Signed-off-by: Richard Ogin <rogin@users.noreply.github.com>
@jonbartelsjonbartels added this to the Technical Preview 2 milestone May 6, 2025
@kpalang
kpalang requested review from a team, gibson9583, kayyagari, pacmano1 and ssrowe and removed request for a teamSeptember 8, 2025 15:25
@jonbartels
jonbartels requested review from a team, gibson9583, kayyagari, kpalang, pacmano1, ssrowe and tonygermano and removed request for a team, gibson9583, kayyagari, pacmano1 and ssroweSeptember 8, 2025 15:26

CopilotAI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR enhances the VMRouter class to automatically track message routing chains, similar to how Channel Writer connectors work. The enhancement adds source channel IDs and message IDs to the sourceMap of routed messages, making it easier to trace message flow through multiple channels when using VMRouter programmatically instead of Channel Writer connectors.

Key Changes:

  • Added a new VMRouter constructor that accepts tracking information (channelId, messageId, sourceMap)
  • Introduced new overloaded routing methods that accept sourceMap and destinationSet parameters for enhanced control
  • Created a TrackingEnhancer inner class that enriches source maps with routing chain information

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 10 comments.

FileDescription
server/src/com/mirth/connect/server/util/javascript/JavaScriptScopeUtil.javaAdds addRouterEnhancement method to create VMRouter instances with tracking capabilities and integrates them into all JavaScript execution scopes (preprocessor, postprocessor, filter/transformer, etc.). Also adds an unused doGetContext method.
server/src/com/mirth/connect/server/userutil/VMRouter.javaExtends VMRouter with new constructor, overloaded routing methods for enhanced control, TrackingEnhancer inner class for automatic source tracking, createRawMessage helper method, and improved Javadoc with @link references.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +273 to +284
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);
}
}

CopilotAIDec 12, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The createRawMessage method doesn't validate that the message parameter is non-null before calling message.toString() on line 282. If a null message is passed, this will throw a NullPointerException. Consider adding validation at the beginning of this method to throw a more meaningful exception or handle the null case appropriately.

Copilot uses AI. Check for mistakes.
Comment on lines +287 to +289
* Adds additional message tracking data.
*
* TrackingEnhancer

CopilotAIDec 12, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Javadoc comment "TrackingEnhancer" on line 289 is redundant as it simply restates the class name. Consider either removing this line or expanding it to provide a more meaningful description of what the TrackingEnhancer does, such as "Inner class that enriches source maps with message tracking information for routing chains."

Suggested change
* Addsadditionalmessagetrackingdata.
*
* TrackingEnhancer
* Innerclassthatenrichessourcemapswithmessagetrackinginformationforroutingchains.
* Usedtoaddchannelandmessageidentifierstothesourcemapfortrackingpurposes.

Copilot uses AI. Check for mistakes.
List<String> sourceChannelIds = lookupAsList("sourceChannelIds", "sourceChannelId");
List<String> sourceMessageIds = lookupAsList("sourceMessageIds", "sourceMessageId");

HashMap<String, Object> newSourceMap = new HashMap<String, Object>(messageSourceMap);

CopilotAIDec 12, 2025

Copy link

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.

Suggested change
HashMap<String, Object> newSourceMap = newHashMap<String, Object>(messageSourceMap);
HashMap<String, Object> newSourceMap = newHashMap<String, Object>(messageSourceMap.size() + 4);
newSourceMap.putAll(messageSourceMap);

Copilot uses AI. Check for mistakes.
Comment on lines +355 to +357
((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);

CopilotAIDec 12, 2025

Copy link

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.

Suggested change
((Collection<?>) primaryValue).stream().map(i -> i.toString()).forEach(result::add);
} elseif (primaryValueinstanceofObject[]) {
Arrays.stream((Object[]) primaryValue).map(i -> i.toString()).forEach(result::add);
((Collection<?>) primaryValue).stream().map(Object::toString).forEach(result::add);
} elseif (primaryValueinstanceofObject[]) {
Arrays.stream((Object[]) primaryValue).map(Object::toString).forEach(result::add);

Copilot uses AI. Check for mistakes.
/**
* Enrich the given source map with additional message tracking data.
*
* @param messageSourceMap

CopilotAIDec 12, 2025

Copy link

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").

Suggested change
* @parammessageSourceMap
* @parammessageSourceMapthesourcemapprovidedbythecaller, maybenull

Copilot uses AI. Check for mistakes.
/**
* Look up a value from the environment's {@link SourceMap}
*
* @param key

CopilotAIDec 12, 2025

Copy link

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.

Suggested change
* @paramkey
* @paramkeythekeytolookupinthesourcemap

Copilot uses AI. Check for mistakes.
Comment on lines +85 to +88
public Context doGetContext(ContextFactory contextFactory) {
return JavaScriptScopeUtil.getContext(contextFactory);
}

CopilotAIDec 12, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This public method doGetContext appears to be unused throughout the codebase. It's a non-static wrapper around the static getContext method but serves no clear purpose in this PR. Consider removing it unless there's a specific planned use case.

Suggested change
publicContextdoGetContext(ContextFactorycontextFactory) {
returnJavaScriptScopeUtil.getContext(contextFactory);
}

Copilot uses AI. Check for mistakes.
*
* TrackingEnhancer
*/
private class TrackingEnhancer {

CopilotAIDec 12, 2025

Copy link

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.

Suggested change
privateclassTrackingEnhancer {
privatestaticclassTrackingEnhancer {

Copilot uses AI. Check for mistakes.
*
* @see #routeMessageByChannelId(String, Object, Map, Collection)
*/
public Response routeMessageByChannelId(String channelId, Object message) {

CopilotAIDec 12, 2025

Copy link

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.

Copilot uses AI. Check for mistakes.
* @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) {

CopilotAIDec 12, 2025

Copy link

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.

Copilot uses AI. Check for mistakes.
@ssrowe

ssrowe commented Mar 11, 2026

Copy link
Copy Markdown

@rogin this is slated for our next release. Can you take a look at the Copilot feedback above, pull in the changes that you think make sense and rebase? We can then get some additional reviews on this and get it merged.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@rogin@pacmano1@ssrowe@jonbartels