Skip to content

Commit

Permalink
Merge 0802db3 into e7b08a5
Browse files Browse the repository at this point in the history
  • Loading branch information
allanmckenzie committed Mar 12, 2020
2 parents e7b08a5 + 0802db3 commit fcd31e0
Show file tree
Hide file tree
Showing 18 changed files with 442 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ on [Keep a CHANGELOG](http://keepachangelog.com/). This project adheres to
[Semantic Versioning](http://semver.org/).

## [Unreleased]
## [3.6.0] - 2020-03-12

### Added
- Command _`sendmessage`_ that sends the contents of a text file as a text message to the DLQ

## [3.5.0] - 2019-06-20

Expand Down
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,27 @@ Only messages that have the same JMSMessageId and different Consumer (_AMQ_ORIG_

`java -jar artemis-manager.jar browse @artemis.config -report created-at-name-total-report`

## Send a Text Message to the DLQ

* Sends a text message to the DLQ. The message must be contained in a text file, the path of which must be included in the command

**Note: SendMessage uses JMS to connect to the Artemis broker.**

`java -jar artemis-manager.jar sendmessage -messageFile path/to/some/text/file/containing/the/message.txt @artemis.config`

**Warning. In order to have the correct permissions to run this command, you will need to update your
broker.xml file in your Artemis configuration.**

Add these 2 lines to broker.xml directly under the

<security-settings/>
tag

<security-enabled>false</security-enabled>
<populate-validated-user>true</populate-validated-user>

For an example, look at src/test/resources/artemis/broker.xml

## Chaining Commands

* Chaining commands
Expand Down
11 changes: 6 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@
<artifactId>utilities-core</artifactId>
<version>${utilities.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>


<!-- Test dependencies -->
<dependency>
Expand All @@ -125,11 +130,6 @@
<artifactId>json-path-assert</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down Expand Up @@ -229,6 +229,7 @@
-Dcom.sun.management.jmxremote.ssl=false \
-Dcom.sun.management.jmxremote.authenticate=false
</javaOptions>
<configuration>${basedir}/src/test/resources/artemis</configuration>
</configuration>
</execution>
<execution>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ void setParameters(final List<String> jmxUrls,
final String jmsUrl,
final String jmsUsername,
final String jmsPassword) throws MalformedURLException;
}

String sendTextMessage(final String destinationName, final String message);
}
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,17 @@ public Map<String, Long> topicMessageCount(final Collection<String> topicNames)
.flatMap(m -> m.entrySet().stream())
.collect(groupingBy(Entry::getKey, summingLong(Entry::getValue)));
}
}

@Override
public String sendTextMessage(final String destinationName, final String message) {

return jmxProcessor
.processQueueControl(
jmxServiceUrls,
jmxEnvironment,
objectNameBuilder,
destinationName,
jmxManagement.sendTextMessage(message))
.collect(toList()).toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,17 @@ public Map<String, Long> topicMessageCount(final Collection<String> topicNames)
groupingBy(Entry::getKey,
summingLong(Entry::getValue)));
}
}

