Skip to content

Commit

Permalink
Spring Cloud Command Router implementation
Browse files Browse the repository at this point in the history
Apparently a Spring RestController doesn't like it when you return null, hence we return "" instead when a dispatch without callback has succeeded.
Additionally change the 'receiveCommand(...)' method to return a CompletableFuture.completedFuture() in every if/else/catch block rather than creating a
CompletableFuture at the start of the function and calling 'complet(...)' on it
  • Loading branch information
stevenb committed Nov 24, 2016
1 parent b7dc7ae commit 8eca6c9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
Expand Up @@ -102,28 +102,26 @@ public Registration subscribe(String commandName, MessageHandler<? super Command
public <C, R> CompletableFuture<?> receiveCommand( public <C, R> CompletableFuture<?> receiveCommand(
@RequestBody SpringHttpDispatchMessage<C> dispatchMessage) throws ExecutionException, InterruptedException { @RequestBody SpringHttpDispatchMessage<C> dispatchMessage) throws ExecutionException, InterruptedException {
CommandMessage<C> commandMessage = dispatchMessage.getCommandMessage(serializer); CommandMessage<C> commandMessage = dispatchMessage.getCommandMessage(serializer);
CompletableFuture<SpringHttpReplyMessage<R>> result = new CompletableFuture<>();

if (dispatchMessage.isExpectReply()) { if (dispatchMessage.isExpectReply()) {
try { try {
SpringHttpReplyFutureCallback<C, R> replyFutureCallback = new SpringHttpReplyFutureCallback<>(); SpringHttpReplyFutureCallback<C, R> replyFutureCallback = new SpringHttpReplyFutureCallback<>();
localCommandBus.dispatch(commandMessage, replyFutureCallback); localCommandBus.dispatch(commandMessage, replyFutureCallback);
return replyFutureCallback; return replyFutureCallback;
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("Could not dispatch command", e); LOGGER.error("Could not dispatch command", e);
result.complete(new SpringHttpReplyMessage<>(commandMessage.getIdentifier(), null, e, serializer)); return CompletableFuture.completedFuture(
new SpringHttpReplyMessage<>(commandMessage.getIdentifier(), null, e, serializer));
} }
} else { } else {
try { try {
localCommandBus.dispatch(commandMessage); localCommandBus.dispatch(commandMessage);
result.complete(null); return CompletableFuture.completedFuture("");
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("Could not dispatch command", e); LOGGER.error("Could not dispatch command", e);
result.complete(new SpringHttpReplyMessage<>(commandMessage.getIdentifier(), null, e, serializer)); return CompletableFuture.completedFuture(
new SpringHttpReplyMessage<>(commandMessage.getIdentifier(), null, e, serializer));
} }
} }

return result;
} }


public class SpringHttpReplyFutureCallback<C, R> extends CompletableFuture<SpringHttpReplyMessage> public class SpringHttpReplyFutureCallback<C, R> extends CompletableFuture<SpringHttpReplyMessage>
Expand Down
Expand Up @@ -250,10 +250,9 @@ public void testReceiveCommandHandlesCommandWithCallbackFails() throws Exception


@Test @Test
public void testReceiveCommandHandlesCommandWithoutCallback() throws Exception { public void testReceiveCommandHandlesCommandWithoutCallback() throws Exception {
SpringHttpReplyMessage result = String result = (String) testSubject.receiveCommand(buildDispatchMessage(false)).get();
(SpringHttpReplyMessage) testSubject.receiveCommand(buildDispatchMessage(false)).get();


assertNull(result); assertEquals("", result);


verify(localCommandBus).dispatch(any()); verify(localCommandBus).dispatch(any());
} }
Expand Down

0 comments on commit 8eca6c9

Please sign in to comment.