INT-4267: Add JSON (De)Serializers for Messaging
JIRA: https://jira.spring.io/browse/INT-4267 Fixes: spring-projects/spring-integration#2110 The documentation clearly point that we can simply use JSON (de)serialization with the `RedisMessageStore`, but actually it isn't so easy * Fix `MessageGroupMetadata`, `MessageHolder`, `MessageMetadata` for Jackson deserialization compatibility * Add `MessageHeaders`-based ctor to the `AdviceMessage` * Add `MessageHeadersJacksonSerializer` to serialize `MessageHeaders` to the `HashMap` for easier deserialization afterwards * Add deserializer implementations for all `Message` types * Add convenient `JsonObjectMapperProvider#jacksonMessageAwareMapper()` factory method to build `ObjectMapper` supplied with mentioned above (de)serializers **Cherry-pick to 4.3.10 without `MessageHolder` and `MessageMetadata`** Address PR comments and document the feature Doc Polishing Conflicts: spring-integration-core/src/main/java/org/springframework/integration/store/MessageHolder.java spring-integration-core/src/main/java/org/springframework/integration/store/MessageMetadata.java Resolved.
This commit is contained in:
committed by
Gary Russell
parent
194d710c3b
commit
376405c39b
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2007-2016 the original author or authors.
|
||||
* Copyright 2007-2017 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.
|
||||
@@ -16,16 +16,20 @@
|
||||
|
||||
package org.springframework.integration.redis.store;
|
||||
|
||||
import static org.hamcrest.Matchers.instanceOf;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNotSame;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@@ -38,18 +42,25 @@ import org.junit.Test;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
|
||||
import org.springframework.integration.channel.DirectChannel;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.history.MessageHistory;
|
||||
import org.springframework.integration.message.AdviceMessage;
|
||||
import org.springframework.integration.redis.rules.RedisAvailable;
|
||||
import org.springframework.integration.redis.rules.RedisAvailableTests;
|
||||
import org.springframework.integration.store.MessageGroup;
|
||||
import org.springframework.integration.store.SimpleMessageGroup;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.integration.support.MutableMessage;
|
||||
import org.springframework.integration.support.json.JacksonJsonUtils;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageChannel;
|
||||
import org.springframework.messaging.support.ErrorMessage;
|
||||
import org.springframework.messaging.support.GenericMessage;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import junit.framework.AssertionFailedError;
|
||||
|
||||
/**
|
||||
@@ -421,4 +432,34 @@ public class RedisMessageGroupStoreTests extends RedisAvailableTests {
|
||||
messageStore.removeMessageGroup("X");
|
||||
}
|
||||
|
||||
@Test
|
||||
@RedisAvailable
|
||||
public void testJsonSerialization() throws Exception {
|
||||
RedisConnectionFactory jcf = getConnectionFactoryForTest();
|
||||
RedisMessageStore store = new RedisMessageStore(jcf);
|
||||
|
||||
ObjectMapper mapper = JacksonJsonUtils.messagingAwareMapper();
|
||||
|
||||
GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer(mapper);
|
||||
store.setValueSerializer(serializer);
|
||||
|
||||
Message<?> genericMessage = new GenericMessage<>(new Date());
|
||||
Message<?> mutableMessage = new MutableMessage<>(UUID.randomUUID());
|
||||
Message<?> adviceMessage = new AdviceMessage<>("foo", genericMessage);
|
||||
ErrorMessage errorMessage = new ErrorMessage(new RuntimeException("test exception"));
|
||||
|
||||
store.addMessagesToGroup(1, genericMessage, mutableMessage, adviceMessage, errorMessage);
|
||||
|
||||
MessageGroup messageGroup = store.getMessageGroup(1);
|
||||
assertEquals(4, messageGroup.size());
|
||||
List<Message<?>> messages = new ArrayList<>(messageGroup.getMessages());
|
||||
assertEquals(genericMessage.getPayload(), messages.get(0).getPayload());
|
||||
assertEquals(mutableMessage.getPayload(), messages.get(1).getPayload());
|
||||
assertEquals(adviceMessage.getPayload(), messages.get(2).getPayload());
|
||||
Message<?> errorMessageResult = messages.get(3);
|
||||
assertThat(errorMessageResult, instanceOf(ErrorMessage.class));
|
||||
assertEquals(errorMessage.getPayload().getMessage(),
|
||||
((ErrorMessage) errorMessageResult).getPayload().getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user