Skip to content

Commit 893ae63

Browse files
committed
Add server endpoint for submitting a batch message
processMessage returns a single message id, taken from the response handler's selected result. For a batch that is only the first or last message, leaving a caller no way to learn the ids of the others. The smoke test harness needs them to assert per-message content. POST /channels/{channelId}/batchMessagesWithObj returns every id. The response handler is already threaded through dispatchBatchMessage, so CollectingResponseHandler only has to record each id as it is set. It keeps the ids and not the DispatchResults, so a long batch does not retain the processed messages, maps and content of the messages that have already finished. Processing is unchanged. Whether a batch is split is still governed by the channel's Process Batch setting; on a channel with batch processing disabled the dispatch takes the single-message path and the endpoint returns a one-element list. EngineController.dispatchRawMessage gains a five-argument overload. The existing four-argument form delegates to it with a SimpleResponseHandler, so its callers are unaffected, but the interface gains an abstract method and out-of-tree implementations will need updating. Signed-off-by: Mitch Gaffigan <mitch.gaffigan@comcast.net>
1 parent 03eefcf commit 893ae63

6 files changed

Lines changed: 87 additions & 1 deletion

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// SPDX-License-Identifier: MPL-2.0
2+
// SPDX-FileCopyrightText: Mitch Gaffigan <mitch@gaffigan.net>
3+
4+
package com.mirth.connect.donkey.server.message.batch;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
import com.mirth.connect.donkey.server.channel.DispatchResult;
10+
11+
/**
12+
* Reports every message of a batch, where {@link ResponseHandler#getResultForResponse()} reports
13+
* only the one selected for the response.
14+
*/
15+
public class CollectingResponseHandler extends SimpleResponseHandler {
16+
17+
private final List<Long> messageIds = new ArrayList<Long>();
18+
19+
@Override
20+
public void setDispatchResult(DispatchResult dispatchResult) {
21+
super.setDispatchResult(dispatchResult);
22+
23+
if (dispatchResult != null) {
24+
messageIds.add(dispatchResult.getMessageId());
25+
}
26+
}
27+
28+
public List<Long> getMessageIds() {
29+
return messageIds;
30+
}
31+
32+
}

‎server/src/main/java/com/mirth/connect/client/core/Client.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1785,6 +1785,16 @@ public Long processMessage(String channelId, RawMessage rawMessage) throws Clien
17851785
return getServlet(MessageServletInterface.class).processMessage(channelId, rawMessage);
17861786
}
17871787