@Override
public String sendTextMessage(final String destinationName, final String message) {

return jmxProcessor
.processQueueControl(
jmxServiceUrls,
jmxEnvironment,
objectNameBuilder,
destinationName,
jmxManagement.sendTextMessage(message))
.collect(toList()).toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package uk.gov.justice.artemis.manager.connector.filehandling;

public class MessageFileException extends RuntimeException {

public MessageFileException(final String message) {
super(message);
}

public MessageFileException(final String message, final Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package uk.gov.justice.artemis.manager.connector.filehandling;

import static java.lang.String.format;
import static java.nio.charset.Charset.defaultCharset;
import static org.apache.commons.io.FileUtils.getFile;
import static org.apache.commons.io.FileUtils.readFileToString;

import java.io.File;

public class TextMessageFileContentsReader {

public String readContentsOf(final String pathToFile) {

try {
final File file = getFile(pathToFile);

if(! file.exists()) {
throw new MessageFileException(format("Failed to find file '%s'", file.getAbsolutePath()));
}

return readFileToString(file, defaultCharset());
} catch (final Exception e) {
throw new MessageFileException(format("Failed to read file '%s'", pathToFile), e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,15 @@ public JmxManagementFunction<Integer> reprocessAllMessages() {
return reprocessedMessageCount;
};
}

public JmxManagementFunction<String> sendTextMessage(final String message) {
return queueControl -> {
try {
return queueControl.sendTextMessage(message);
} catch (final Exception exception) {
outputPrinter.writeException(exception);
return format("%s: %s", exception.getClass().getSimpleName(), exception.getMessage());
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ abstract class AbstractArtemisCommand {
public static final String DEFAULT_BROKER_NAME = "default";
public static final String DEFAULT_JMS_URL = "tcp://localhost:61616?clientID=artemis-manager";

final OutputPrinter outputPrinter = new ConsolePrinter();
OutputPrinter outputPrinter = new ConsolePrinter();

ArtemisConnector artemisConnector = new CombinedJmsAndJmxArtemisConnector();

Expand All @@ -41,6 +41,9 @@ abstract class AbstractArtemisCommand {
@Parameter(names = "-jmsPassword", description = "JMS Password (optional)")
String jmsPassword;

@Parameter(names = "-messageFile", description = "The text message file that should be sent when calling sendmessage. If not calling sendmessage this parameter is not required")
String textMessageFile;

@Parameter(names = "-help", help = true)
private boolean help;

Expand All @@ -53,4 +56,4 @@ public void setup() throws MalformedURLException {
this.jmsUsername,
this.jmsPassword);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package uk.gov.justice.framework.tools.command;


import uk.gov.justice.artemis.manager.connector.filehandling.MessageFileException;
import uk.gov.justice.artemis.manager.connector.filehandling.TextMessageFileContentsReader;
import uk.gov.justice.framework.tools.common.command.ShellCommand;

public class SendMessage extends AbstractArtemisCommand implements ShellCommand {

private final TextMessageFileContentsReader textMessageFileContentsReader;

public SendMessage() {
this(new TextMessageFileContentsReader());
}

public SendMessage(final TextMessageFileContentsReader textMessageFileContentsReader) {
this.textMessageFileContentsReader = textMessageFileContentsReader;
}

@Override
public void run(final String[] args) {

try {
super.setup();

if(textMessageFile == null || textMessageFile.isEmpty()) {
throw new MessageFileException("Cannot read message file. No file name set. Please use -messageFile parameter when calling this command");
}

final String message = textMessageFileContentsReader.readContentsOf(textMessageFile);

final String results = artemisConnector.sendTextMessage("DLQ", message);
outputPrinter.write(results);

} catch (final Exception exception) {
outputPrinter.writeStackTrace(exception);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
import java.util.Optional;
import java.util.Set;

import com.beust.jcommander.JCommander;
import org.reflections.Reflections;
import org.slf4j.Logger;

import com.beust.jcommander.JCommander;

public class Bootstrap {

private static final Logger LOGGER = getLogger(Bootstrap.class);
Expand Down Expand Up @@ -60,4 +59,4 @@ private void createInstanceAndAddToJCommander(final Class<? extends ShellCommand
LOGGER.error("Unable to create instance of {}", commandClass.getName());
}
}
}
}
22 changes: 22 additions & 0 deletions src/test/java/uk/gov/justice/artemis/manager/ArtemisManagerIT.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package uk.gov.justice.artemis.manager;

import static com.jayway.jsonassert.JsonAssert.with;
import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
Expand Down Expand Up @@ -42,6 +43,7 @@ public class ArtemisManagerIT {
private static final String COMMAND_LINE_REMOVE = "env -u _JAVA_OPTIONS java -jar target/artemis-manager.jar remove -brokerName 0.0.0.0 -jmxUrl service:jmx:rmi:///jndi/rmi://localhost:3000/jmxrmi -jmsUrl tcp://localhost:61616?clientID=artemis-manager";
private static final String COMMAND_LINE_REMOVE_ALL_DUPLICATES = "env -u _JAVA_OPTIONS java -jar target/artemis-manager.jar removeallduplicates -brokerName 0.0.0.0 -jmxUrl service:jmx:rmi:///jndi/rmi://localhost:3000/jmxrmi -jmsUrl tcp://localhost:61616?clientID=artemis-manager";
private static final String COMMAND_LINE_DEDUPLICATE_TOPIC_MESSAGES = "env -u _JAVA_OPTIONS java -jar target/artemis-manager.jar deduplicatetopicmessages -brokerName 0.0.0.0 -jmxUrl service:jmx:rmi:///jndi/rmi://localhost:3000/jmxrmi -jmsUrl tcp://localhost:61616?clientID=artemis-manager";
private static final String COMMAND_LINE_SEND_TEXT_MESSAGE = "env -u _JAVA_OPTIONS java -jar target/artemis-manager.jar sendmessage -messageFile src/test/resources/messages/messageForSendingToArtemis.txt -brokerName 0.0.0.0 -jmxUrl service:jmx:rmi:///jndi/rmi://localhost:3000/jmxrmi -jmsUrl tcp://localhost:61616?clientID=artemis-manager";

@BeforeClass
public static void beforeClass() throws JMSException {
Expand All @@ -53,6 +55,26 @@ public static void afterClass() throws JMSException {
closeJmsConnection();
}

@Test
public void shouldSendTextMessage() throws Exception {

cleanQueue(DLQ);

final Output output = execute(COMMAND_LINE_SEND_TEXT_MESSAGE);

assertThat(output.errorOutput(), isEmptyString());

final Output browseOutput = execute(COMMAND_LINE_BROWSE);

assertThat(browseOutput.errorOutput(), isEmptyString());

final String messageText = browseOutput.standardOutput();
final String messageJson = messageText.replace("[", "").replace("]", "");

with(messageJson)
.assertThat("$.msgContent.message", is("All your base are belong to us"));
}

@Test
public void shouldBrowseMessagesInDLQ() throws Exception {
cleanQueue(DLQ);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,4 @@ public void shouldReturnTopicsAndCounts() throws Exception {
throw e;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package uk.gov.justice.artemis.manager.connector.filehandling;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class TextMessageFileContentsReaderTest {

@InjectMocks
private TextMessageFileContentsReader textMessageFileContentsReader;

@Test
public void shouldReadContentsOfAMessageFileAsString() throws Exception {

final String pathToFile = "src/test/resources/messages/messageForSendingToArtemis.txt";

final String message = textMessageFileContentsReader.readContentsOf(pathToFile);

assertThat(message, is("{\n \"message\": \"All your base are belong to us\"\n}\n"));
}

@Test
public void shouldFailIfFileNotFound() throws Exception {

final String pathToFile = "this/file/does/not/exist.txt";

try {
textMessageFileContentsReader.readContentsOf(pathToFile);
fail();
} catch (final MessageFileException expected) {
assertThat(expected.getMessage(), is("Failed to read file 'this/file/does/not/exist.txt'"));
}
}
}
Loading

0 comments on commit fcd31e0

Please sign in to comment.