Search in sources :

Example 1 with ICancelService

use of org.jowidgets.invocation.common.api.ICancelService in project jo-client-platform by jo-source.

the class InvocationClientImpl method getCancelService.

@Override
public ICancelService getCancelService() {
    return new ICancelService() {

        @Override
        public void canceled(final Object invocationId) {
            Assert.paramNotNull(invocationId, "invocationId");
            final TimeStampedObject<IMessageChannel> ackInvocation = acknowledgedInvocations.get(invocationId);
            if (ackInvocation != null) {
                final CancelMessage message = new CancelMessage(invocationId);
                ackInvocation.getObject().send(message, new ExceptionCallback(invocationClientServiceRegistry, invocationId));
                canceledInvocations.remove(invocationId);
            } else {
                canceledInvocations.put(invocationId, new TimeStampedObject<Object>(invocationId));
            }
        }
    };
}
Also used : CancelMessage(org.jowidgets.invocation.common.impl.CancelMessage) IMessageChannel(org.jowidgets.message.api.IMessageChannel) ICancelService(org.jowidgets.invocation.common.api.ICancelService)

Example 2 with ICancelService

use of org.jowidgets.invocation.common.api.ICancelService in project jo-client-platform by jo-source.

the class InvocationIntegrationTest method testCancelAfterMessageInvocation.

@Test
public void testCancelAfterMessageInvocation() {
    final String methodName = "Method";
    final Integer invocationId = Integer.valueOf(0);
    final String invocationParameter = "InvocationParameter";
    // setup server
    final ICancelService serverCancelService = Mockito.mock(ICancelService.class);
    InvocationServerToolkit.getRegistry(BROKER_ID).register(serverCancelService);
    final IMethod serverMethodMock = Mockito.mock(IMethod.class);
    InvocationServerToolkit.getRegistry(BROKER_ID).register(methodName, serverMethodMock);
    // setup client
    final IInvocationCallbackService clientInvocationCallback = Mockito.mock(IInvocationCallbackService.class);
    InvocationClientToolkit.getRegistry(BROKER_ID).register(clientInvocationCallback);
    final IInvocationClient client = InvocationClientToolkit.getClient(BROKER_ID);
    final IMethod clientMethod = client.getMethod(methodName);
    final ICancelService clientCancelService = client.getCancelService();
    // invoke methods on client
    clientMethod.invoke(invocationId, invocationParameter);
    Mockito.verify(serverMethodMock, Mockito.never()).invoke(invocationId, invocationParameter);
    messaging.getMessageReceiverBroker().dispatchMessages();
    Mockito.verify(serverMethodMock, Mockito.times(1)).invoke(invocationId, invocationParameter);
    clientCancelService.canceled(invocationId);
    Mockito.verify(serverCancelService, Mockito.never()).canceled(invocationId);
    messaging.getMessageReceiverBroker().dispatchMessages();
    Mockito.verify(serverCancelService, Mockito.times(1)).canceled(invocationId);
    assertNoLogErrorOrWarning();
}
Also used : IInvocationClient(org.jowidgets.invocation.client.api.IInvocationClient) IMethod(org.jowidgets.invocation.common.api.IMethod) ICancelService(org.jowidgets.invocation.common.api.ICancelService) IInvocationCallbackService(org.jowidgets.invocation.common.api.IInvocationCallbackService) Test(org.junit.Test)

Example 3 with ICancelService

use of org.jowidgets.invocation.common.api.ICancelService in project jo-client-platform by jo-source.

the class InvocationIntegrationTest method testCancelBeforeMethodInvocation.

@Test
public void testCancelBeforeMethodInvocation() {
    final String methodName = "Method";
    final Integer invocationId = Integer.valueOf(0);
    final String invocationParameter = "InvocationParameter";
    // setup server
    final ICancelService serverCancelService = Mockito.mock(ICancelService.class);
    InvocationServerToolkit.getRegistry(BROKER_ID).register(serverCancelService);
    final IMethod serverMethodMock = Mockito.mock(IMethod.class);
    InvocationServerToolkit.getRegistry(BROKER_ID).register(methodName, serverMethodMock);
    // setup client
    final IInvocationCallbackService clientInvocationCallback = Mockito.mock(IInvocationCallbackService.class);
    InvocationClientToolkit.getRegistry(BROKER_ID).register(clientInvocationCallback);
    final IInvocationClient client = InvocationClientToolkit.getClient(BROKER_ID);
    final IMethod clientMethod = client.getMethod(methodName);
    final ICancelService cancelService = client.getCancelService();
    // invoke methods on client
    clientMethod.invoke(invocationId, invocationParameter);
    cancelService.canceled(invocationId);
    Mockito.verify(serverMethodMock, Mockito.never()).invoke(invocationId, invocationParameter);
    Mockito.verify(serverCancelService, Mockito.never()).canceled(invocationId);
    messaging.getMessageReceiverBroker().dispatchMessages();
    Mockito.verify(serverMethodMock, Mockito.times(1)).invoke(invocationId, invocationParameter);
    Mockito.verify(serverCancelService, Mockito.times(1)).canceled(invocationId);
    assertNoLogErrorOrWarning();
}
Also used : IInvocationClient(org.jowidgets.invocation.client.api.IInvocationClient) IMethod(org.jowidgets.invocation.common.api.IMethod) ICancelService(org.jowidgets.invocation.common.api.ICancelService) IInvocationCallbackService(org.jowidgets.invocation.common.api.IInvocationCallbackService) Test(org.junit.Test)

Example 4 with ICancelService

use of org.jowidgets.invocation.common.api.ICancelService in project jo-client-platform by jo-source.

the class InvocationCallbackServiceImpl method registerInvocation.

