Search in sources :

Example 46 with Event

use of org.apache.ignite.events.Event in project ignite by apache.

the class GridEventConsumeSelfTest method testAllEvents.

/**
 * @throws Exception If failed.
 */
public void testAllEvents() throws Exception {
    final Collection<UUID> nodeIds = new HashSet<>();
    final AtomicInteger cnt = new AtomicInteger();
    final CountDownLatch latch = new CountDownLatch(GRID_CNT);
    UUID consumeId = grid(0).events().remoteListen(new P2<UUID, Event>() {

        @Override
        public boolean apply(UUID nodeId, Event evt) {
            info("Event from " + nodeId + " [" + evt.shortDisplay() + ']');
            if (evt.type() == EVT_JOB_STARTED) {
                nodeIds.add(nodeId);
                cnt.incrementAndGet();
                latch.countDown();
            }
            return true;
        }
    }, null);
    try {
        assertNotNull(consumeId);
        grid(0).compute().broadcast(F.noop());
        assert latch.await(2, SECONDS);
        assertEquals(GRID_CNT, nodeIds.size());
        assertEquals(GRID_CNT, cnt.get());
    } finally {
        grid(0).events().stopRemoteListen(consumeId);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) JobEvent(org.apache.ignite.events.JobEvent) Event(org.apache.ignite.events.Event) UUID(java.util.UUID) CountDownLatch(java.util.concurrent.CountDownLatch) HashSet(java.util.HashSet) GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet)

Example 47 with Event

use of org.apache.ignite.events.Event in project ignite by apache.

the class GridEventConsumeSelfTest method testStopByCallback.

/**
 * @throws Exception If failed.
 */
public void testStopByCallback() throws Exception {
    final Collection<UUID> nodeIds = new HashSet<>();
    final AtomicInteger cnt = new AtomicInteger();
    final CountDownLatch latch = new CountDownLatch(1);
    UUID consumeId = grid(0).events().remoteListen(new P2<UUID, Event>() {

        @Override
        public boolean apply(UUID nodeId, Event evt) {
            info("Event from " + nodeId + " [" + evt.shortDisplay() + ']');
            assertEquals(EVT_JOB_STARTED, evt.type());
            nodeIds.add(nodeId);
            cnt.incrementAndGet();
            latch.countDown();
            return false;
        }
    }, null, EVT_JOB_STARTED);
    try {
        assertNotNull(consumeId);
        grid(0).compute().broadcast(F.noop());
        assert latch.await(2, SECONDS);
        assertEquals(1, nodeIds.size());
        assertEquals(1, cnt.get());
    } finally {
        grid(0).events().stopRemoteListen(consumeId);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) JobEvent(org.apache.ignite.events.JobEvent) Event(org.apache.ignite.events.Event) UUID(java.util.UUID) CountDownLatch(java.util.concurrent.CountDownLatch) HashSet(java.util.HashSet) GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet)

Example 48 with Event

use of org.apache.ignite.events.Event in project ignite by apache.

the class GridEventConsumeSelfTest method testLocalNodeOnly.

/**
 * @throws Exception If failed.
 */
public void testLocalNodeOnly() throws Exception {
    final Collection<UUID> nodeIds = new HashSet<>();
    final AtomicInteger cnt = new AtomicInteger();
    final CountDownLatch latch = new CountDownLatch(1);
    UUID consumeId = events(grid(0).cluster().forLocal()).remoteListen(new P2<UUID, Event>() {

        @Override
        public boolean apply(UUID nodeId, Event evt) {
            info("Event from " + nodeId + " [" + evt.shortDisplay() + ']');
            assertEquals(EVT_JOB_STARTED, evt.type());
            nodeIds.add(nodeId);
            cnt.incrementAndGet();
            latch.countDown();
            return true;
        }
    }, null, EVT_JOB_STARTED);
    try {
        assertNotNull(consumeId);
        grid(0).compute().broadcast(F.noop());
        assert latch.await(2, SECONDS);
        assertEquals(1, nodeIds.size());
        assertEquals(1, cnt.get());
        assertEquals(grid(0).localNode().id(), F.first(nodeIds));
    } finally {
        grid(0).events().stopRemoteListen(consumeId);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) JobEvent(org.apache.ignite.events.JobEvent) Event(org.apache.ignite.events.Event) UUID(java.util.UUID) CountDownLatch(java.util.concurrent.CountDownLatch) HashSet(java.util.HashSet) GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet)

Example 49 with Event

use of org.apache.ignite.events.Event in project ignite by apache.

the class GridEventConsumeSelfTest method testApi.

/**
 * @throws Exception If failed.
 */
