DATAKV-22

+ add message abstraction
This commit is contained in:
Costin Leau
2011-01-12 22:40:08 +02:00
parent 1386f5bd96
commit 871802a1cb
5 changed files with 87 additions and 12 deletions

View File

@@ -0,0 +1,44 @@
/*
* Copyright 2011 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.data.keyvalue.redis;
import org.springframework.data.keyvalue.redis.connection.Message;
/**
* Default message implementation.
*
* @author Costin Leau
*/
public class DefaultMessage implements Message {
private final byte[] payload;
private final byte[] channel;
public DefaultMessage(byte[] payload, byte[] channel) {
this.payload = payload;
this.channel = channel;
}
@Override
public byte[] getChannel() {
return channel;
}
@Override
public byte[] getPayload() {
return payload;
}
}

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2011 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.data.keyvalue.redis.connection;
import java.io.Serializable;
/**
* Class encapsulating a Redis message body and its properties.
*
* @author Costin Leau
*/
public interface Message extends Serializable {
byte[] getPayload();
byte[] getChannel();
}

View File

@@ -26,8 +26,7 @@ public interface MessageListener {
* Callback for processing received objects through Redis.
*
* @param message message
* @param channel Redis channel
* @param pattern channel pattern - matching pattern (if used), null otherwise.
* @param pattern pattern matching the channel (if specified) - can be null
*/
void onMessage(byte[] message, byte[] channel, byte[] pattern);
void onMessage(Message message, byte[] pattern);
}

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.data.keyvalue.redis.connection.jedis;
import org.springframework.data.keyvalue.redis.DefaultMessage;
import org.springframework.data.keyvalue.redis.connection.MessageListener;
import org.springframework.util.Assert;
@@ -36,12 +37,12 @@ class JedisMessageListener extends JedisPubSub {
@Override
public void onMessage(String channel, String message) {
listener.onMessage(message.getBytes(), channel.getBytes(), null);
listener.onMessage(new DefaultMessage(message.getBytes(), channel.getBytes()), null);
}
@Override
public void onPMessage(String pattern, String channel, String message) {
listener.onMessage(message.getBytes(), channel.getBytes(), pattern.getBytes());
listener.onMessage(new DefaultMessage(message.getBytes(), channel.getBytes()), pattern.getBytes());
}
@Override

View File

@@ -21,6 +21,7 @@ import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.data.keyvalue.redis.SettingsUtils;
import org.springframework.data.keyvalue.redis.connection.AbstractConnectionIntegrationTests;
import org.springframework.data.keyvalue.redis.connection.Message;
import org.springframework.data.keyvalue.redis.connection.MessageListener;
import org.springframework.data.keyvalue.redis.connection.RedisConnectionFactory;
@@ -51,10 +52,10 @@ public class JedisConnectionIntegrationTests extends AbstractConnectionIntegrati
MessageListener listener = new MessageListener() {
@Override
public void onMessage(byte[] message, byte[] channel, byte[] pattern) {
assertArrayEquals(expectedChannel, channel);
assertArrayEquals(expectedMessage, message);
System.out.println("Received message '" + new String(message) + "'");
public void onMessage(Message message, byte[] pattern) {
assertArrayEquals(expectedChannel, message.getChannel());
assertArrayEquals(expectedMessage, message.getPayload());
System.out.println("Received message '" + new String(message.getPayload()) + "'");
}
};
@@ -89,10 +90,10 @@ public class JedisConnectionIntegrationTests extends AbstractConnectionIntegrati
MessageListener listener = new MessageListener() {
@Override
public void onMessage(byte[] message, byte[] channel, byte[] pattern) {
public void onMessage(Message message, byte[] pattern) {
assertArrayEquals(expectedPattern, pattern);
assertArrayEquals(expectedMessage, message);
System.out.println("Received message '" + new String(message) + "'");
assertArrayEquals(expectedMessage, message.getPayload());
System.out.println("Received message '" + new String(message.getPayload()) + "'");
}
};