INT-2029 added support for dealing with Non Serializable message data. Added tests for non-String Messages

This commit is contained in:
Oleg Zhurakousky
2011-08-09 11:58:59 -04:00
parent 7b0cd7329b
commit 09d4707104
2 changed files with 75 additions and 1 deletions

View File

@@ -29,6 +29,7 @@ import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import org.springframework.integration.Message;
import org.springframework.integration.store.MessageStore;
import org.springframework.integration.store.MessageStoreException;
import org.springframework.util.Assert;
/**
@@ -63,7 +64,14 @@ public class RedisMessageStore implements MessageStore, InitializingBean{
public <T> Message<T> addMessage(Message<T> message) {
Assert.notNull(message, "'message' must not be null");
BoundValueOperations<UUID, Message<?>> ops = redisTemplate.boundValueOps(message.getHeaders().getId());
ops.set(message);
try {
ops.set(message);
} catch (SerializationException e) {
throw new MessageStoreException(message, "It seems like while relying on the default RedisSerializer (JdkSerializationRedisSerializer) " +
"the Message contains data that is not Serializable. Either make it Serializable or provide your own implementation of " +
"RedisSerializer via 'setValueSerializer(..)'", e);
}
return (Message<T>) ops.get();
}

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.integration.redis.store;
import java.io.Serializable;
import java.util.UUID;
import org.junit.Test;
@@ -23,6 +24,7 @@ import org.springframework.integration.Message;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.redis.rules.RedisAvailable;
import org.springframework.integration.redis.rules.RedisAvailableTests;
import org.springframework.integration.store.MessageStoreException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@@ -63,6 +65,31 @@ public class RedisMessageStoreTests extends RedisAvailableTests {
assertEquals("Hello Redis", storedMessage.getPayload());
}
@Test
@RedisAvailable
public void testAddSerializableObjectMessage(){
JedisConnectionFactory jcf = this.getConnectionFactoryForTest();
RedisMessageStore store = new RedisMessageStore(jcf);
Address address = new Address();
address.setAddress("1600 Pennsylvania Av, Washington, DC");
Person person = new Person(address, "Barak Obama");
Message<Person> objectMessage = new GenericMessage<Person>(person);
Message<Person> storedMessage = store.addMessage(objectMessage);
assertNotSame(objectMessage, storedMessage);
assertEquals("Barak Obama", storedMessage.getPayload().getName());
}
@Test(expected=MessageStoreException.class)
@RedisAvailable
public void testAddNonSerializableObjectMessage(){
JedisConnectionFactory jcf = this.getConnectionFactoryForTest();
RedisMessageStore store = new RedisMessageStore(jcf);
Message<Foo> objectMessage = new GenericMessage<Foo>(new Foo());
store.addMessage(objectMessage);
}
@SuppressWarnings("unchecked")
@Test
@RedisAvailable
@@ -88,6 +115,7 @@ public class RedisMessageStoreTests extends RedisAvailableTests {
assertEquals("Hello Redis", retrievedMessage.getPayload());
assertNull(store.getMessage(stringMessage.getHeaders().getId()));
}
@Test(expected=IllegalArgumentException.class)
@RedisAvailable
public void testRemoveNonExistingMessage(){
@@ -95,4 +123,42 @@ public class RedisMessageStoreTests extends RedisAvailableTests {
RedisMessageStore store = new RedisMessageStore(jcf);
store.removeMessage(UUID.randomUUID());
}
@SuppressWarnings("serial")
public static class Person implements Serializable{
private Address address;
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private String name;
public Person(Address address, String name){
this.address = address;
this.name = name;
}
}
@SuppressWarnings("serial")
public static class Address implements Serializable{
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
public static class Foo{
}
}