Search in sources :

Example 1 with DataAwareItemEvent

use of com.hazelcast.collection.impl.common.DataAwareItemEvent in project hazelcast by hazelcast.

the class QueueAddListenerMessageTask method call.

@Override
protected Object call() {
    final ClientEndpoint endpoint = getEndpoint();
    final QueueService service = getService(QueueService.SERVICE_NAME);
    final Data partitionKey = serializationService.toData(parameters.name);
    ItemListener listener = new ItemListener() {

        @Override
        public void itemAdded(ItemEvent item) {
            send(item);
        }

        @Override
        public void itemRemoved(ItemEvent item) {
            send(item);
        }

        private void send(ItemEvent event) {
            if (endpoint.isAlive()) {
                if (!(event instanceof DataAwareItemEvent)) {
                    throw new IllegalArgumentException("Expecting: DataAwareItemEvent, Found: " + event.getClass().getSimpleName());
                }
                DataAwareItemEvent dataAwareItemEvent = (DataAwareItemEvent) event;
                Data item = dataAwareItemEvent.getItemData();
                ClientMessage clientMessage = QueueAddListenerCodec.encodeItemEvent(item, event.getMember().getUuid(), event.getEventType().getType());
                sendClientMessage(partitionKey, clientMessage);
            }
        }
    };
    String registrationId = service.addItemListener(parameters.name, listener, parameters.includeValue, parameters.localOnly);
    endpoint.addListenerDestroyAction(QueueService.SERVICE_NAME, parameters.name, registrationId);
    return registrationId;
}
Also used : DataAwareItemEvent(com.hazelcast.collection.impl.common.DataAwareItemEvent) ItemEvent(com.hazelcast.core.ItemEvent) DataAwareItemEvent(com.hazelcast.collection.impl.common.DataAwareItemEvent) QueueService(com.hazelcast.collection.impl.queue.QueueService) Data(com.hazelcast.nio.serialization.Data) ItemListener(com.hazelcast.core.ItemListener) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) ClientEndpoint(com.hazelcast.client.ClientEndpoint)

Example 2 with DataAwareItemEvent

use of com.hazelcast.collection.impl.common.DataAwareItemEvent in project hazelcast by hazelcast.

the class CollectionService method dispatchEvent.

@Override
public void dispatchEvent(CollectionEvent event, ItemListener listener) {
    final MemberImpl member = nodeEngine.getClusterService().getMember(event.getCaller());
    ItemEvent itemEvent = new DataAwareItemEvent(event.getName(), event.getEventType(), event.getData(), member, nodeEngine.getSerializationService());
    if (member == null) {
        if (logger.isInfoEnabled()) {
            logger.info("Dropping event " + itemEvent + " from unknown address:" + event.getCaller());
        }
        return;
    }
    if (event.getEventType().equals(ItemEventType.ADDED)) {
        listener.itemAdded(itemEvent);
    } else {
        listener.itemRemoved(itemEvent);
    }
}
Also used : ItemEvent(com.hazelcast.core.ItemEvent) DataAwareItemEvent(com.hazelcast.collection.impl.common.DataAwareItemEvent) DataAwareItemEvent(com.hazelcast.collection.impl.common.DataAwareItemEvent) MemberImpl(com.hazelcast.instance.MemberImpl)

Example 3 with DataAwareItemEvent

use of com.hazelcast.collection.impl.common.DataAwareItemEvent in project hazelcast by hazelcast.

the class QueueService method dispatchEvent.

@Override
public void dispatchEvent(QueueEvent event, ItemListener listener) {
    final MemberImpl member = nodeEngine.getClusterService().getMember(event.caller);
    ItemEvent itemEvent = new DataAwareItemEvent(event.name, event.eventType, event.data, member, serializationService);
    if (member == null) {
        if (logger.isInfoEnabled()) {
            logger.info("Dropping event " + itemEvent + " from unknown address:" + event.caller);
        }
        return;
    }
    if (event.eventType.equals(ItemEventType.ADDED)) {
        listener.itemAdded(itemEvent);
    } else {
        listener.itemRemoved(itemEvent);
    }
    getLocalQueueStatsImpl(event.name).incrementReceivedEvents();
}
Also used : ItemEvent(com.hazelcast.collection.ItemEvent) DataAwareItemEvent(com.hazelcast.collection.impl.common.DataAwareItemEvent) DataAwareItemEvent(com.hazelcast.collection.impl.common.DataAwareItemEvent) MemberImpl(com.hazelcast.cluster.impl.MemberImpl)

