Example usage for org.springframework.data.redis.connection Message getBody

List of usage examples for org.springframework.data.redis.connection Message getBody

Introduction

In this page you can find the example usage for org.springframework.data.redis.connection Message getBody.

Prototype

byte[] getBody();

Source Link

Document

Returns the body (or the payload) of the message.

Usage

From source file:org.springframework.data.redis.listener.adapter.MessageListenerAdapter.java

/**
 * Extract the message body from the given Redis message.
 * /* w  ww .j  a  v  a2  s.  c  o m*/
 * @param message the Redis <code>Message</code>
 * @return the content of the message, to be passed into the listener method as argument
 */
protected Object extractMessage(Message message) {
    if (serializer != null) {
        return serializer.deserialize(message.getBody());
    }
    return message.getBody();
}

From source file:org.springframework.session.data.redis.RedisOperationsSessionRepository.java

@SuppressWarnings("unchecked")
public void onMessage(Message message, byte[] pattern) {
    byte[] messageChannel = message.getChannel();
    byte[] messageBody = message.getBody();
    if (messageChannel == null || messageBody == null) {
        return;/*from w  w w.  j a va2s  .  co m*/
    }

    String channel = new String(messageChannel);

    if (channel.startsWith(getSessionCreatedChannelPrefix())) {
        // TODO: is this thread safe?
        Map<Object, Object> loaded = (Map<Object, Object>) this.defaultSerializer
                .deserialize(message.getBody());
        handleCreated(loaded, channel);
        return;
    }

    String body = new String(messageBody);
    if (!body.startsWith(getExpiredKeyPrefix())) {
        return;
    }

    boolean isDeleted = channel.endsWith(":del");
    if (isDeleted || channel.endsWith(":expired")) {
        int beginIndex = body.lastIndexOf(":") + 1;
        int endIndex = body.length();
        String sessionId = body.substring(beginIndex, endIndex);

        RedisSession session = getSession(sessionId, true);

        if (logger.isDebugEnabled()) {
            logger.debug("Publishing SessionDestroyedEvent for session " + sessionId);
        }

        cleanupPrincipalIndex(session);

        if (isDeleted) {
            handleDeleted(sessionId, session);
        } else {
            handleExpired(sessionId, session);
        }

        return;
    }
}

From source file:org.springframework.session.data.redis.SessionMessageListener.java

public void onMessage(Message message, byte[] pattern) {
    byte[] messageChannel = message.getChannel();
    byte[] messageBody = message.getBody();
    if (messageChannel == null || messageBody == null) {
        return;/*from   w ww  .ja  v a2 s  .c  om*/
    }
    String channel = new String(messageChannel);
    if (!(channel.endsWith(":del") || channel.endsWith(":expired"))) {
        return;
    }
    String body = new String(messageBody);
    if (!body.startsWith("spring:session:sessions:")) {
        return;
    }

    int beginIndex = body.lastIndexOf(":") + 1;
    int endIndex = body.length();
    String sessionId = body.substring(beginIndex, endIndex);

    if (logger.isDebugEnabled()) {
        logger.debug("Publishing SessionDestroyedEvent for session " + sessionId);
    }

    if (channel.endsWith(":del")) {
        publishEvent(new SessionDeletedEvent(this, sessionId));
    } else {
        publishEvent(new SessionExpiredEvent(this, sessionId));
    }
}