Java Code Examples for org.springframework.data.redis.connection.Message#getBody()

The following examples show how to use org.springframework.data.redis.connection.Message#getBody() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MessageScheduler.java    From rqueue with Apache License 2.0 6 votes vote down vote up
@Override
public void onMessage(Message message, byte[] pattern) {
  if (message.getBody().length == 0 || message.getChannel().length == 0) {
    return;
  }
  String body = new String(message.getBody());
  String channel = new String(message.getChannel());
  getLogger().debug("Body: {} Channel: {}", body, channel);
  try {
    Long startTime = Long.parseLong(body);
    String queueName = channelNameToQueueName.get(channel);
    if (queueName == null) {
      getLogger().warn("Unknown channel name {}", channel);
      return;
    }
    schedule(queueName, startTime, false);
  } catch (Exception e) {
    getLogger().error("Error occurred on a channel {}, body: {}", channel, body, e);
  }
}
 
Example 2
Source File: SpringRedisMessageListener.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
@Override
public void onMessage(Message message, byte[] pattern) {
    byte[] messageChannel = message.getChannel();
    byte[] messageBody = message.getBody();
    if (messageChannel == null || messageBody == null) {
        return;
    }
    try {
        Command cmd = Command.parse(String.valueOf(SerializationUtils.deserialize(messageBody)));
        if (cmd == null || isLocalCommand(cmd)) { return; }

        switch (cmd.getOperator()) {
            case Command.OPT_JOIN:
                logger.info("Node-" + cmd.getSrc() + " joined to " + this.channel);
                break;
            case Command.OPT_EVICT_KEY:
                clusterPolicy.evict(cmd.getRegion(), cmd.getKeys());
                logger.debug("Received cache evict message, region=" + cmd.getRegion() + ",key="
                    + String.join(",", cmd.getKeys()));
                break;
            case Command.OPT_CLEAR_KEY:
                clusterPolicy.clear(cmd.getRegion());
                logger.debug("Received cache clear message, region=" + cmd.getRegion());
                break;
            case Command.OPT_QUIT:
                logger.info("Node-" + cmd.getSrc() + " quit to " + this.channel);
                break;
            default:
                logger.warn("Unknown message type = " + cmd.getOperator());
        }
    } catch (Exception e) {
        logger.error("Failed to handle received msg", e);
    }
}
 
Example 3
Source File: SpringRedisMessageListener.java    From J2Cache with Apache License 2.0 5 votes vote down vote up
@Override
public void onMessage(Message message, byte[] pattern) {
	byte[] messageChannel = message.getChannel();
	byte[] messageBody = message.getBody();
	if (messageChannel == null || messageBody == null) {
		return;
	}
       try {
           Command cmd = Command.parse(String.valueOf(SerializationUtils.deserialize(messageBody)));
           if (cmd == null || isLocalCommand(cmd))
               return;

           switch (cmd.getOperator()) {
               case Command.OPT_JOIN:
               	logger.info("Node-"+cmd.getSrc()+" joined to " + this.channel);
                   break;
               case Command.OPT_EVICT_KEY:
               	clusterPolicy.evict(cmd.getRegion(), cmd.getKeys());
                   logger.debug("Received cache evict message, region=" + cmd.getRegion() + ",key=" + String.join(",", cmd.getKeys()));
                   break;
               case Command.OPT_CLEAR_KEY:
               	clusterPolicy.clear(cmd.getRegion());
                   logger.debug("Received cache clear message, region=" + cmd.getRegion());
                   break;
               case Command.OPT_QUIT:
               	logger.info("Node-"+cmd.getSrc()+" quit to " + this.channel);
                   break;
               default:
               	logger.warn("Unknown message type = " + cmd.getOperator());
           }
       } catch (Exception e) {
       	logger.error("Failed to handle received msg", e);
       }
}
 
