Skip to content

Commit fda398e

Browse files
Add log runtime id, replay event to listener and indexer commands
1 parent 900772e commit fda398e

File tree

42 files changed

+1156
-180
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1156
-180
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ on [Keep a CHANGELOG](http://keepachangelog.com/). This project adheres to
55

66
## [Unreleased]
77

8+
## [8.0.8] - 2024-04-17
9+
### Added
10+
- Add LOG_RUNTIME_ID system command and it's handler to log command runtime id
11+
- Add REPLAY_EVENT_TO_EVENT_LISTENER and REPLAY_EVENT_TO_EVENT_INDEXER system commands to replay single event
12+
813
## [8.0.7] - 2024-01-11
914
### Changed
1015
- Update to framework-libraries to 8.0.7

framework-jmx-command-client/runSystemCommand.sh

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,18 @@ if [ -z "$1" ]; then
2727
echo "Listing commands"
2828
echo
2929
java -jar target/framework-jmx-command-client-${FRAMEWORK_JMX_COMMAND_CLIENT_VERSION}.jar -l -u "$USER_NAME" -pw "$PASSWORD" -cn "$CONTEXT_NAME"
30+
elif [ "$1" == "--help" ]; then
31+
java -jar target/framework-jmx-command-client-${FRAMEWORK_JMX_COMMAND_CLIENT_VERSION}.jar --help -u "$USER_NAME" -pw "$PASSWORD" -cn "$CONTEXT_NAME"
32+
fi
33+
34+
COMMAND=$1
35+
36+
if [ ! -z "$2" ]; then
37+
COMMAND_RUNTIME_ID=$2
38+
echo "Running command '$COMMAND' with command runtime id '$COMMAND_RUNTIME_ID'"
39+
echo
40+
java -jar target/framework-jmx-command-client-${FRAMEWORK_JMX_COMMAND_CLIENT_VERSION}.jar -c "$COMMAND" -crid "$COMMAND_RUNTIME_ID" -u "$USER_NAME" -pw "$PASSWORD" -cn "$CONTEXT_NAME"
3041
else
31-
COMMAND=$1
3242
echo "Running command '$COMMAND'"
3343
echo
3444
java -jar target/framework-jmx-command-client-${FRAMEWORK_JMX_COMMAND_CLIENT_VERSION}.jar -c "$COMMAND" -u "$USER_NAME" -pw "$PASSWORD" -cn "$CONTEXT_NAME"

framework-jmx-command-client/src/main/java/uk/gov/justice/framework/command/client/CommandExecutor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public void executeCommand(
2828
commandPrinter.printSystemCommands(systemCommandDetails);
2929
} else {
3030
final String commandName = commandLine.getOptionValue("command");
31-
systemCommandInvoker.runSystemCommand(commandName, jmxParameters);
31+
final String commandRuntimeId = commandLine.getOptionValue("crid");
32+
systemCommandInvoker.runSystemCommand(commandName, commandRuntimeId, jmxParameters);
3233
}
3334
}
3435
}

