Search in sources :

Example 1 with CPMembershipListener

use of com.hazelcast.cp.event.CPMembershipListener in project hazelcast by hazelcast.

the class RaftService method dispatchEvent.

@Override
public void dispatchEvent(Object e, EventListener l) {
    long now = Clock.currentTimeMillis();
    recentAvailabilityEvents.values().removeIf(expirationTime -> expirationTime < now);
    if (e instanceof CPMembershipEvent) {
        CPMembershipEvent event = (CPMembershipEvent) e;
        CPMembershipListener listener = (CPMembershipListener) l;
        switch(event.getType()) {
            case ADDED:
                listener.memberAdded(event);
                break;
            case REMOVED:
                listener.memberRemoved(event);
                break;
            default:
                throw new IllegalArgumentException("Unhandled event: " + event);
        }
        return;
    }
    if (e instanceof CPGroupAvailabilityEvent) {
        CPGroupAvailabilityEvent event = (CPGroupAvailabilityEvent) e;
        if (recentAvailabilityEvents.putIfAbsent(new CPGroupAvailabilityEventKey(event, l), now + AVAILABILITY_EVENTS_DEDUPLICATION_PERIOD) != null) {
            return;
        }
        CPGroupAvailabilityListener listener = (CPGroupAvailabilityListener) l;
        if (event.isMajorityAvailable()) {
            listener.availabilityDecreased(event);
        } else {
            listener.majorityLost(event);
        }
        return;
    }
    throw new IllegalArgumentException("Unhandled event: " + e);
}
Also used : CPGroupAvailabilityEvent(com.hazelcast.cp.event.CPGroupAvailabilityEvent) CPMembershipListener(com.hazelcast.cp.event.CPMembershipListener) CPMembershipEvent(com.hazelcast.cp.event.CPMembershipEvent) CPGroupAvailabilityListener(com.hazelcast.cp.event.CPGroupAvailabilityListener)

Example 2 with CPMembershipListener

use of com.hazelcast.cp.event.CPMembershipListener in project hazelcast by hazelcast.

the class AddCPMembershipListenerMessageTask method processInternal.

@Override
protected CompletableFuture<UUID> processInternal() {
    EventService eventService = clientEngine.getEventService();
    CPMembershipListener listener = new ClientCPMembershipListener(endpoint);
    boolean local = parameters;
    if (local) {
        UUID id = eventService.registerLocalListener(getServiceName(), TOPIC, listener).getId();
        return CompletableFuture.completedFuture(id);
    }
    return eventService.registerListenerAsync(getServiceName(), TOPIC, listener).thenApplyAsync(EventRegistration::getId, CALLER_RUNS);
}
Also used : EventRegistration(com.hazelcast.spi.impl.eventservice.EventRegistration) CPMembershipListener(com.hazelcast.cp.event.CPMembershipListener) EventService(com.hazelcast.spi.impl.eventservice.EventService) UUID(java.util.UUID)

Example 3 with CPMembershipListener

use of com.hazelcast.cp.event.CPMembershipListener in project hazelcast by hazelcast.

the class Node method initializeListeners.