Example 4
Source File: SpringRedisMessageListener.java    From J2Cache with Apache License 2.0 5 votes vote down vote up
@Override
public void onMessage(Message message, byte[] pattern) {
	byte[] messageChannel = message.getChannel();
	byte[] messageBody = message.getBody();
	if (messageChannel == null || messageBody == null) {
		return;
	}
       try {
           Command cmd = Command.parse(String.valueOf(SerializationUtils.deserialize(messageBody)));
           if (cmd == null || isLocalCommand(cmd))
               return;

           switch (cmd.getOperator()) {
               case Command.OPT_JOIN:
               	logger.info("Node-"+cmd.getSrc()+" joined to " + this.channel);
                   break;
               case Command.OPT_EVICT_KEY:
               	clusterPolicy.evict(cmd.getRegion(), cmd.getKeys());
                   logger.debug("Received cache evict message, region=" + cmd.getRegion() + ",key=" + String.join(",", cmd.getKeys()));
                   break;
               case Command.OPT_CLEAR_KEY:
               	clusterPolicy.clear(cmd.getRegion());
                   logger.debug("Received cache clear message, region=" + cmd.getRegion());
                   break;
               case Command.OPT_QUIT:
               	logger.info("Node-"+cmd.getSrc()+" quit to " + this.channel);
                   break;
               default:
               	logger.warn("Unknown message type = " + cmd.getOperator());
           }
       } catch (Exception e) {
       	logger.error("Failed to handle received msg", e);
       }
}
 
Example 5
Source File: RedisMessageListener.java    From spring-cloud-shop with MIT License 5 votes vote down vote up
@Override
public void onMessage(@NonNull Message message, @Nullable byte[] pattern) {

    byte[] body = message.getBody();//请使用valueSerializer
    byte[] channel = message.getChannel();
    String msg = stringRedisSerializer.deserialize(body);
    String topic = stringRedisSerializer.deserialize(channel);
    System.out.println("我是sub,监听" + topic + ",我收到消息:" + msg);

}
 
Example 6
Source File: TokenRepositoryImpl.java    From authlib-agent with MIT License 5 votes vote down vote up
@PostConstruct
private void registerExpiredEventListener() {
	expiredListener = new MessageListener() {

		@Override
		public void onMessage(Message message, byte[] pattern) {
			byte[] body = message.getBody();
			if (body == null) {
				return;
			}

			String key;
			try {
				key = new String(message.getBody(), "UTF-8");
			} catch (UnsupportedEncodingException e) {
				LOGGER.debug(() -> "failed to decode message body: " + bytesToHex(body), e);
				return;
			}

			if (!key.startsWith(PREFIX_EXPIRE)) {
				return;
			}

			String accessToken = key.substring(PREFIX_EXPIRE.length());
			template.delete(keyAccessToken(accessToken));
			Map<String, String> values = hashOps.entries(keyAccessToken(accessToken));
			if (values != null && !values.isEmpty()) {
				setOps.remove(keyClientToken(values.get(KEY_CLIENT_TOKEN)), accessToken);
				setOps.remove(keyAccount(values.get(KEY_OWNER)), accessToken);
			}
		}
	};
	container.addMessageListener(expiredListener, new PatternTopic("__keyevent@*__:expired"));
}
 
Example 7
Source File: RedisIndexedSessionRepository.java    From spring-session with Apache License 2.0 4 votes vote down vote up
@Override
public void onMessage(Message message, byte[] pattern) {
	byte[] messageChannel = message.getChannel();
	byte[] messageBody = message.getBody();

	String channel = new String(messageChannel);

	if (channel.startsWith(this.sessionCreatedChannelPrefix)) {
		// TODO: is this thread safe?
		@SuppressWarnings("unchecked")
		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.equals(this.sessionDeletedChannel);
	if (isDeleted || channel.equals(this.sessionExpiredChannel)) {
		int beginIndex = body.lastIndexOf(":") + 1;
		int endIndex = body.length();
		String sessionId = body.substring(beginIndex, endIndex);

		RedisSession session = getSession(sessionId, true);

		if (session == null) {
			logger.warn("Unable to publish SessionDestroyedEvent for session " + sessionId);
			return;
		}

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

		cleanupPrincipalIndex(session);

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