Example 4 with DataAwareItemEvent

use of com.hazelcast.collection.impl.common.DataAwareItemEvent in project hazelcast by hazelcast.

the class QueueAddListenerMessageTask method processInternal.

@Override
protected CompletableFuture<UUID> processInternal() {
    final QueueService service = getService(QueueService.SERVICE_NAME);
    final Data partitionKey = serializationService.toData(parameters.name);
    ItemListener listener = new ItemListener() {

        @Override
        public void itemAdded(ItemEvent item) {
            send(item);
        }

        @Override
        public void itemRemoved(ItemEvent item) {
            send(item);
        }

        private void send(ItemEvent event) {
            if (endpoint.isAlive()) {
                if (!(event instanceof DataAwareItemEvent)) {
                    throw new IllegalArgumentException("Expecting: DataAwareItemEvent, Found: " + event.getClass().getSimpleName());
                }
                DataAwareItemEvent dataAwareItemEvent = (DataAwareItemEvent) event;
                Data item = dataAwareItemEvent.getItemData();
                ClientMessage clientMessage = QueueAddListenerCodec.encodeItemEvent(item, event.getMember().getUuid(), event.getEventType().getType());
                sendClientMessage(partitionKey, clientMessage);
            }
        }
    };
    if (parameters.localOnly) {
        return newCompletedFuture(service.addLocalItemListener(parameters.name, listener, parameters.includeValue));
    }
    return service.addItemListenerAsync(parameters.name, listener, parameters.includeValue);
}
Also used : DataAwareItemEvent(com.hazelcast.collection.impl.common.DataAwareItemEvent) ItemEvent(com.hazelcast.collection.ItemEvent) DataAwareItemEvent(com.hazelcast.collection.impl.common.DataAwareItemEvent) QueueService(com.hazelcast.collection.impl.queue.QueueService) Data(com.hazelcast.internal.serialization.Data) ItemListener(com.hazelcast.collection.ItemListener) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage)

Example 5 with DataAwareItemEvent

use of com.hazelcast.collection.impl.common.DataAwareItemEvent in project hazelcast by hazelcast.

the class SetAddListenerMessageTask method createItemListener.

private ItemListener createItemListener(final ClientEndpoint endpoint, final Data partitionKey) {
    return new ItemListener() {

        @Override
        public void itemAdded(ItemEvent item) {
            send(item);
        }

        @Override
        public void itemRemoved(ItemEvent item) {
            send(item);
        }

        private void send(ItemEvent event) {
            if (endpoint.isAlive()) {
                if (!(event instanceof DataAwareItemEvent)) {
                    throw new IllegalArgumentException("Expecting: DataAwareItemEvent, Found: " + event.getClass().getSimpleName());
                }
                DataAwareItemEvent dataAwareItemEvent = (DataAwareItemEvent) event;
                Data item = dataAwareItemEvent.getItemData();
                ClientMessage clientMessage = SetAddListenerCodec.encodeItemEvent(item, event.getMember().getUuid(), event.getEventType().getType());
                sendClientMessage(partitionKey, clientMessage);
            }
        }
    };
}
Also used : DataAwareItemEvent(com.hazelcast.collection.impl.common.DataAwareItemEvent) ItemEvent(com.hazelcast.collection.ItemEvent) DataAwareItemEvent(com.hazelcast.collection.impl.common.DataAwareItemEvent) ItemListener(com.hazelcast.collection.ItemListener) Data(com.hazelcast.internal.serialization.Data) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage)

Aggregations

DataAwareItemEvent (com.hazelcast.collection.impl.common.DataAwareItemEvent)7 ClientMessage (com.hazelcast.client.impl.protocol.ClientMessage)4 ItemEvent (com.hazelcast.collection.ItemEvent)4 Data (com.hazelcast.internal.serialization.Data)4 ItemListener (com.hazelcast.collection.ItemListener)3 MemberImpl (com.hazelcast.cluster.impl.MemberImpl)2 QueueService (com.hazelcast.collection.impl.queue.QueueService)2 ItemEvent (com.hazelcast.core.ItemEvent)2 ClientEndpoint (com.hazelcast.client.ClientEndpoint)1 ItemListener (com.hazelcast.core.ItemListener)1 MemberImpl (com.hazelcast.instance.MemberImpl)1 Data (com.hazelcast.nio.serialization.Data)1