use of org.jowidgets.message.api.IExceptionCallback in project jo-client-platform by jo-source.
the class MessageBrokerTest method testPostWithServerError.
@Test
public void testPostWithServerError() throws ClientProtocolException, IOException, InterruptedException {
final String expectedResultMessage = "RESULT_MESSAGE";
final MessageStub requestMessage = new MessageStub(expectedResultMessage);
final int postErrorCount = 5;
final int messageCount = 5;
final HttpClientStub httpClient = new HttpClientStub(messageCount, 0);
httpClient.setPostStatus(HttpClientStub.INTERNAL_SERVER_ERROR_STATUS_LINE);
final ScheduledExecutorServiceMock executor = new ScheduledExecutorServiceMock();
final AtomicReference<JUnitLogger> senderThreadLoggerRef = new AtomicReference<JUnitLogger>(null);
final AtomicReference<JUnitLogger> receiverThreadLoggerRef = new AtomicReference<JUnitLogger>(null);
final MessageBrokerBuilder builder = new MessageBrokerBuilder(BROKER_ID);
builder.setUrl(DEFAULT_URL);
builder.setHttpClient(httpClient);
builder.setIncommingMessageExecutor(executor);
builder.setSleepDurationMillisAfterIoException(0);
builder.setHttpRequestInitializer(new IHttpRequestInitializer() {
@Override
public void initialize(final HttpRequest httpRequest) {
if (Thread.currentThread().getName().contains("messageSender")) {
prepareLoggerForThreadAndDisableConsole(senderThreadLoggerRef);
} else {
prepareLoggerForThreadAndDisableConsole(receiverThreadLoggerRef);
}
}
});
final IMessageBroker messageBroker = builder.build();
final IMessageReceiver receiver = Mockito.mock(IMessageReceiver.class);
messageBroker.setReceiver(receiver);
for (int i = 0; i < postErrorCount; i++) {
messageBroker.getChannel().send(requestMessage, null);
}
while (httpClient.getPostInvocationCount() < postErrorCount) {
// do nothing
}
final IExceptionCallback exceptionCallback = Mockito.mock(IExceptionCallback.class);
for (int i = 0; i < postErrorCount; i++) {
messageBroker.getChannel().send(requestMessage, exceptionCallback);
}
while (httpClient.getPostInvocationCount() < postErrorCount * 2) {
// do nothing
}
// now fix error and send some messages with success
httpClient.setPostStatus(HttpClientStub.OK_STATUS_LINE);
for (int i = 0; i < messageCount; i++) {
messageBroker.getChannel().send(requestMessage, exceptionCallback);
}
Assert.assertTrue(httpClient.awaitMessagesConsumed(SYNC_TIMEOUT, TimeUnit.MILLISECONDS));
executor.executeEvents();
Mockito.verify(receiver, Mockito.times(messageCount)).onMessage(expectedResultMessage, messageBroker.getChannel());
Assert.assertTrue(messageBroker.shutdown(SYNC_TIMEOUT));
Mockito.verify(httpClient.getConnectionManager(), Mockito.times(1)).shutdown();
Assert.assertEquals(2 * postErrorCount + messageCount, httpClient.getPostInvocationCount());
Assert.assertEquals(postErrorCount, senderThreadLoggerRef.get().getMessageCount());
Mockito.verify(exceptionCallback, Mockito.times(postErrorCount)).exception(Mockito.any(Exception.class));
Assert.assertEquals(0, receiverThreadLoggerRef.get().getMessageCount());
}
use of org.jowidgets.message.api.IExceptionCallback in project jo-client-platform by jo-source.
the class InvocationServerMessageReceiver method onMessage.
@Override
public void onMessage(final Object message, final IMessageChannel replyChannel) {
if (message instanceof MethodInvocationMessage) {
final MethodInvocationMessage invocationMessage = (MethodInvocationMessage) message;
final Object invocationId = invocationMessage.getInvocationId();
invocationServer.registerInvocation(invocationId, replyChannel);
final IExceptionCallback exceptionCallback = new IExceptionCallback() {
@Override
public void exception(final Throwable throwable) {
invocationServer.unregisterInvocation(invocationId);
invocationServerServiceRegistry.onCancel(invocationId);
}
};
replyChannel.send(new AcknowledgeMessage(invocationId), exceptionCallback);
invocationServerServiceRegistry.onMethodInvocation((MethodInvocationMessage) message);
} else if (message instanceof CancelMessage) {
final CancelMessage cancelMessage = (CancelMessage) message;
invocationServerServiceRegistry.onCancel(cancelMessage);
invocationServer.unregisterInvocation(cancelMessage.getInvocationId());
} else if (message instanceof ResponseMessage) {
invocationServerServiceRegistry.onResponse((ResponseMessage) message);
}
}
Aggregations