@SuppressWarnings({ "checkstyle:npathcomplexity", "checkstyle:cyclomaticcomplexity", "checkstyle:methodlength" })
private void initializeListeners(Config config) {
    for (final ListenerConfig listenerCfg : config.getListenerConfigs()) {
        Object listener = listenerCfg.getImplementation();
        if (listener == null) {
            try {
                listener = ClassLoaderUtil.newInstance(configClassLoader, listenerCfg.getClassName());
            } catch (Exception e) {
                logger.severe(e);
            }
        }
        if (listener instanceof HazelcastInstanceAware) {
            ((HazelcastInstanceAware) listener).setHazelcastInstance(hazelcastInstance);
        }
        boolean known = false;
        if (listener instanceof DistributedObjectListener) {
            final ProxyServiceImpl proxyService = (ProxyServiceImpl) nodeEngine.getProxyService();
            proxyService.addProxyListener((DistributedObjectListener) listener);
            known = true;
        }
        if (listener instanceof MembershipListener) {
            clusterService.addMembershipListener((MembershipListener) listener);
            known = true;
        }
        if (listener instanceof MigrationListener) {
            partitionService.addMigrationListener((MigrationListener) listener);
            known = true;
        }
        if (listener instanceof PartitionLostListener) {
            partitionService.addPartitionLostListener((PartitionLostListener) listener);
            known = true;
        }
        if (listener instanceof LifecycleListener) {
            hazelcastInstance.lifecycleService.addLifecycleListener((LifecycleListener) listener);
            known = true;
        }
        if (listener instanceof ClientListener) {
            String serviceName = ClientEngineImpl.SERVICE_NAME;
            nodeEngine.getEventService().registerLocalListener(serviceName, serviceName, listener);
            known = true;
        }
        if (listener instanceof MigrationInterceptor) {
            partitionService.setMigrationInterceptor((MigrationInterceptor) listener);
            known = true;
        }
        if (listener instanceof CPMembershipListener) {
            hazelcastInstance.cpSubsystem.addMembershipListener((CPMembershipListener) listener);
            known = true;
        }
        if (listener instanceof CPGroupAvailabilityListener) {
            hazelcastInstance.cpSubsystem.addGroupAvailabilityListener((CPGroupAvailabilityListener) listener);
            known = true;
        }
        if (nodeExtension.registerListener(listener)) {
            known = true;
        }
        if (listener != null && !known) {
            final String error = "Unknown listener type: " + listener.getClass();
            Throwable t = new IllegalArgumentException(error);
            logger.warning(error, t);
        }
    }
}
Also used : LifecycleListener(com.hazelcast.core.LifecycleListener) HazelcastInstanceAware(com.hazelcast.core.HazelcastInstanceAware) DistributedObjectListener(com.hazelcast.core.DistributedObjectListener) ProxyServiceImpl(com.hazelcast.spi.impl.proxyservice.impl.ProxyServiceImpl) ClientListener(com.hazelcast.client.ClientListener) ListenerConfig(com.hazelcast.config.ListenerConfig) PartitionLostListener(com.hazelcast.partition.PartitionLostListener) CPMembershipListener(com.hazelcast.cp.event.CPMembershipListener) MigrationInterceptor(com.hazelcast.internal.partition.impl.MigrationInterceptor) CPMembershipListener(com.hazelcast.cp.event.CPMembershipListener) MembershipListener(com.hazelcast.cluster.MembershipListener) MigrationListener(com.hazelcast.partition.MigrationListener) CPGroupAvailabilityListener(com.hazelcast.cp.event.CPGroupAvailabilityListener)

Aggregations

CPMembershipListener (com.hazelcast.cp.event.CPMembershipListener)3 CPGroupAvailabilityListener (com.hazelcast.cp.event.CPGroupAvailabilityListener)2 ClientListener (com.hazelcast.client.ClientListener)1 MembershipListener (com.hazelcast.cluster.MembershipListener)1 ListenerConfig (com.hazelcast.config.ListenerConfig)1 DistributedObjectListener (com.hazelcast.core.DistributedObjectListener)1 HazelcastInstanceAware (com.hazelcast.core.HazelcastInstanceAware)1 LifecycleListener (com.hazelcast.core.LifecycleListener)1 CPGroupAvailabilityEvent (com.hazelcast.cp.event.CPGroupAvailabilityEvent)1 CPMembershipEvent (com.hazelcast.cp.event.CPMembershipEvent)1 MigrationInterceptor (com.hazelcast.internal.partition.impl.MigrationInterceptor)1 MigrationListener (com.hazelcast.partition.MigrationListener)1 PartitionLostListener (com.hazelcast.partition.PartitionLostListener)1 EventRegistration (com.hazelcast.spi.impl.eventservice.EventRegistration)1 EventService (com.hazelcast.spi.impl.eventservice.EventService)1 ProxyServiceImpl (com.hazelcast.spi.impl.proxyservice.impl.ProxyServiceImpl)1 UUID (java.util.UUID)1