Search in sources :

Example 1 with PlatformEventFilterListener

use of org.apache.ignite.internal.processors.platform.PlatformEventFilterListener in project ignite by apache.

the class GridEventConsumeHandler method register.

/** {@inheritDoc} */
@Override
public RegisterStatus register(final UUID nodeId, final UUID routineId, final GridKernalContext ctx) throws IgniteCheckedException {
    assert nodeId != null;
    assert routineId != null;
    assert ctx != null;
    if (cb != null)
        ctx.resource().injectGeneric(cb);
    if (filter != null)
        ctx.resource().injectGeneric(filter);
    if (filter instanceof PlatformEventFilterListener)
        ((PlatformEventFilterListener) filter).initialize(ctx);
    final boolean loc = nodeId.equals(ctx.localNodeId());
    lsnr = new GridLocalEventListener() {

        /** node ID, routine ID, event */
        private final Queue<T3<UUID, UUID, Event>> notificationQueue = new LinkedList<>();

        private boolean notificationInProgress;

        @Override
        public void onEvent(Event evt) {
            if (filter != null && !filter.apply(evt))
                return;
            if (loc) {
                if (!cb.apply(nodeId, evt))
                    ctx.continuous().stopRoutine(routineId);
            } else {
                if (ctx.discovery().node(nodeId) == null)
                    return;
                synchronized (notificationQueue) {
                    notificationQueue.add(new T3<>(nodeId, routineId, evt));
                    if (!notificationInProgress) {
                        ctx.getSystemExecutorService().execute(new Runnable() {

                            @Override
                            public void run() {
                                if (!ctx.continuous().lockStopping())
                                    return;
                                try {
                                    while (true) {
                                        T3<UUID, UUID, Event> t3;
                                        synchronized (notificationQueue) {
                                            t3 = notificationQueue.poll();
                                            if (t3 == null) {
                                                notificationInProgress = false;
                                                return;
                                            }
                                        }
                                        try {
                                            Event evt = t3.get3();
                                            EventWrapper wrapper = new EventWrapper(evt);
                                            if (evt instanceof CacheEvent) {
                                                String cacheName = ((CacheEvent) evt).cacheName();
                                                ClusterNode node = ctx.discovery().node(t3.get1());
                                                if (node == null)
                                                    continue;
                                                if (ctx.config().isPeerClassLoadingEnabled()) {
                                                    GridCacheContext cctx = ctx.cache().internalCache(cacheName).context();
                                                    if (cctx.deploymentEnabled() && ctx.discovery().cacheNode(node, cacheName)) {
                                                        wrapper.p2pMarshal(ctx.config().getMarshaller());
                                                        wrapper.cacheName = cacheName;
                                                        cctx.deploy().prepare(wrapper);
                                                    }
                                                }
                                            }
                                            ctx.continuous().addNotification(t3.get1(), t3.get2(), wrapper, null, false, false);
                                        } catch (ClusterTopologyCheckedException ignored) {
                                        // No-op.
                                        } catch (Throwable e) {
                                            U.error(ctx.log(GridEventConsumeHandler.class), "Failed to send event notification to node: " + nodeId, e);
                                        }
                                    }
                                } finally {
                                    ctx.continuous().unlockStopping();
                                }
                            }
                        });
                        notificationInProgress = true;
                    }
                }
            }
        }
    };
    if (F.isEmpty(types))
        types = EVTS_ALL;
    ctx.event().addLocalEventListener(lsnr, types);
    return RegisterStatus.REGISTERED;
}
Also used : PlatformEventFilterListener(org.apache.ignite.internal.processors.platform.PlatformEventFilterListener) ClusterNode(org.apache.ignite.cluster.ClusterNode) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) GridLocalEventListener(org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener) LinkedList(java.util.LinkedList) CacheEvent(org.apache.ignite.events.CacheEvent) CacheEvent(org.apache.ignite.events.CacheEvent) Event(org.apache.ignite.events.Event) UUID(java.util.UUID) T3(org.apache.ignite.internal.util.typedef.T3) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Example 2 with PlatformEventFilterListener