Object registerInvocation(final IInvocationCallback<?> invocationCallback, final IInterimResponseCallback<?> interimResponseCallback, final IInterimRequestCallback<?, ?> interimRequestCallback, final long timeout, final IInvocationClient invocationClient) {
    final Object invocationId = UUID.randomUUID();
    final InvocationContext invocationContext = new InvocationContext(invocationCallback, interimResponseCallback, interimRequestCallback, timeout);
    if (invocationCallback != null) {
        invocationCallback.addCancelListener(new ICancelListener() {

            @Override
            public void canceled() {
                final ICancelService cancelService = invocationClient.getCancelService();
                if (cancelService != null) {
                    cancelService.canceled(invocationId);
                    invocationContexts.remove(invocationId);
                }
            }
        });
    }
    invocationContexts.put(invocationId, invocationContext);
    checkMapSize();
    return invocationId;
}
Also used : ICancelService(org.jowidgets.invocation.common.api.ICancelService) ICancelListener(org.jowidgets.invocation.service.common.api.ICancelListener)

Example 5 with ICancelService

use of org.jowidgets.invocation.common.api.ICancelService in project jo-client-platform by jo-source.

the class InvocationIntegrationTest method testCancelExceptionAfterCancel.

@Test
public void testCancelExceptionAfterCancel() {
    final String methodName = "Method";
    final Integer invocationId = Integer.valueOf(0);
    final String invocationParameter = "InvocationParameter";
    final RuntimeException cancelException = new RuntimeException("Service was canceled");
    final CountDownLatch cancelLatch = new CountDownLatch(1);
    final CountDownLatch serverFinsihedLatch = new CountDownLatch(1);
    // setup server
    final ICancelService serverCancelService = Mockito.mock(ICancelService.class);
    InvocationServerToolkit.getRegistry(BROKER_ID).register(serverCancelService);
    final IInvocationCallbackService serverInvocationCallback = InvocationServerToolkit.getServer(BROKER_ID).getInvocationCallbackService();
    final IMethod serverMethodMock = Mockito.mock(IMethod.class);
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(final InvocationOnMock invocation) throws Throwable {
            // wait until service was canceled
            cancelLatch.await();
            // after service was canceled return a cancel exception
            serverInvocationCallback.exeption(invocationId, cancelException);
            return null;
        }
    }).when(serverMethodMock).invoke(Mockito.any(), Mockito.any());
    InvocationServerToolkit.getRegistry(BROKER_ID).register(methodName, serverMethodMock);
    // setup client
    final IInvocationCallbackService clientInvocationCallback = Mockito.mock(IInvocationCallbackService.class);
    InvocationClientToolkit.getRegistry(BROKER_ID).register(clientInvocationCallback);
    final IInvocationClient client = InvocationClientToolkit.getClient(BROKER_ID);
    final IMethod clientMethod = client.getMethod(methodName);
    final ICancelService cancelService = client.getCancelService();
    // invoke methods on client
    clientMethod.invoke(invocationId, invocationParameter);
    // dispatch method on server
    final AtomicReference<JUnitLogger> serverMethodThreadLogger = new AtomicReference<JUnitLogger>();
    new Thread(new Runnable() {

        @Override
        public void run() {
            serverMethodThreadLogger.set(JUnitLoggerProvider.getGlobalLogger());
            messaging.getMessageReceiverBroker().dispatchMessages();
            serverFinsihedLatch.countDown();
        }
    }).start();
    // invoke cancel on client
    cancelService.canceled(invocationId);
    // dispatch cancel message on server
    final AtomicReference<JUnitLogger> serverCancelThreadLogger = new AtomicReference<JUnitLogger>();
    new Thread(new Runnable() {

        @Override
        public void run() {
            serverCancelThreadLogger.set(JUnitLoggerProvider.getGlobalLogger());
            messaging.getMessageReceiverBroker().dispatchMessages();
            cancelLatch.countDown();
        }
    }).start();
    try {
        serverFinsihedLatch.await();
    } catch (final InterruptedException e) {
        throw new RuntimeException(e);
    }
    messaging.getMessageChannelBroker().dispatchReturnedMessages();
    Mockito.verify(serverCancelService, Mockito.times(1)).canceled(invocationId);
    Mockito.verify(clientInvocationCallback, Mockito.never()).exeption(invocationId, cancelException);
    Assert.assertFalse(serverMethodThreadLogger.get().hasMessage());
    Assert.assertFalse(serverCancelThreadLogger.get().hasMessage());
    assertNoLogErrorOrWarning();
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ICancelService(org.jowidgets.invocation.common.api.ICancelService) IInvocationCallbackService(org.jowidgets.invocation.common.api.IInvocationCallbackService) JUnitLogger(org.jowidgets.logging.tools.JUnitLogger) IInvocationClient(org.jowidgets.invocation.client.api.IInvocationClient) InvocationOnMock(org.mockito.invocation.InvocationOnMock) IMethod(org.jowidgets.invocation.common.api.IMethod) Test(org.junit.Test)

Aggregations

ICancelService (org.jowidgets.invocation.common.api.ICancelService)5 IInvocationClient (org.jowidgets.invocation.client.api.IInvocationClient)3 IInvocationCallbackService (org.jowidgets.invocation.common.api.IInvocationCallbackService)3 IMethod (org.jowidgets.invocation.common.api.IMethod)3 Test (org.junit.Test)3 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 CancelMessage (org.jowidgets.invocation.common.impl.CancelMessage)1 ICancelListener (org.jowidgets.invocation.service.common.api.ICancelListener)1 JUnitLogger (org.jowidgets.logging.tools.JUnitLogger)1 IMessageChannel (org.jowidgets.message.api.IMessageChannel)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1