Search in sources :

Example 1 with StreamName

use of com.oracle.bedrock.runtime.concurrent.options.StreamName in project oracle-bedrock by coherence-community.

the class SocketBasedRemoteChannelTest method shouldReceiveEventsInOrder.

@Test
public void shouldReceiveEventsInOrder() throws Exception {
    int count = 100;
    final CountDownLatch latch = new CountDownLatch(count);
    final List<Integer> list = new ArrayList<>();
    RemoteEventListener listener = new RemoteEventListener() {

        @Override
        public void onEvent(RemoteEvent event) {
            list.add(((Event) event).getId());
            latch.countDown();
        }
    };
    try (SocketBasedRemoteChannelServer server = new SocketBasedRemoteChannelServer("Test")) {
        StreamName streamName = StreamName.of("Foo");
        InetAddress address = server.open();
        server.addListener(listener, streamName);
        try (SocketBasedRemoteChannelClient client = new SocketBasedRemoteChannelClient(address, server.getPort())) {
            client.open();
            for (int i = 0; i < count; i++) {
                client.raise(new Event(i), streamName);
            }
            assertThat(latch.await(1, TimeUnit.MINUTES), is(true));
            assertThat(list.size(), is(count));
            for (int i = 0; i < count; i++) {
                assertThat(list.get(i), is(i));
            }
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StreamName(com.oracle.bedrock.runtime.concurrent.options.StreamName) RemoteEvent(com.oracle.bedrock.runtime.concurrent.RemoteEvent) ArrayList(java.util.ArrayList) RemoteEvent(com.oracle.bedrock.runtime.concurrent.RemoteEvent) CountDownLatch(java.util.concurrent.CountDownLatch) InetAddress(java.net.InetAddress) RemoteEventListener(com.oracle.bedrock.runtime.concurrent.RemoteEventListener) Test(org.junit.Test)

Example 2 with StreamName

use of com.oracle.bedrock.runtime.concurrent.options.StreamName in project oracle-bedrock by coherence-community.

the class AbstractControllableRemoteChannel method removeListener.

@Override
public void removeListener(RemoteEventListener listener, Option... options) {
    OptionsByType optionsByType = OptionsByType.of(options);
    StreamName streamName = optionsByType.get(StreamName.class);
    eventListenersByStreamName.computeIfPresent(streamName, (name, eventListeners) -> {
        eventListeners.remove(listener);
        return eventListeners.size() == 0 ? null : eventListeners;
    });
}
Also used : StreamName(com.oracle.bedrock.runtime.concurrent.options.StreamName) OptionsByType(com.oracle.bedrock.OptionsByType)

Example 3 with StreamName

use of com.oracle.bedrock.runtime.concurrent.options.StreamName in project oracle-bedrock by coherence-community.

the class AbstractRemoteChannel method raise.

@Override
public CompletableFuture<Void> raise(RemoteEvent event, Option... options) {
    if (isOpen()) {
        OptionsByType optionsByType = OptionsByType.of(options);
        StreamName streamName = optionsByType.get(StreamName.class);
        // by default we acknowledge when sent
        optionsByType.addIfAbsent(AcknowledgeWhen.SENT);
        EventOperation operation = new EventOperation(streamName, event, optionsByType);
        return sendOperation(operation, optionsByType);
    } else {
        throw new IllegalStateException("RemoteChannel is closed");
    }
}
Also used : StreamName(com.oracle.bedrock.runtime.concurrent.options.StreamName) OptionsByType(com.oracle.bedrock.OptionsByType)

Example 4 with StreamName

use of com.oracle.bedrock.runtime.concurrent.options.StreamName in project oracle-bedrock by coherence-community.

the class SocketBasedRemoteChannelTest method shouldRaiseAndProcessEvents.

@Test
public void shouldRaiseAndProcessEvents() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    RemoteEventListener listener = new RemoteEventListener() {

        @Override
        public void onEvent(RemoteEvent event) {
            latch.countDown();
        }
    };
    try (SocketBasedRemoteChannelServer server = new SocketBasedRemoteChannelServer("Test")) {
        StreamName streamName = StreamName.of("Foo");
        InetAddress address = server.open();
        server.addListener(listener, streamName);
        try (SocketBasedRemoteChannelClient client = new SocketBasedRemoteChannelClient(address, server.getPort())) {
            client.open();
            client.raise(new Event(1), streamName);
            assertThat(latch.await(1, TimeUnit.MINUTES), is(true));
        }
    }
}
Also used : StreamName(com.oracle.bedrock.runtime.concurrent.options.StreamName) RemoteEvent(com.oracle.bedrock.runtime.concurrent.RemoteEvent) RemoteEvent(com.oracle.bedrock.runtime.concurrent.RemoteEvent) CountDownLatch(java.util.concurrent.CountDownLatch) InetAddress(java.net.InetAddress) RemoteEventListener(com.oracle.bedrock.runtime.concurrent.RemoteEventListener) Test(org.junit.Test)

Example 5 with StreamName

use of com.oracle.bedrock.runtime.concurrent.options.StreamName in project oracle-bedrock by coherence-community.

the class AbstractControllableRemoteChannel method addListener.

@Override
public void addListener(RemoteEventListener listener, Option... options) {
    OptionsByType optionsByType = OptionsByType.of(options);
    StreamName streamName = optionsByType.get(StreamName.class);
    eventListenersByStreamName.compute(streamName, (name, eventListeners) -> {
        if (eventListeners == null) {
            eventListeners = new CopyOnWriteArraySet<>();
        }
        eventListeners.add(listener);
        return eventListeners;
    });
}
Also used : StreamName(com.oracle.bedrock.runtime.concurrent.options.StreamName) OptionsByType(com.oracle.bedrock.OptionsByType)

Aggregations

StreamName (com.oracle.bedrock.runtime.concurrent.options.StreamName)7 OptionsByType (com.oracle.bedrock.OptionsByType)4 RemoteEventListener (com.oracle.bedrock.runtime.concurrent.RemoteEventListener)3 RemoteEvent (com.oracle.bedrock.runtime.concurrent.RemoteEvent)2 InetAddress (java.net.InetAddress)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Test (org.junit.Test)2 ClassLoaderAwareObjectInputStream (com.oracle.bedrock.runtime.java.io.ClassLoaderAwareObjectInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 NotSerializableException (java.io.NotSerializableException)1 ObjectInputStream (java.io.ObjectInputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1