public void testApi() throws Exception {
    try {
        grid(0).events().stopRemoteListen(null);
    } catch (NullPointerException ignored) {
    // No-op.
    }
    grid(0).events().stopRemoteListen(UUID.randomUUID());
    UUID consumeId = null;
    try {
        consumeId = grid(0).events().remoteListen(new P2<UUID, DiscoveryEvent>() {

            @Override
            public boolean apply(UUID uuid, DiscoveryEvent evt) {
                return false;
            }
        }, new P1<DiscoveryEvent>() {

            @Override
            public boolean apply(DiscoveryEvent e) {
                return false;
            }
        }, EVTS_DISCOVERY);
        assertNotNull(consumeId);
    } finally {
        grid(0).events().stopRemoteListen(consumeId);
    }
    try {
        consumeId = grid(0).events().remoteListen(new P2<UUID, DiscoveryEvent>() {

            @Override
            public boolean apply(UUID uuid, DiscoveryEvent evt) {
                return false;
            }
        }, new P1<DiscoveryEvent>() {

            @Override
            public boolean apply(DiscoveryEvent e) {
                return false;
            }
        });
        assertNotNull(consumeId);
    } finally {
        grid(0).events().stopRemoteListen(consumeId);
    }
    try {
        consumeId = grid(0).events().remoteListen(new P2<UUID, Event>() {

            @Override
            public boolean apply(UUID uuid, Event evt) {
                return false;
            }
        }, new P1<Event>() {

            @Override
            public boolean apply(Event e) {
                return false;
            }
        });
        assertNotNull(consumeId);
    } finally {
        grid(0).events().stopRemoteListen(consumeId);
    }
}
Also used : P1(org.apache.ignite.internal.util.typedef.P1) P2(org.apache.ignite.internal.util.typedef.P2) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) JobEvent(org.apache.ignite.events.JobEvent) Event(org.apache.ignite.events.Event) UUID(java.util.UUID)

Example 50 with Event

use of org.apache.ignite.events.Event in project ignite by apache.

the class GridEventConsumeSelfTest method testNodeJoinWithProjection.

/**
 * @throws Exception If failed.
 */
public void testNodeJoinWithProjection() throws Exception {
    final Collection<UUID> nodeIds = new HashSet<>();
    final AtomicInteger cnt = new AtomicInteger();
    final CountDownLatch latch = new CountDownLatch(GRID_CNT);
    UUID consumeId = events(grid(0).cluster().forAttribute("include", null)).remoteListen(new P2<UUID, Event>() {

        @Override
        public boolean apply(UUID nodeId, Event evt) {
            info("Event from " + nodeId + " [" + evt.shortDisplay() + ']');
            assertEquals(EVT_JOB_STARTED, evt.type());
            nodeIds.add(nodeId);
            cnt.incrementAndGet();
            latch.countDown();
            return true;
        }
    }, null, EVT_JOB_STARTED);
    try {
        assertNotNull(consumeId);
        include = true;
        startGrid("anotherGrid1");
        include = false;
        startGrid("anotherGrid2");
        grid(0).compute().broadcast(F.noop());
        assert latch.await(2, SECONDS);
        assertEquals(GRID_CNT, nodeIds.size());
        assertEquals(GRID_CNT, cnt.get());
    } finally {
        stopGrid("anotherGrid1");
        stopGrid("anotherGrid2");
        grid(0).events().stopRemoteListen(consumeId);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) JobEvent(org.apache.ignite.events.JobEvent) Event(org.apache.ignite.events.Event) UUID(java.util.UUID) CountDownLatch(java.util.concurrent.CountDownLatch) HashSet(java.util.HashSet) GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet)

Aggregations

Event (org.apache.ignite.events.Event)226 CountDownLatch (java.util.concurrent.CountDownLatch)127 Ignite (org.apache.ignite.Ignite)112 DiscoveryEvent (org.apache.ignite.events.DiscoveryEvent)96 UUID (java.util.UUID)71 IgnitePredicate (org.apache.ignite.lang.IgnitePredicate)44 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)43 ClusterNode (org.apache.ignite.cluster.ClusterNode)38 GridLocalEventListener (org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener)28 ArrayList (java.util.ArrayList)27 JobEvent (org.apache.ignite.events.JobEvent)27 IgniteException (org.apache.ignite.IgniteException)20 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)19 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)16 HashSet (java.util.HashSet)15 TaskEvent (org.apache.ignite.events.TaskEvent)15 Collection (java.util.Collection)13 HashMap (java.util.HashMap)12 GridMessageListener (org.apache.ignite.internal.managers.communication.GridMessageListener)12 GridConcurrentHashSet (org.apache.ignite.internal.util.GridConcurrentHashSet)12