1788+
/**
1789+
* Processes a new message through a channel, returning the ID of every message it produced.
1790+
*
1791+
* @see MessageServletInterface#processBatchMessage
1792+
*/
1793+
@Override
1794+
public List<Long> processBatchMessage(String channelId, RawMessage rawMessage) throws ClientException {
1795+
return getServlet(MessageServletInterface.class).processBatchMessage(channelId, rawMessage);
1796+
}
1797+
17881798
/**
17891799
* Processes a new message through a channel, using the RawMessage object.
17901800
*

‎server/src/main/java/com/mirth/connect/client/core/api/servlets/MessageServletInterface.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,19 @@ public Long processMessage(// @formatter:off
9797
@ExampleObject(name = "rawMessage", ref = "../apiexamples/raw_message_json") }) }) RawMessage rawMessage) throws ClientException;
9898
// @formatter:on
9999

100+
@POST
101+
@Path("/{channelId}/batchMessagesWithObj")
102+
@Operation(summary = "Processes a new message through a channel, returning the ID of every message it produced.")
103+
@MirthOperation(name = "processMessages", display = "Process messages", permission = Permissions.MESSAGES_PROCESS, type = ExecuteType.ASYNC)
104+
public List<Long> processBatchMessage(// @formatter:off
105+
@Param("channelId") @Parameter(description = "The ID of the channel.", required = true) @PathParam("channelId") String channelId,
106+
@Param("rawMessage") @RequestBody(description = "The RawMessage object to process.", required = true, content = {
107+
@Content(mediaType = MediaType.APPLICATION_XML, examples = {
108+
@ExampleObject(name = "rawMessage", ref = "../apiexamples/raw_message_xml") }),
109+
@Content(mediaType = MediaType.APPLICATION_JSON, examples = {
110+
@ExampleObject(name = "rawMessage", ref = "../apiexamples/raw_message_json") }) }) RawMessage rawMessage) throws ClientException;
111+
// @formatter:on
112+
100113
@GET
101114
@Path("/{channelId}/messages/{messageId}")
102115
@Operation(summary = "Retrieve a message by ID.")

‎server/src/main/java/com/mirth/connect/server/api/servlets/MessageServlet.java‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import com.mirth.connect.donkey.server.channel.ChannelException;
4646
import com.mirth.connect.donkey.server.channel.DispatchResult;
4747
import com.mirth.connect.donkey.server.message.batch.BatchMessageException;
48+
import com.mirth.connect.donkey.server.message.batch.CollectingResponseHandler;
4849
import com.mirth.connect.model.MessageImportResult;
4950
import com.mirth.connect.model.ServerEvent;
5051
import com.mirth.connect.model.ServerEvent.Level;
@@ -133,6 +134,27 @@ public Long processMessage(final String channelId, final RawMessage rawMessage)
133134
return null;
134135
}
135136

137+
@Override
138+
@CheckAuthorizedChannelId
139+
public List<Long> processBatchMessage(final String channelId, final RawMessage rawMessage) {
140+
CollectingResponseHandler responseHandler = new CollectingResponseHandler();
141+
142+
try {
143+
engineController.dispatchRawMessage(channelId, rawMessage, true, true, responseHandler);
144+
145+
containerRequestContext.setProperty(ResponseCodeFilter.RESPONSE_CODE_PROPERTY, Response.Status.CREATED.getStatusCode());
146+
return responseHandler.getMessageIds();
147+
} catch (ChannelException e) {
148+
// Do nothing. An error should have been logged.
149+
} catch (BatchMessageException e) {
150+
logger.error("Error processing batch message for channel " + channelId, e);
151+
}
152+
153+
containerRequestContext.setProperty(ResponseCodeFilter.RESPONSE_CODE_PROPERTY, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
154+
155+
return null;
156+
}
157+
136158
@Override
137159
@CheckAuthorizedChannelId
138160
public Message getMessageContent(String channelId, Long messageId, List<Integer> metaDataIds) {

‎server/src/main/java/com/mirth/connect/server/controllers/DonkeyEngineController.java‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,11 @@ public Channel getDeployedChannel(String channelId) {
11191119

11201120
@Override
11211121
public DispatchResult dispatchRawMessage(String channelId, RawMessage rawMessage, boolean force, boolean canBatch) throws ChannelException, BatchMessageException {
1122+
return dispatchRawMessage(channelId, rawMessage, force, canBatch, new SimpleResponseHandler());
1123+
}
1124+
1125+
@Override
1126+
public DispatchResult dispatchRawMessage(String channelId, RawMessage rawMessage, boolean force, boolean canBatch, ResponseHandler responseHandler) throws ChannelException, BatchMessageException {
11221127
if (!isDeployed(channelId)) {
11231128
ChannelException e = new ChannelException(true);
11241129
logger.error("Could not find channel to route to: " + channelId, e);
@@ -1133,7 +1138,6 @@ public DispatchResult dispatchRawMessage(String channelId, RawMessage rawMessage
11331138
} else {
11341139
BatchRawMessage batchRawMessage = new BatchRawMessage(new BatchMessageReader(rawMessage.getRawData()), rawMessage.getSourceMap());
11351140

1136-
ResponseHandler responseHandler = new SimpleResponseHandler();
11371141
sourceConnector.dispatchBatchMessage(batchRawMessage, responseHandler, rawMessage.getDestinationMetaDataIds());
11381142

11391143
return responseHandler.getResultForResponse();
@@ -1144,6 +1148,7 @@ public DispatchResult dispatchRawMessage(String channelId, RawMessage rawMessage
11441148
try {
11451149
dispatchResult = sourceConnector.dispatchRawMessage(rawMessage, force);
11461150
dispatchResult.setAttemptedResponse(true);
1151+
responseHandler.setDispatchResult(dispatchResult);
11471152
} finally {
11481153
sourceConnector.finishDispatch(dispatchResult);
11491154
}

‎server/src/main/java/com/mirth/connect/server/controllers/EngineController.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.mirth.connect.donkey.model.channel.DebugOptions;
2323
import com.mirth.connect.donkey.server.channel.DispatchResult;
2424
import com.mirth.connect.donkey.server.message.batch.BatchMessageException;
25+
import com.mirth.connect.donkey.server.message.batch.ResponseHandler;
2526
import com.mirth.connect.model.ChannelStatistics;
2627
import com.mirth.connect.model.DashboardStatus;
2728
import com.mirth.connect.model.ServerEventContext;
@@ -73,6 +74,9 @@ public interface EngineController {
7374

7475
public DispatchResult dispatchRawMessage(String channelId, RawMessage rawMessage, boolean force, boolean canBatch) throws ChannelException, BatchMessageException;
7576

77+
/** Dispatches a raw message, reporting each message it produced to {@code responseHandler}. */
78+
public DispatchResult dispatchRawMessage(String channelId, RawMessage rawMessage, boolean force, boolean canBatch, ResponseHandler responseHandler) throws ChannelException, BatchMessageException;
79+
7680
/**
7781
* Returns a list of DashboardStatus objects representing the running channels.
7882
*

0 commit comments

Comments
 (0)