use of org.apache.ignite.internal.processors.platform.PlatformEventFilterListener in project ignite by apache.

the class PlatformEvents method startRemoteQueryAsync.

/**
     * Starts the remote query asynchronously.
     *
     * @param reader Reader.
     * @param events Events.
     * @return Result.
     */
private IgniteFuture<List<Event>> startRemoteQueryAsync(BinaryRawReaderEx reader, IgniteEvents events) {
    Object pred = reader.readObjectDetached();
    long timeout = reader.readLong();
    int[] types = readEventTypes(reader);
    PlatformEventFilterListener filter = platformCtx.createRemoteEventFilter(pred, types);
    return events.remoteQueryAsync(filter, timeout);
}
Also used : PlatformEventFilterListener(org.apache.ignite.internal.processors.platform.PlatformEventFilterListener)

Example 3 with PlatformEventFilterListener

use of org.apache.ignite.internal.processors.platform.PlatformEventFilterListener in project ignite by apache.

the class GridEventStorageManager method localEvents.

/**
     * @param p Grid event predicate.
     * @return Collection of grid events.
     */
@SuppressWarnings("unchecked")
public <T extends Event> Collection<T> localEvents(IgnitePredicate<T> p) throws IgniteCheckedException {
    assert p != null;
    if (getSpi() instanceof NoopEventStorageSpi) {
        throw new IgniteCheckedException("Failed to query events because default no-op event storage SPI is used. " + "Consider configuring " + MemoryEventStorageSpi.class.getSimpleName() + " or another " + EventStorageSpi.class.getSimpleName() + " implementation via " + "IgniteConfiguration.setEventStorageSpi() configuration property.");
    }
    if (p instanceof PlatformEventFilterListener) {
        PlatformEventFilterListener p0 = (PlatformEventFilterListener) p;
        p0.initialize(ctx);
        try {
            return (Collection<T>) getSpi().localEvents(p0);
        } finally {
            p0.onClose();
        }
    } else
        return getSpi().localEvents(p);
}
Also used : PlatformEventFilterListener(org.apache.ignite.internal.processors.platform.PlatformEventFilterListener) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Collection(java.util.Collection) NoopEventStorageSpi(org.apache.ignite.spi.eventstorage.NoopEventStorageSpi)

Example 4 with PlatformEventFilterListener

use of org.apache.ignite.internal.processors.platform.PlatformEventFilterListener in project ignite by apache.

the class PlatformEvents method startRemoteQuery.

/**
     * Starts the remote query.
     *
     * @param reader Reader.
     * @param events Events.
     * @return Result.
     */
private Collection<Event> startRemoteQuery(BinaryRawReaderEx reader, IgniteEvents events) {
    Object pred = reader.readObjectDetached();
    long timeout = reader.readLong();
    int[] types = readEventTypes(reader);
    PlatformEventFilterListener filter = platformCtx.createRemoteEventFilter(pred, types);
    return events.remoteQuery(filter, timeout);
}
Also used : PlatformEventFilterListener(org.apache.ignite.internal.processors.platform.PlatformEventFilterListener)

Aggregations

PlatformEventFilterListener (org.apache.ignite.internal.processors.platform.PlatformEventFilterListener)4 Collection (java.util.Collection)1 LinkedList (java.util.LinkedList)1 UUID (java.util.UUID)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 ClusterNode (org.apache.ignite.cluster.ClusterNode)1 CacheEvent (org.apache.ignite.events.CacheEvent)1 Event (org.apache.ignite.events.Event)1 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)1 GridLocalEventListener (org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener)1 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)1 T3 (org.apache.ignite.internal.util.typedef.T3)1 NoopEventStorageSpi (org.apache.ignite.spi.eventstorage.NoopEventStorageSpi)1