Skip to content

Commit

Permalink
Fixes #43 - batch messages can be enriched with fixed JMS properties …
Browse files Browse the repository at this point in the history
…such as JMSType, JMSCorrelationId, JMSReplyTo etc
  • Loading branch information
northlander committed Dec 28, 2020
1 parent 5b585a7 commit 8db63ab
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
50 changes: 28 additions & 22 deletions src/main/java/co/nordlander/a/A.java
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,33 @@ protected void putData(final String data, final CommandLine cmdLine) throws IOEx
outMsg.setBooleanProperty((String) p.getKey(), Boolean.parseBoolean((String)p.getValue()));
}

populateJmsProperties(outMsg, mp);

boolean useScript = cmdLine.hasOption(CMD_TRANSFORM_SCRIPT);
final String script = cmdLine.getOptionValue(CMD_TRANSFORM_SCRIPT);

// send multiple messages?
if (cmdLine.hasOption("c")) {
int count = Integer.parseInt(cmdLine.getOptionValue("c"));
for (int i = 0; i < count; i++) {
final Message finalMsg = useScript ? transformMessage(outMsg, script) : outMsg;
mp.send(finalMsg);
}
output("", count, " messages sent");
} else if (cmdLine.hasOption(CMD_BATCH_FILE)) {
if (!useScript) {
output("Batch put must be used with script");
} else {
putBatchMessage(script, cmdLine.getOptionValue(CMD_BATCH_FILE), outMsg, mp);
}
} else {
final Message finalMsg = useScript ? transformMessage(outMsg, script) : outMsg;
mp.send(finalMsg);
}
}

// Fixed message properties must be parsed and set.
private void populateJmsProperties(Message outMsg, MessageProducer mp) throws JMSException {
if (cmdLine.hasOption("r")) {
outMsg.setJMSReplyTo(createDestination(cmdLine.getOptionValue("r")));
}
Expand Down Expand Up @@ -685,28 +712,6 @@ protected void putData(final String data, final CommandLine cmdLine) throws IOEx
if (cmdLine.hasOption(CMD_JMS_TYPE)) {
outMsg.setJMSType(cmdLine.getOptionValue(CMD_JMS_TYPE));
}

boolean useScript = cmdLine.hasOption(CMD_TRANSFORM_SCRIPT);
final String script = cmdLine.getOptionValue(CMD_TRANSFORM_SCRIPT);

// send multiple messages?
if (cmdLine.hasOption("c")) {
int count = Integer.parseInt(cmdLine.getOptionValue("c"));
for (int i = 0; i < count; i++) {
final Message finalMsg = useScript ? transformMessage(outMsg, script) : outMsg;
mp.send(finalMsg);
}
output("", count, " messages sent");
} else if (cmdLine.hasOption(CMD_BATCH_FILE)) {
if (!useScript) {
output("Batch put must be used with script");
} else {
putBatchMessage(script, cmdLine.getOptionValue(CMD_BATCH_FILE), outMsg, mp);
}
} else {
final Message finalMsg = useScript ? transformMessage(outMsg, script) : outMsg;
mp.send(finalMsg);
}
}

private void putBatchMessage(String script, String batchFile, Message outMsg, MessageProducer mp) {
Expand All @@ -716,6 +721,7 @@ private void putBatchMessage(String script, String batchFile, Message outMsg, Me
for (String line : lines) {
transformer.getContext().put("entry", line);
final Message finalMsg = transformMessage(outMsg, script);
populateJmsProperties(finalMsg, mp);
mp.send(finalMsg);
}
output(lines.length + " messages sent");
Expand Down
10 changes: 8 additions & 2 deletions src/test/java/co/nordlander/a/BaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -733,16 +733,22 @@ public void testBatch() throws Exception {
String script = "\"msg.body=msg.body.replace('PLACEHOLDER',entry);\"";

String cmdLine = getConnectCommand() + "-" + CMD_PUT + " \"test-PLACEHOLDER\" -" + CMD_BATCH_FILE + " "
+ batchFile.getAbsolutePath() + " -" + CMD_TRANSFORM_SCRIPT + " " + script + " TEST.QUEUE";
+ batchFile.getAbsolutePath() + " -" + CMD_TRANSFORM_SCRIPT + " " + script
+ " -" + CMD_JMS_TYPE + " foo.jmstype" + " -" + CMD_REPLY_TO + " foo.reply.to"
+ " -" + CMD_CORRELATION_ID + " foo.corr.id" + " TEST.QUEUE";

System.out.println("Testing cmd: " + cmdLine);
a.run(cmdLine.split(" "));

MessageConsumer mc = session.createConsumer(testQueue);
String[] entries = batchContent.split("\\n");
for (int i=0; i<entries.length; i++) {
TextMessage msg = (TextMessage) mc.receive(TEST_TIMEOUT);
assertNotNull(msg);
assertNotNull("A message is expected", msg);
assertEquals("test-" + entries[i], msg.getText());
assertEquals("foo.jmstype", msg.getJMSType());
assertNotNull("A reply queue is expected", msg.getJMSReplyTo());
assertEquals("foo.corr.id", msg.getJMSCorrelationID());
}

}
Expand Down

0 comments on commit 8db63ab

Please sign in to comment.