Search in sources :

Example 1 with KryoMessageConverter

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

the class AbstractBinderTests method testSendAndReceiveKryo.

@Test
@SuppressWarnings({ "rawtypes", "deprecation" })
public void testSendAndReceiveKryo() 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%s0x", getDestinationNameDelimiter()), moduleOutputChannel, outputBindingProperties.getProducer());
    Binding<MessageChannel> consumerBinding = binder.bindConsumer(String.format("foo%s0x", getDestinationNameDelimiter()), "testSendAndReceiveKryo", moduleInputChannel, inputBindingProperties.getConsumer());
    Foo foo = new Foo();
    foo.setName("Bill");
    Message<?> message = MessageBuilder.withPayload(foo).setHeader(MessageHeaders.CONTENT_TYPE, MessageConverterUtils.X_JAVA_OBJECT).build();
    // Let the consumer actually bind to the producer before sending a msg
    binderBindUnbindLatency();
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<Message<Foo>> inboundMessageRef = new AtomicReference<Message<Foo>>();
    moduleInputChannel.subscribe(message1 -> {
        try {
            inboundMessageRef.set((Message<Foo>) message1);
        } finally {
            latch.countDown();
        }
    });
    moduleOutputChannel.send(message);
    Assert.isTrue(latch.await(5, TimeUnit.SECONDS), "Failed to receive message");
    KryoMessageConverter kryo = new KryoMessageConverter(null, true);
    Foo fooPayload = (Foo) kryo.fromMessage(inboundMessageRef.get(), Foo.class);
    assertNotNull(fooPayload);
    assertThat(inboundMessageRef.get().getHeaders().get(BinderHeaders.BINDER_ORIGINAL_CONTENT_TYPE)).isNull();
    producerBinding.unbind();
    consumerBinding.unbind();
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) Message(org.springframework.messaging.Message) DirectChannel(org.springframework.integration.channel.DirectChannel) BindingProperties(org.springframework.cloud.stream.config.BindingProperties) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) KryoMessageConverter(org.springframework.cloud.stream.converter.KryoMessageConverter) Test(org.junit.Test)

Example 2 with KryoMessageConverter

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

the class ContentTypeTckTests method kryo_pojoToPojoContentTypeHeader.

@Test
@SuppressWarnings("deprecation")
public void kryo_pojoToPojoContentTypeHeader() {
    ApplicationContext context = new SpringApplicationBuilder(PojoToPojoStreamListener.class).web(WebApplicationType.NONE).run("--spring.jmx.enabled=false", "--spring.cloud.stream.bindings.output.contentType=application/x-java-object");
    InputDestination source = context.getBean(InputDestination.class);
    OutputDestination target = context.getBean(OutputDestination.class);
    KryoMessageConverter converter = new KryoMessageConverter(null, true);
    @SuppressWarnings("unchecked") Message<byte[]> message = (Message<byte[]>) converter.toMessage(new Person("oleg"), new MessageHeaders(Collections.singletonMap(MessageHeaders.CONTENT_TYPE, MessageConverterUtils.X_JAVA_OBJECT)));
    source.send(message);
    Message<byte[]> outputMessage = target.receive();
    assertNotNull(outputMessage);
    MimeType contentType = (MimeType) outputMessage.getHeaders().get(MessageHeaders.CONTENT_TYPE);
    assertEquals("x-java-object", contentType.getSubtype());
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) OutputDestination(org.springframework.cloud.stream.binder.test.OutputDestination) SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) MessageHeaders(org.springframework.messaging.MessageHeaders) InputDestination(org.springframework.cloud.stream.binder.test.InputDestination) MimeType(org.springframework.util.MimeType) KryoMessageConverter(org.springframework.cloud.stream.converter.KryoMessageConverter) Test(org.junit.Test)

Example 3 with KryoMessageConverter

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

the class ContentTypeTckTests method kryo_pojoToPojo.

@Test
@SuppressWarnings("deprecation")
public void kryo_pojoToPojo() {
    ApplicationContext context = new SpringApplicationBuilder(PojoToPojoStreamListener.class).web(WebApplicationType.NONE).run("--spring.cloud.stream.default.contentType=application/x-java-object", "--spring.jmx.enabled=false");
    InputDestination source = context.getBean(InputDestination.class);
    OutputDestination target = context.getBean(OutputDestination.class);
    KryoMessageConverter converter = new KryoMessageConverter(null, true);
    @SuppressWarnings("unchecked") Message<byte[]> message = (Message<byte[]>) converter.toMessage(new Person("oleg"), new MessageHeaders(Collections.singletonMap(MessageHeaders.CONTENT_TYPE, MessageConverterUtils.X_JAVA_OBJECT)));
    source.send(new GenericMessage<>(message.getPayload()));
    Message<byte[]> outputMessage = target.receive();
    assertNotNull(outputMessage);
    MimeType contentType = (MimeType) outputMessage.getHeaders().get(MessageHeaders.CONTENT_TYPE);
    assertEquals("x-java-object", contentType.getSubtype());
    assertEquals(Person.class.getName(), contentType.getParameters().get("type"));
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) OutputDestination(org.springframework.cloud.stream.binder.test.OutputDestination) SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) MessageHeaders(org.springframework.messaging.MessageHeaders) InputDestination(org.springframework.cloud.stream.binder.test.InputDestination) MimeType(org.springframework.util.MimeType) KryoMessageConverter(org.springframework.cloud.stream.converter.KryoMessageConverter) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)3 KryoMessageConverter (org.springframework.cloud.stream.converter.KryoMessageConverter)3 Message (org.springframework.messaging.Message)3 SpringApplicationBuilder (org.springframework.boot.builder.SpringApplicationBuilder)2 InputDestination (org.springframework.cloud.stream.binder.test.InputDestination)2 OutputDestination (org.springframework.cloud.stream.binder.test.OutputDestination)2 ApplicationContext (org.springframework.context.ApplicationContext)2 MessageHeaders (org.springframework.messaging.MessageHeaders)2 GenericMessage (org.springframework.messaging.support.GenericMessage)2 MimeType (org.springframework.util.MimeType)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 BindingProperties (org.springframework.cloud.stream.config.BindingProperties)1 DirectChannel (org.springframework.integration.channel.DirectChannel)1 MessageChannel (org.springframework.messaging.MessageChannel)1