framework-jmx-command-client/src/main/java/uk/gov/justice/framework/command/client/cdi/producers/OptionsFactory.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public Options createOptions() {
99

1010
options.addOption("cn", "context-name", true, "The name of the context on which to run the command. Required");
1111
options.addOption("c", "command", true, "Framework command to execute. Run with --list for a list of all commands");
12+
options.addOption("crid", "command-runtime-id", true, "Optional command runtime id. Required if your command requires (for example) an eventId");
1213
options.addOption("h", "host", true, "Hostname or IP address of the Wildfly server. Defaults to localhost");
1314
options.addOption("p", "port", true, "Wildfly management port. Defaults to 9990");
1415
options.addOption("u", "username", true, "Optional username for Wildfly management security");

framework-jmx-command-client/src/main/java/uk/gov/justice/framework/command/client/jmx/SystemCommandInvoker.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package uk.gov.justice.framework.command.client.jmx;
22

3+
import org.apache.commons.lang3.StringUtils;
4+
import uk.gov.justice.framework.command.client.CommandLineException;
35
import uk.gov.justice.framework.command.client.io.ToConsolePrinter;
46
import uk.gov.justice.services.jmx.api.SystemCommandInvocationFailedException;
57
import uk.gov.justice.services.jmx.api.UnrunnableSystemCommandException;
@@ -8,10 +10,9 @@
810
import uk.gov.justice.services.jmx.system.command.client.SystemCommanderClientFactory;
911
import uk.gov.justice.services.jmx.system.command.client.connection.JmxParameters;
1012

11-
import java.util.UUID;
12-
1313
import javax.enterprise.context.ApplicationScoped;
1414
import javax.inject.Inject;
15+
import java.util.UUID;
1516

1617
@ApplicationScoped
1718
public class SystemCommandInvoker {
@@ -25,7 +26,7 @@ public class SystemCommandInvoker {
2526
@Inject
2627
private ToConsolePrinter toConsolePrinter;
2728

28-
public void runSystemCommand(final String commandName, final JmxParameters jmxParameters) {
29+
public void runSystemCommand(final String commandName, final String commandRuntimeId, final JmxParameters jmxParameters) {
2930

3031
final String contextName = jmxParameters.getContextName();
3132

@@ -39,17 +40,36 @@ public void runSystemCommand(final String commandName, final JmxParameters jmxPa
3940
toConsolePrinter.printf("Connected to %s context", contextName);
4041

4142
final SystemCommanderMBean systemCommanderMBean = systemCommanderClient.getRemote(contextName);
42-
final UUID commandId = systemCommanderMBean.call(commandName);
43+
final UUID commandId = invoke(systemCommanderMBean, commandName, commandRuntimeId);
4344
toConsolePrinter.printf("System command '%s' with id '%s' successfully sent to %s", commandName, commandId, contextName);
4445
commandPoller.runUntilComplete(systemCommanderMBean, commandId, commandName);
4546

4647
} catch (final UnrunnableSystemCommandException e) {
4748
toConsolePrinter.printf("The command '%s' is not supported on this %s context", commandName, contextName);
4849
throw e;
50+
} catch (final CommandLineException e) {
51+
toConsolePrinter.printf("The command '%s' failed: %s", commandName, e.getMessage());
52+
throw e;
4953
} catch (final SystemCommandInvocationFailedException e) {
50-
toConsolePrinter.printf("The command '%s' failed: %s", e.getMessage(), commandName);
54+
toConsolePrinter.printf("The command '%s' failed: %s", commandName, e.getMessage());
5155
toConsolePrinter.println(e.getServerStackTrace());
5256
throw e;
5357
}
5458
}
59+
60+
private UUID invoke(SystemCommanderMBean systemCommanderMBean, String commandName, String commandRuntimeId) {
61+
if(StringUtils.isEmpty(commandRuntimeId)) {
62+
return systemCommanderMBean.call(commandName);
63+
} else {
64+
return systemCommanderMBean.callWithRuntimeId(commandName, convertToUUID(commandRuntimeId));
65+
}
66+
}
67+
68+
private UUID convertToUUID(String id) {
69+
try{
70+
return UUID.fromString(id);
71+
} catch(IllegalArgumentException e) {
72+
throw new CommandLineException("Unable to invoke command as supplied commandRuntimeId is not uuid format: " + id, e);
73+
}
74+
}
5575
}

framework-jmx-command-client/src/main/java/uk/gov/justice/framework/command/client/startup/Bootstrapper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import org.jboss.weld.environment.se.WeldContainer;
88
import org.jboss.weld.inject.WeldInstance;
99

10+
import java.util.UUID;
11+
1012
public class Bootstrapper {
1113

1214
private final WeldFactory weldFactory;
@@ -32,6 +34,7 @@ public ReturnCode startContainerAndRun(final String[] args) {
3234
public static void main(String[] args) {
3335

3436
final String command = "PING";
37+
final String commandRuntimeId = "";
3538
final String contextName = "people";
3639

3740
final String userName = "admin";
@@ -40,6 +43,7 @@ public static void main(String[] args) {
4043

4144
final String[] arguments = {
4245
"-c", command,
46+
"-rcid", commandRuntimeId,
4347
"-u", userName,
4448
"-pw", password,
4549
"-cn", contextName

framework-jmx-command-client/src/test/java/uk/gov/justice/framework/command/client/CommandExecutorTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public class CommandExecutorTest {
4141
public void shouldLookupSystemCommandByNameAndExecute() throws Exception {
4242

4343
final String commandName = "CATCHUP";
44+
final String commandRuntimeId = "SOME_ID";
4445

4546
final SystemCommandDetails systemCommandDetails_1 = mock(SystemCommandDetails.class);
4647
final SystemCommandDetails systemCommandDetails_2 = mock(SystemCommandDetails.class);
@@ -51,10 +52,11 @@ public void shouldLookupSystemCommandByNameAndExecute() throws Exception {
5152

5253
when(commandLine.hasOption("list")).thenReturn(false);
5354
when(commandLine.getOptionValue("command")).thenReturn(commandName);
55+
when(commandLine.getOptionValue("crid")).thenReturn(commandRuntimeId);
5456

5557
commandExecutor.executeCommand(commandLine, jmxParameters, systemCommands);
5658

57-
verify(systemCommandInvoker).runSystemCommand(commandName, jmxParameters);
59+
verify(systemCommandInvoker).runSystemCommand(commandName, commandRuntimeId, jmxParameters);
5860
}
5961

6062
@Test

framework-jmx-command-client/src/test/java/uk/gov/justice/framework/command/client/cdi/producers/OptionsFactoryTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@ public void shouldCreateNewOptionsObjectConfiguredWithTheCorrectCommandLineParam
2727

2828
final Collection<Option> allOptions = options.getOptions();
2929

30-
assertThat(allOptions.size(), is(8));
30+
assertThat(allOptions.size(), is(9));
3131

3232
assertThat(allOptions, hasItem(new Option("help", false, "Show help.")));
3333
assertThat(allOptions, hasItem(new Option("cn", "context-name", true, "The name of the context on which to run the command. Required")));
3434
assertThat(allOptions, hasItem(new Option("c", "command", true, "Framework command to execute. Run with --list for a list of all commands")));
35+
assertThat(allOptions, hasItem(new Option("crid", "command-runtime-id", true, "Optional command runtime id. Required if your command requires (for example) an eventId")));
3536
assertThat(allOptions, hasItem(new Option("h", "host", true, "Hostname or IP address of the Wildfly server. Defaults to localhost")));
3637
assertThat(allOptions, hasItem(new Option("p", "port", true, "Wildfly management port. Defaults to 9990")));
3738
assertThat(allOptions, hasItem(new Option("u", "username", true, "Optional username for Wildfly management security")));

framework-jmx-command-client/src/test/java/uk/gov/justice/framework/command/client/jmx/SystemCommandInvokerTest.java

Lines changed: 76 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
import static java.util.Optional.empty;
44
import static java.util.Optional.of;
55
import static java.util.UUID.randomUUID;
6-
import static org.mockito.Mockito.doThrow;
7-
import static org.mockito.Mockito.inOrder;
8-
import static org.mockito.Mockito.mock;
9-
import static org.mockito.Mockito.when;
6+
import static org.hamcrest.CoreMatchers.is;
7+
import static org.junit.Assert.assertThat;
8+
import static org.junit.Assert.assertThrows;
9+
import static org.mockito.Mockito.*;
1010

11+
import org.hamcrest.CoreMatchers;
12+
import uk.gov.justice.framework.command.client.CommandLineException;
1113
import uk.gov.justice.framework.command.client.io.ToConsolePrinter;
1214
import uk.gov.justice.services.jmx.api.SystemCommandInvocationFailedException;
1315
import uk.gov.justice.services.jmx.api.UnrunnableSystemCommandException;
@@ -42,7 +44,7 @@ public class SystemCommandInvokerTest {
4244
private SystemCommandInvoker systemCommandInvoker;
4345

4446
@Test
45-
public void shouldMakeJmxCallToRetrieveTheListOfCommands() throws Exception {
47+
public void shouldInvokeMbeanWithCommandNameGivenCommandRuntimeIdNotSupplied() {
4648

4749
final String contextName = "my-context";
4850
final String host = "localhost";
@@ -62,7 +64,7 @@ public void shouldMakeJmxCallToRetrieveTheListOfCommands() throws Exception {
6264
when(systemCommanderClient.getRemote(contextName)).thenReturn(systemCommanderMBean);
6365
when(systemCommanderMBean.call(commandName)).thenReturn(commandId);
6466

65-
systemCommandInvoker.runSystemCommand(commandName, jmxParameters);
67+
systemCommandInvoker.runSystemCommand(commandName, null, jmxParameters);
6668

6769
final InOrder inOrder = inOrder(
6870
toConsolePrinter,
@@ -81,6 +83,71 @@ public void shouldMakeJmxCallToRetrieveTheListOfCommands() throws Exception {
8183
inOrder.verify(commandPoller).runUntilComplete(systemCommanderMBean, commandId, commandName);
8284
}
8385

86+
@Test
87+
public void shouldInvokeMbeanWithCommandNameAndRuntimeIdGivenCommandRuntimeIdIsSupplied() {
88+
final String contextName = "my-context";
89+
final String host = "localhost";
90+
final int port = 92834;
91+
final String commandName = "SOME_COMMAND";
92+
final UUID commandRuntimeId = UUID.randomUUID();
93+
final UUID commandId = randomUUID();
94+
95+
final JmxParameters jmxParameters = mock(JmxParameters.class);
96+
final SystemCommanderClient systemCommanderClient = mock(SystemCommanderClient.class);
97+
final SystemCommanderMBean systemCommanderMBean = mock(SystemCommanderMBean.class);
98+
99+
when(jmxParameters.getContextName()).thenReturn(contextName);
100+
when(jmxParameters.getHost()).thenReturn(host);
101+
when(jmxParameters.getPort()).thenReturn(port);
102+
when(jmxParameters.getCredentials()).thenReturn(empty());
103+
when(systemCommanderClientFactory.create(jmxParameters)).thenReturn(systemCommanderClient);
104+
when(systemCommanderClient.getRemote(contextName)).thenReturn(systemCommanderMBean);
105+
when(systemCommanderMBean.callWithRuntimeId(commandName, commandRuntimeId)).thenReturn(commandId);
106+
107+
systemCommandInvoker.runSystemCommand(commandName, commandRuntimeId.toString(), jmxParameters);
108+
109+
final InOrder inOrder = inOrder(
110+
toConsolePrinter,
111+
systemCommanderClientFactory,
112+
systemCommanderClient,
113+
systemCommanderMBean,
114+
commandPoller);
115+
116+
inOrder.verify(toConsolePrinter).printf("Running system command '%s'", commandName);
117+
inOrder.verify(toConsolePrinter).printf("Connecting to %s context at '%s' on port %d", contextName, host, port);
118+
inOrder.verify(systemCommanderClientFactory).create(jmxParameters);
119+
inOrder.verify(toConsolePrinter).printf("Connected to %s context", contextName);
120+
inOrder.verify(systemCommanderClient).getRemote(contextName);
121+
inOrder.verify(systemCommanderMBean).callWithRuntimeId(commandName, commandRuntimeId);
122+
inOrder.verify(toConsolePrinter).printf("System command '%s' with id '%s' successfully sent to %s", commandName, commandId, contextName);
123+
inOrder.verify(commandPoller).runUntilComplete(systemCommanderMBean, commandId, commandName);
124+
}
125+
126+
@Test
127+
public void shouldLogMessageAndThrowExceptionWhenSuppliedCommandRuntimeIdIsNotUUID() {
128+
final String contextName = "my-context";
129+
final String host = "localhost";
130+
final int port = 92834;
131+
final String commandName = "SOME_COMMAND";
132+
final String commandRuntimeId = "NOT_UUID_INVALID";
133+
final UUID commandId = randomUUID();
134+
135+
final JmxParameters jmxParameters = mock(JmxParameters.class);
136+
final SystemCommanderClient systemCommanderClient = mock(SystemCommanderClient.class);
137+
final SystemCommanderMBean systemCommanderMBean = mock(SystemCommanderMBean.class);
138+
139+
when(jmxParameters.getContextName()).thenReturn(contextName);
140+
when(jmxParameters.getHost()).thenReturn(host);
141+
when(jmxParameters.getPort()).thenReturn(port);
142+
when(jmxParameters.getCredentials()).thenReturn(empty());
143+
when(systemCommanderClientFactory.create(jmxParameters)).thenReturn(systemCommanderClient);
144+
when(systemCommanderClient.getRemote(contextName)).thenReturn(systemCommanderMBean);
145+
when(systemCommanderMBean.call(commandName)).thenReturn(commandId);
146+
147+
final CommandLineException e = assertThrows(CommandLineException.class, () -> systemCommandInvoker.runSystemCommand(commandName, commandRuntimeId, jmxParameters));
148+
assertThat(e.getMessage(), is("Unable to invoke command as supplied commandRuntimeId is not uuid format: " + commandRuntimeId));
149+
}
150+
84151
@Test
85152
public void shouldLogIfUsingCredentials() throws Exception {
86153

@@ -105,7 +172,7 @@ public void shouldLogIfUsingCredentials() throws Exception {
105172
when(systemCommanderClient.getRemote(contextName)).thenReturn(systemCommanderMBean);
106173
when(systemCommanderMBean.call(commandName)).thenReturn(commandId);
107174

108-
systemCommandInvoker.runSystemCommand(commandName, jmxParameters);
175+
systemCommandInvoker.runSystemCommand(commandName, null, jmxParameters);
109176

110177
final InOrder inOrder = inOrder(
111178
toConsolePrinter,
@@ -150,7 +217,7 @@ public void shouldLogIfTheCommandIsUnsupported() throws Exception {
150217
when(systemCommanderClient.getRemote(contextName)).thenReturn(systemCommanderMBean);
151218
doThrow(unrunnableSystemCommandException).when(systemCommanderMBean).call(commandName);
152219

153-
systemCommandInvoker.runSystemCommand(commandName, jmxParameters);
220+
systemCommandInvoker.runSystemCommand(commandName, "", jmxParameters);
154221

155222
final InOrder inOrder = inOrder(
156223
toConsolePrinter,
@@ -191,7 +258,7 @@ public void shouldLogAndPrintTheServerStackTraceIfTheCommandFails() throws Excep
191258
when(systemCommanderClient.getRemote(contextName)).thenReturn(systemCommanderMBean);
192259
doThrow(systemCommandInvocationFailedException).when(systemCommanderMBean).call(commandName);
193260

194-
systemCommandInvoker.runSystemCommand(commandName, jmxParameters);
261+
systemCommandInvoker.runSystemCommand(commandName, null, jmxParameters);
195262

196263
final InOrder inOrder = inOrder(
197264
toConsolePrinter,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package uk.gov.justice.services.management.ping.commands;
2+
3+
import uk.gov.justice.services.jmx.api.command.BaseSystemCommand;
4+
5+
public class LogRuntimeIdCommand extends BaseSystemCommand {
6+
7+
public static final String LOG_RUNTIME_ID = "LOG_RUNTIME_ID";
8+
public static final String DESCRIPTION = "Logs to console any uuid set from the JmxClient using the switch";
9+
10+
public LogRuntimeIdCommand() {
11+
super(LOG_RUNTIME_ID, DESCRIPTION);
12+
}
13+
14+
@Override
15+
public boolean requiresCommandRuntimeId() {
16+
return true;
17+
}
18+
19+
@Override
20+
public String commandRuntimeIdType() {
21+
return "command-runtime-id";
22+
}
23+
}

0 commit comments

Comments
 (0)