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();
}
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());
}
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"));
}
Aggregations