Search in sources :

Example 1 with JavaSerializationMessageConverter

use of org.springframework.cloud.stream.converter.JavaSerializationMessageConverter in project spring-cloud-stream by spring-cloud.

the class AbstractBinderTests method testSendAndReceiveJavaSerialization.

@Test
@SuppressWarnings({ "rawtypes", "deprecation" })
public void testSendAndReceiveJavaSerialization() throws Exception {
    Binder binder = getBinder();
    BindingProperties outputBindingProperties = createProducerBindingProperties(createProducerProperties());
    DirectChannel moduleOutputChannel = createBindableChannel("output", outputBindingProperties);
    BindingProperties inputBindingProperties = createConsumerBindingProperties(createConsumerProperties());
    DirectChannel moduleInputChannel = createBindableChannel("input", inputBindingProperties);
    Binding<MessageChannel> producerBinding = binder.bindProducer(String.format("foo%s0y", getDestinationNameDelimiter()), moduleOutputChannel, outputBindingProperties.getProducer());
    Binding<MessageChannel> consumerBinding = binder.bindConsumer(String.format("foo%s0y", getDestinationNameDelimiter()), "testSendAndReceiveJavaSerialization", moduleInputChannel, inputBindingProperties.getConsumer());
    SerializableFoo foo = new SerializableFoo();
    Message<?> message = MessageBuilder.withPayload(foo).setHeader(MessageHeaders.CONTENT_TYPE, MessageConverterUtils.X_JAVA_SERIALIZED_OBJECT).build();
    // Let the consumer actually bind to the producer before sending a msg
    binderBindUnbindLatency();
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<Message<byte[]>> inboundMessageRef = new AtomicReference<Message<byte[]>>();
    moduleInputChannel.subscribe(message1 -> {
        try {
            inboundMessageRef.set((Message<byte[]>) message1);
        } finally {
            latch.countDown();
        }
    });
    moduleOutputChannel.send(message);
    Assert.isTrue(latch.await(5, TimeUnit.SECONDS), "Failed to receive message");
    JavaSerializationMessageConverter converter = new JavaSerializationMessageConverter();
    SerializableFoo serializableFoo = (SerializableFoo) converter.convertFromInternal(inboundMessageRef.get(), SerializableFoo.class, null);
    assertNotNull(serializableFoo);
    assertThat(inboundMessageRef.get().getHeaders().get(BinderHeaders.BINDER_ORIGINAL_CONTENT_TYPE)).isNull();
    assertThat(inboundMessageRef.get().getHeaders().get(MessageHeaders.CONTENT_TYPE)).isEqualTo(MessageConverterUtils.X_JAVA_SERIALIZED_OBJECT);
    producerBinding.unbind();
    consumerBinding.unbind();
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) Message(org.springframework.messaging.Message) DirectChannel(org.springframework.integration.channel.DirectChannel) JavaSerializationMessageConverter(org.springframework.cloud.stream.converter.JavaSerializationMessageConverter) BindingProperties(org.springframework.cloud.stream.config.BindingProperties) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Test (org.junit.Test)1 BindingProperties (org.springframework.cloud.stream.config.BindingProperties)1 JavaSerializationMessageConverter (org.springframework.cloud.stream.converter.JavaSerializationMessageConverter)1 DirectChannel (org.springframework.integration.channel.DirectChannel)1 Message (org.springframework.messaging.Message)1 MessageChannel (org.springframework.messaging.MessageChannel)1