/** * Create a new {@link PatternTopic} for channel subscriptions based on a {@code pattern}. * * @param pattern the channel pattern, must not be {@literal null} or empty. * @return the {@link PatternTopic} for {@code pattern}. * @since 2.1 */ public static PatternTopic of(String pattern) { return new PatternTopic(pattern); }
/** * Process a message received from the provider. * * @param message * @param pattern */ protected void processMessage(MessageListener listener, Message message, byte[] pattern) { executeListener(listener, message, pattern); }
@Override public void afterPropertiesSet() throws Exception { init(); } }
private void initMessageListenerContainer() { this.messageListenerContainer = new RedisMessageListenerContainer(); this.messageListenerContainer.setConnectionFactory(((RedisTemplate<?, ?>) redisOps).getConnectionFactory()); this.messageListenerContainer.afterPropertiesSet(); this.messageListenerContainer.start(); }
/** * Register instance within the container. * * @param container never {@literal null}. */ protected void doRegister(RedisMessageListenerContainer container) { listenerContainer.addMessageListener(this, TOPIC_ALL_KEYEVENTS); }
/** * Create a new {@link ChannelTopic} for channel subscriptions. * * @param name the channel name, must not be {@literal null} or empty. * @return the {@link ChannelTopic} for {@code channelName}. * @since 2.1 */ public static ChannelTopic of(String name) { return new ChannelTopic(name); }
/** * Handle subscription task exception. Will attempt to restart the subscription if the Exception is a connection * failure (for example, Redis was restarted). * * @param ex Throwable exception */ protected void handleSubscriptionException(Throwable ex) { listening = false; subscriptionTask.closeConnection(); if (ex instanceof RedisConnectionFailureException) { if (isRunning()) { logger.error("Connection failure occurred. Restarting subscription task after " + recoveryInterval + " ms"); sleepBeforeRecoveryAttempt(); lazyListen(); } } else { logger.error("SubscriptionTask aborted with exception:", ex); } }
@Override public void destroy() throws Exception { listenerContainer.removeMessageListener(this); }
/** * Adds a message listener to the (potentially running) container. If the container is running, the listener starts * receiving (matching) messages as soon as possible. * * @param listener message listener * @param topics message listener topic */ public void addMessageListener(MessageListener listener, Collection<? extends Topic> topics) { addListener(listener, topics); lazyListen(); }
public void afterPropertiesSet() { if (taskExecutor == null) { manageExecutor = true; taskExecutor = createDefaultTaskExecutor(); } if (subscriptionExecutor == null) { subscriptionExecutor = taskExecutor; } initialized = true; }
/** * Attaches the given listeners (and their topics) to the container. * <p> * Note: it's possible to call this method while the container is running forcing a reinitialization of the container. * Note however that this might cause some messages to be lost (while the container reinitializes) - hence calling * this method at runtime is considered advanced usage. * * @param listeners map of message listeners and their associated topics */ public void setMessageListeners(Map<? extends MessageListener, Collection<? extends Topic>> listeners) { initMapping(listeners); }
/** * Removes a message listener from the given topics. If the container is running, the listener stops receiving * (matching) messages as soon as possible. * <p> * Note that this method obeys the Redis (p)unsubscribe semantics - meaning an empty/null collection will remove * listener from all channels. Similarly a null listener will unsubscribe all listeners from the given topic. * * @param listener message listener * @param topics message listener topics */ public void removeMessageListener(MessageListener listener, Collection<? extends Topic> topics) { removeListener(listener, topics); }
private void verifyConnection() { if (!isActive()) { throw new IllegalStateException("ReactiveRedisMessageListenerContainer is already disposed!"); } }
@Override public void destroy() { destroyLater().block(); }
/** * Execute the specified listener. * * @see #handleListenerException */ protected void executeListener(MessageListener listener, Message message, byte[] pattern) { try { listener.onMessage(message, pattern); } catch (Throwable ex) { handleListenerException(ex); } }
@Override protected void doRegister(RedisMessageListenerContainer listenerContainer) { listenerContainer.addMessageListener(this, KEYEVENT_EXPIRED_TOPIC); }
/** * Removes the given message listener completely (from all topics). If the container is running, the listener stops * receiving (matching) messages as soon as possible. Similarly a null listener will unsubscribe all listeners from * the given topic. * * @param listener message listener */ public void removeMessageListener(MessageListener listener) { removeMessageListener(listener, Collections.<Topic> emptySet()); }
/** * Adds a message listener to the (potentially running) container. If the container is running, the listener starts * receiving (matching) messages as soon as possible. * * @param listener message listener * @param topic message topic */ public void addMessageListener(MessageListener listener, Topic topic) { addMessageListener(listener, Collections.singleton(topic)); }