INT-3325 Add Redis Channel Message Group Store
JIRA: https://jira.spring.io/browse/INT-3325 JIRA: https://jira.spring.io/browse/INT-1870 Optimized MGS for QueueChannel - uses a LIST for each channel and LPUSH, RPOP. * Also fix MutableMessage to be Serializable INT-1870 Priority Redis Channel Message Store Supports priorities 0-9 (+ no priority). Priorities out of that range are treated as no priority. Polishing - Add Marker Interfaces * Emit a `WARN` log if a channel is used with a regular MessageGroupStore * Allow message-store on namespace when defining a priority channel INT-3325 Polishing; PR Comments Fix some typos in JavaDocs and Docs
This commit is contained in:
committed by
Artem Bilan
parent
a9faa5836f
commit
a8c8a4fed5
@@ -0,0 +1,135 @@
|
||||
/*
|
||||
* Copyright 2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.integration.redis.store;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.beans.factory.BeanNameAware;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
|
||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
import org.springframework.integration.store.ChannelMessageStore;
|
||||
import org.springframework.integration.store.MessageGroup;
|
||||
import org.springframework.integration.store.SimpleMessageGroup;
|
||||
import org.springframework.jmx.export.annotation.ManagedAttribute;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Specialized Redis {@link ChannelMessageStore} that uses a list to back a QueueChannel.
|
||||
* <p>
|
||||
* Requires {@link #setBeanName(String)} which is used as part of the key.
|
||||
*
|
||||
* @author Gary Russell
|
||||
* @since 4.0
|
||||
*
|
||||
*/
|
||||
public class RedisChannelMessageStore implements ChannelMessageStore, BeanNameAware, InitializingBean {
|
||||
|
||||
private final RedisTemplate<Object, Message<?>> redisTemplate;
|
||||
|
||||
private String beanName;
|
||||
|
||||
/**
|
||||
* Construct a message store that uses Java Serialization for messages.
|
||||
*
|
||||
* @param connectionFactory The redis connection factory.
|
||||
*/
|
||||
public RedisChannelMessageStore(RedisConnectionFactory connectionFactory) {
|
||||
this.redisTemplate = new RedisTemplate<Object, Message<?>>();
|
||||
this.redisTemplate.setConnectionFactory(connectionFactory);
|
||||
this.redisTemplate.setKeySerializer(new StringRedisSerializer());
|
||||
this.redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
|
||||
this.redisTemplate.afterPropertiesSet();
|
||||
}
|
||||
|
||||
/**
|
||||
* Use a different serializer (default {@link JdkSerializationRedisSerializer} for
|
||||
* the {@link Message}.
|
||||
*
|
||||
* @param valueSerializer The value serializer.
|
||||
*/
|
||||
public void setValueSerializer(RedisSerializer<?> valueSerializer) {
|
||||
Assert.notNull(valueSerializer, "'valueSerializer' must not be null");
|
||||
this.redisTemplate.setValueSerializer(valueSerializer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBeanName(String name) {
|
||||
Assert.notNull(name, "'beanName' must not be null");
|
||||
this.beanName = name;
|
||||
}
|
||||
|
||||
protected String getBeanName() {
|
||||
return beanName;
|
||||
}
|
||||
|
||||
protected RedisTemplate<Object, Message<?>> getRedisTemplate() {
|
||||
return redisTemplate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
Assert.notNull(this.beanName, "'beanName' must not be null");
|
||||
}
|
||||
|
||||
@Override
|
||||
@ManagedAttribute
|
||||
public int messageGroupSize(Object groupId) {
|
||||
return (int) this.redisTemplate.boundListOps(groupId).size().longValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public MessageGroup getMessageGroup(Object groupId) {
|
||||
List<Message<?>> messages = this.redisTemplate.boundListOps(groupId).range(0, -1);
|
||||
return new SimpleMessageGroup(messages, groupId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MessageGroup addMessageToGroup(Object groupId, Message<?> message) {
|
||||
this.redisTemplate.boundListOps(groupId).leftPush(message);
|
||||
return null;
|
||||
}
|
||||
|
||||
public void removeMessageGroup(Object groupId) {
|
||||
this.redisTemplate.boundListOps(groupId).trim(1, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Message<?> pollMessageFromGroup(Object groupId) {
|
||||
return this.redisTemplate.boundListOps(groupId).rightPop();
|
||||
}
|
||||
|
||||
@ManagedAttribute
|
||||
public int getMessageCountForAllMessageGroups() {
|
||||
Set<?> keys = this.redisTemplate.keys(this.beanName + ":*");
|
||||
int count = 0;
|
||||
for (Object key : keys) {
|
||||
count += this.messageGroupSize(key);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
@ManagedAttribute
|
||||
public int getMessageGroupCount() {
|
||||
return this.redisTemplate.keys(this.beanName + ":*").size();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
/*
|
||||
* Copyright 2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.integration.redis.store;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.integration.IntegrationMessageHeaderAccessor;
|
||||
import org.springframework.integration.store.MessageGroup;
|
||||
import org.springframework.integration.store.PriorityCapableChannelMessageStore;
|
||||
import org.springframework.integration.store.SimpleMessageGroup;
|
||||
import org.springframework.jmx.export.annotation.ManagedAttribute;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Specialized Redis {@link PriorityCapableChannelMessageStore} that uses lists to back a QueueChannel.
|
||||
* Messages are removed in priority order ({@link IntegrationMessageHeaderAccessor#PRIORITY}).
|
||||
* Priorities 0-9 are supported; higher values are treated with the same priority (none)
|
||||
* as messages with no priority header (retrieved after any messages that have a priority).
|
||||
* <p>
|
||||
* Requires that groupId is a String.
|
||||
*
|
||||
* @author Gary Russell
|
||||
* @since 4.0
|
||||
*
|
||||
*/
|
||||
public class RedisChannelPriorityMessageStore extends RedisChannelMessageStore implements PriorityCapableChannelMessageStore {
|
||||
|
||||
public RedisChannelPriorityMessageStore(RedisConnectionFactory connectionFactory) {
|
||||
super(connectionFactory);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPriorityEnabled() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ManagedAttribute
|
||||
public int messageGroupSize(Object groupId) {
|
||||
Assert.isInstanceOf(String.class, groupId);
|
||||
List<String> list = sortedKeys((String) groupId);
|
||||
int count = 0;
|
||||
for (String key : list) {
|
||||
count += this.getRedisTemplate().boundListOps(key).size();
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MessageGroup getMessageGroup(Object groupId) {
|
||||
Assert.isInstanceOf(String.class, groupId);
|
||||
List<Message<?>> allMessages = new LinkedList<Message<?>>();
|
||||
List<String> list = sortedKeys((String) groupId);
|
||||
for (String key : list) {
|
||||
List<Message<?>> messages = this.getRedisTemplate().boundListOps(key).range(0, -1);
|
||||
allMessages.addAll(messages);
|
||||
}
|
||||
return new SimpleMessageGroup(allMessages, groupId);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public MessageGroup addMessageToGroup(Object groupId, Message<?> message) {
|
||||
Assert.isInstanceOf(String.class, groupId);
|
||||
String key = (String) groupId;
|
||||
Integer priority = new IntegrationMessageHeaderAccessor(message).getPriority();
|
||||
if (priority != null && priority < 10 && priority >= 0) {
|
||||
key = key + ":" + priority;
|
||||
}
|
||||
else {
|
||||
key = key + ":z";
|
||||
}
|
||||
return super.addMessageToGroup(key, message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Message<?> pollMessageFromGroup(Object groupId) {
|
||||
Assert.isInstanceOf(String.class, groupId);
|
||||
List<String> list = sortedKeys((String) groupId);
|
||||
Message<?> message;
|
||||
for (String key : list) {
|
||||
message = super.pollMessageFromGroup(key);
|
||||
if (message != null) {
|
||||
return message;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private List<String> sortedKeys(String groupId) {
|
||||
Set<Object> keys = this.getRedisTemplate().keys(groupId == null ? (this.getBeanName() + ":*") : (groupId + "*"));
|
||||
List<String> list = new LinkedList<String>();
|
||||
for (Object key : keys) {
|
||||
Assert.isInstanceOf(String.class, key);
|
||||
list.add((String) key);
|
||||
}
|
||||
Collections.sort(list);
|
||||
return list;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ManagedAttribute
|
||||
public int getMessageGroupCount() {
|
||||
Set<Object> narrowedKeys = narrowedKeys();
|
||||
return narrowedKeys.size();
|
||||
}
|
||||
|
||||
|
||||
private Set<Object> narrowedKeys() {
|
||||
Set<Object> keys = this.getRedisTemplate().keys(this.getBeanName() + ":*");
|
||||
Set<Object> narrowedKeys = new HashSet<Object>();
|
||||
for (Object key : keys) {
|
||||
Assert.isInstanceOf(String.class, key);
|
||||
String keyString = (String) key;
|
||||
int lastIndexOfColon = keyString.lastIndexOf(":");
|
||||
if (keyString.indexOf(":") != lastIndexOfColon) {
|
||||
narrowedKeys.add(keyString.substring(0, lastIndexOfColon));
|
||||
}
|
||||
else {
|
||||
narrowedKeys.add(key);
|
||||
}
|
||||
}
|
||||
return narrowedKeys;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeMessageGroup(Object groupId) {
|
||||
Assert.isInstanceOf(String.class, groupId);
|
||||
List<String> list = sortedKeys((String) groupId);
|
||||
for (String key : list) {
|
||||
super.removeMessageGroup(key);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@ManagedAttribute
|
||||
public int getMessageCountForAllMessageGroups() {
|
||||
Set<Object> narrowedKeys = narrowedKeys();
|
||||
int count = 0;
|
||||
for (Object key : narrowedKeys) {
|
||||
count += this.messageGroupSize(key);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user