org.springframework.cloud.stream.messaging.Processor Java Examples

The following examples show how to use org.springframework.cloud.stream.messaging.Processor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StreamListenerMethodReturnWithConversionTests.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testReturnNoConversion() throws Exception {
	ConfigurableApplicationContext context = SpringApplication.run(
			this.configClass, "--server.port=0", "--spring.jmx.enabled=false");
	MessageCollector collector = context.getBean(MessageCollector.class);
	Processor processor = context.getBean(Processor.class);
	String id = UUID.randomUUID().toString();
	processor.input()
			.send(MessageBuilder.withPayload("{\"foo\":\"barbar" + id + "\"}")
					.setHeader("contentType", "application/json").build());
	TestPojoWithMimeType testPojoWithMimeType = context
			.getBean(TestPojoWithMimeType.class);
	Assertions.assertThat(testPojoWithMimeType.receivedPojos).hasSize(1);
	Assertions.assertThat(testPojoWithMimeType.receivedPojos.get(0))
			.hasFieldOrPropertyWithValue("foo", "barbar" + id);
	Message<String> message = (Message<String>) collector
			.forChannel(processor.output()).poll(1, TimeUnit.SECONDS);
	assertThat(message).isNotNull();
	StreamListenerTestUtils.BarPojo barPojo = this.mapper.readValue(
			message.getPayload(), StreamListenerTestUtils.BarPojo.class);
	assertThat(barPojo.getBar()).isEqualTo("barbar" + id);
	assertThat(message.getHeaders().get(MessageHeaders.CONTENT_TYPE,
			MimeType.class) != null);
	context.close();
}
 
Example #2
Source File: InputOutputBindingOrderTest.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testInputOutputBindingOrder() {
	ConfigurableApplicationContext applicationContext = new SpringApplicationBuilder(
			TestSource.class).web(WebApplicationType.NONE).run(
					"--spring.cloud.stream.defaultBinder=mock",
					"--spring.jmx.enabled=false");
	Binder binder = applicationContext.getBean(BinderFactory.class).getBinder(null,
			MessageChannel.class);
	Processor processor = applicationContext.getBean(Processor.class);
	// input is bound after the context has been started
	verify(binder).bindConsumer(eq("input"), isNull(), eq(processor.input()),
			Mockito.any());
	SomeLifecycle someLifecycle = applicationContext.getBean(SomeLifecycle.class);
	assertThat(someLifecycle.isRunning());
	applicationContext.close();
	assertThat(someLifecycle.isRunning()).isFalse();
	applicationContext.close();
}
 
Example #3
Source File: StreamListenerHandlerMethodTests.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
@StreamListener
public void receive(@Input(Processor.INPUT) SubscribableChannel input,
		@Output(Processor.OUTPUT) final MessageChannel output1,
		@Output(StreamListenerTestUtils.FooOutboundChannel1.OUTPUT) final MessageChannel output2) {
	input.subscribe(new MessageHandler() {
		@Override
		public void handleMessage(Message<?> message) throws MessagingException {
			if (message.getHeaders().get("output").equals("output1")) {
				output1.send(org.springframework.messaging.support.MessageBuilder
						.withPayload(
								message.getPayload().toString().toUpperCase())
						.build());
			}
			else if (message.getHeaders().get("output").equals("output2")) {
				output2.send(org.springframework.messaging.support.MessageBuilder
						.withPayload(
								message.getPayload().toString().toLowerCase())
						.build());
			}
		}
	});
}
 
Example #4
Source File: StreamListenerHandlerMethodTests.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testMethodWithObjectAsMethodArgument() throws Exception {
	ConfigurableApplicationContext context = SpringApplication.run(
			TestMethodWithObjectAsMethodArgument.class, "--server.port=0",
			"--spring.jmx.enabled=false",
			"--spring.cloud.stream.bindings.input.contentType=text/plain",
			"--spring.cloud.stream.bindings.output.contentType=text/plain");
	Processor processor = context.getBean(Processor.class);
	final String testMessage = "testing";
	processor.input().send(MessageBuilder.withPayload(testMessage).build());
	MessageCollector messageCollector = context.getBean(MessageCollector.class);
	Message<String> result = (Message<String>) messageCollector
			.forChannel(processor.output()).poll(1000, TimeUnit.MILLISECONDS);
	assertThat(result).isNotNull();
	assertThat(result.getPayload()).isEqualTo(testMessage.toUpperCase());
	context.close();
}
 
Example #5
Source File: ScriptableTransformProcessorConfiguration.java    From spring-cloud-stream-app-starters with Apache License 2.0 6 votes vote down vote up
@Bean
@Transformer(inputChannel = Processor.INPUT, outputChannel = Processor.OUTPUT)
public MessageProcessor<?> transformer() {
	String language = this.properties.getLanguage();
	String script = this.properties.getScript();
	logger.info(String.format("Input script is '%s', language is '%s'", script, language));
	Resource scriptResource = new ByteArrayResource(decodeScript(script).getBytes()) {

		// TODO until INT-3976
		@Override
		public String getFilename() {
			// Only the groovy script processor enforces this requirement for a name
			return "StaticScript";
		}

	};
	return Scripts.script(scriptResource)
			.lang(language)
			.variableGenerator(this.scriptVariableGenerator)
			.get();
}
 
Example #6
Source File: ContentTypeTckTests.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
@ServiceActivator(inputChannel = Processor.INPUT, outputChannel = Processor.OUTPUT)
public Message<String> echo(Message<byte[]> value) throws Exception {
	ObjectMapper mapper = new ObjectMapper();
	Person person = mapper.readValue(value.getPayload(), Person.class);
	person.setName("bob");
	String json = mapper.writeValueAsString(person);
	return MessageBuilder.withPayload(json).build();
}
 
Example #7
Source File: StreamListenerHandlerMethodTests.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testMethodHeadersNotPropagatged() throws Exception {
	ConfigurableApplicationContext context = SpringApplication.run(
			TestMethodHeadersNotPropagated.class, "--server.port=0",
			"--spring.jmx.enabled=false",
			"--spring.cloud.stream.bindings.input.contentType=text/plain",
			"--spring.cloud.stream.bindings.output.contentType=text/plain");
	Processor processor = context.getBean(Processor.class);
	final String testMessage = "testing";
	processor.input().send(
			MessageBuilder.withPayload(testMessage).setHeader("foo", "bar").build());
	MessageCollector messageCollector = context.getBean(MessageCollector.class);
	Message<String> result = (Message<String>) messageCollector
			.forChannel(processor.output()).poll(1000, TimeUnit.MILLISECONDS);
	assertThat(result).isNotNull();
	assertThat(result.getPayload()).isEqualTo(testMessage.toUpperCase());
	assertThat(result.getHeaders().get("foo")).isNull();
	context.close();
}
 
Example #8
Source File: StreamListenerHandlerMethodTests.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
/**
 * @since 2.0 : This test is an example of the new behavior of 2.0 when it comes to
 * contentType handling. The default contentType being JSON in order to be able to
 * check a message without quotes the user needs to set the input/output contentType
 * accordingly Also, received messages are always of Message<byte[]> now.
 */
public void testMethodHeadersPropagatged() throws Exception {
	ConfigurableApplicationContext context = SpringApplication.run(
			TestMethodHeadersPropagated.class, "--server.port=0",
			"--spring.jmx.enabled=false",
			"--spring.cloud.stream.bindings.input.contentType=text/plain",
			"--spring.cloud.stream.bindings.output.contentType=text/plain");
	Processor processor = context.getBean(Processor.class);
	final String testMessage = "testing";
	processor.input().send(
			MessageBuilder.withPayload(testMessage).setHeader("foo", "bar").build());
	MessageCollector messageCollector = context.getBean(MessageCollector.class);
	Message<String> result = (Message<String>) messageCollector
			.forChannel(processor.output()).poll(1000, TimeUnit.MILLISECONDS);
	assertThat(result).isNotNull();
	assertThat(result.getPayload()).isEqualTo(testMessage.toUpperCase());
	assertThat(result.getHeaders().get("foo")).isEqualTo("bar");
	context.close();
}
 
Example #9
Source File: StreamListenerMessageArgumentTests.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testMessageArgument() throws Exception {
	ConfigurableApplicationContext context = SpringApplication.run(this.configClass,
			"--server.port=0",
			"--spring.cloud.stream.bindings.output.contentType=text/plain",
			"--spring.jmx.enabled=false");
	MessageCollector collector = context.getBean(MessageCollector.class);
	Processor processor = context.getBean(Processor.class);
	String id = UUID.randomUUID().toString();
	processor.input().send(MessageBuilder.withPayload("barbar" + id)
			.setHeader("contentType", "text/plain").build());
	TestPojoWithMessageArgument testPojoWithMessageArgument = context
			.getBean(TestPojoWithMessageArgument.class);
	assertThat(testPojoWithMessageArgument.receivedMessages).hasSize(1);
	assertThat(testPojoWithMessageArgument.receivedMessages.get(0).getPayload())
			.isEqualTo("barbar" + id);
	Message<String> message = (Message<String>) collector
			.forChannel(processor.output()).poll(1, TimeUnit.SECONDS);
	assertThat(message).isNotNull();
	assertThat(message.getPayload()).contains("barbar" + id);
	context.close();
}
 
Example #10
Source File: StreamListenerMethodWithReturnValueTests.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testReturn() throws Exception {
	ConfigurableApplicationContext context = SpringApplication.run(this.configClass,
			"--server.port=0", "--spring.jmx.enabled=false");
	MessageCollector collector = context.getBean(MessageCollector.class);
	Processor processor = context.getBean(Processor.class);
	String id = UUID.randomUUID().toString();
	processor.input()
			.send(MessageBuilder.withPayload("{\"foo\":\"barbar" + id + "\"}")
					.setHeader("contentType", "application/json").build());
	Message<String> message = (Message<String>) collector
			.forChannel(processor.output()).poll(1, TimeUnit.SECONDS);
	TestStringProcessor testStringProcessor = context
			.getBean(TestStringProcessor.class);
	Assertions.assertThat(testStringProcessor.receivedPojos).hasSize(1);
	Assertions.assertThat(testStringProcessor.receivedPojos.get(0))
			.hasFieldOrPropertyWithValue("foo", "barbar" + id);
	assertThat(message).isNotNull();
	assertThat(message.getPayload()).contains("barbar" + id);
	context.close();
}
 
Example #11
Source File: StreamListenerMethodWithReturnMessageTests.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testReturnMessage() throws Exception {
	ConfigurableApplicationContext context = SpringApplication.run(this.configClass,
			"--server.port=0", "--spring.jmx.enabled=false");
	MessageCollector collector = context.getBean(MessageCollector.class);
	Processor processor = context.getBean(Processor.class);
	String id = UUID.randomUUID().toString();
	processor.input()
			.send(MessageBuilder.withPayload("{\"foo\":\"barbar" + id + "\"}")
					.setHeader("contentType", "application/json").build());
	TestPojoWithMessageReturn testPojoWithMessageReturn = context
			.getBean(TestPojoWithMessageReturn.class);
	Assertions.assertThat(testPojoWithMessageReturn.receivedPojos).hasSize(1);
	Assertions.assertThat(testPojoWithMessageReturn.receivedPojos.get(0))
			.hasFieldOrPropertyWithValue("foo", "barbar" + id);
	Message<String> message = (Message<String>) collector
			.forChannel(processor.output()).poll(1, TimeUnit.SECONDS);
	assertThat(message).isNotNull();
	assertThat(message.getPayload()).contains("barbar" + id);
	context.close();
}
 
Example #12
Source File: TaskProcessor.java    From spring-cloud-task with Apache License 2.0 5 votes vote down vote up
@Transformer(inputChannel = Processor.INPUT, outputChannel = Processor.OUTPUT)
public Object setupRequest(String message) {
	Map<String, String> properties = new HashMap<>();
	if (StringUtils.hasText(this.processorProperties.getDataSourceUrl())) {
		properties
			.put("spring_datasource_url", this.processorProperties
				.getDataSourceUrl());
	}
	if (StringUtils
		.hasText(this.processorProperties.getDataSourceDriverClassName())) {
		properties.put("spring_datasource_driverClassName", this.processorProperties
			.getDataSourceDriverClassName());
	}
	if (StringUtils.hasText(this.processorProperties.getDataSourceUserName())) {
		properties.put("spring_datasource_username", this.processorProperties
			.getDataSourceUserName());
	}
	if (StringUtils.hasText(this.processorProperties.getDataSourcePassword())) {
		properties.put("spring_datasource_password", this.processorProperties
			.getDataSourcePassword());
	}
	properties.put("payload", message);

	TaskLaunchRequest request = new TaskLaunchRequest(
		this.processorProperties.getUri(), null, properties, null,
		this.processorProperties.getApplicationName());

	return new GenericMessage<>(request);
}
 
Example #13
Source File: StreamListenerWithAnnotatedInputOutputArgsTests.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private void sendMessageAndValidate(ConfigurableApplicationContext context)
		throws InterruptedException {
	Processor processor = context.getBean(Processor.class);
	processor.input().send(MessageBuilder.withPayload("hello")
			.setHeader("contentType", "text/plain").build());
	MessageCollector messageCollector = context.getBean(MessageCollector.class);
	Message<String> result = (Message<String>) messageCollector
			.forChannel(processor.output()).poll(1000, TimeUnit.MILLISECONDS);
	assertThat(result).isNotNull();
	assertThat(result.getPayload()).isEqualTo("HELLO");
	context.close();
}
 
Example #14
Source File: CommentService.java    From Learning-Spring-Boot-2.0-Second-Edition with MIT License 5 votes vote down vote up
@StreamListener
@Output(Processor.OUTPUT)
public Flux<Void> save(@Input(Processor.INPUT) Flux<Comment> newComment) {
	return repository
		.saveAll(newComment)
		.flatMap(comment -> {
			meterRegistry
				.counter("comments.consumed", "imageId", comment.getImageId())
				.increment();
			return Mono.empty();
		});
}
 
Example #15
Source File: KinesisBinderProcessorTests.java    From spring-cloud-stream-binder-aws-kinesis with Apache License 2.0 5 votes vote down vote up
@Bean
public MessageProducer kinesisMessageDriverChannelAdapter() {
	KinesisMessageDrivenChannelAdapter kinesisMessageDrivenChannelAdapter =
			new KinesisMessageDrivenChannelAdapter(amazonKinesis(), Processor.OUTPUT);
	kinesisMessageDrivenChannelAdapter.setOutputChannel(fromProcessorChannel());
	kinesisMessageDrivenChannelAdapter.setConverter(null);
	kinesisMessageDrivenChannelAdapter.setBindSourceRecord(true);

	DirectFieldAccessor dfa = new DirectFieldAccessor(kinesisMessageDrivenChannelAdapter);
	dfa.setPropertyValue("describeStreamBackoff", 10);
	dfa.setPropertyValue("consumerBackoff", 10);
	dfa.setPropertyValue("idleBetweenPolls", 1);

	return kinesisMessageDrivenChannelAdapter;
}
 
Example #16
Source File: SampleTransformer.java    From spring-cloud-stream-samples with Apache License 2.0 5 votes vote down vote up
@StreamListener(Processor.INPUT)
@SendTo(Processor.OUTPUT)
public Bar receive(Bar bar) {
	logger.info("******************\nAt the transformer\n******************");
	logger.info("Received value "+ bar.getValue() + " of type " + bar.getClass());
	logger.info("Transforming the value to " + TRANSFORMATION_VALUE + " and with the type " + bar.getClass());
	bar.setValue(TRANSFORMATION_VALUE);
	return bar;
}
 
Example #17
Source File: StreamListenerMethodWithReturnMessageTests.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@StreamListener(Processor.INPUT)
@SendTo(Processor.OUTPUT)
public Message<?> receive(StreamListenerTestUtils.FooPojo fooPojo) {
	this.receivedPojos.add(fooPojo);
	StreamListenerTestUtils.BarPojo barPojo = new StreamListenerTestUtils.BarPojo();
	barPojo.setBar(fooPojo.getFoo());
	return MessageBuilder.withPayload(barPojo).setHeader("foo", "bar").build();
}
 
Example #18
Source File: TasklaunchrequestTransformProcessorConfiguration.java    From spring-cloud-stream-app-starters with Apache License 2.0 5 votes vote down vote up
@Transformer(inputChannel = Processor.INPUT, outputChannel = Processor.OUTPUT)
public Object setupRequest(String message) {
	Map<String, String> properties = new HashMap<String, String>();
	Map<String, String> deploymentProperties = null;
	List<String> commandLineArgs = null;

	if (StringUtils.hasText(processorProperties.getDataSourceUrl())) {
		properties.put("spring_datasource_url", processorProperties.getDataSourceUrl());
	}
	if (StringUtils.hasText(processorProperties.getDataSourceDriverClassName())) {
		properties.put("spring_datasource_driverClassName", processorProperties.getDataSourceDriverClassName());
	}
	if (StringUtils.hasText(processorProperties.getDataSourceUserName())) {
		properties.put("spring_datasource_username", processorProperties.getDataSourceUserName());
	}
	if (StringUtils.hasText(processorProperties.getDataSourcePassword())) {
		properties.put("spring_datasource_password", processorProperties.getDataSourcePassword());
	}
	if (StringUtils.hasLength(processorProperties.getDeploymentProperties())) {
		deploymentProperties = parse(processorProperties.getDeploymentProperties());
	}
	if (StringUtils.hasLength(processorProperties.getCommandLineArguments())) {
		commandLineArgs = parseParams(processorProperties.getCommandLineArguments());
	}

	TaskLaunchRequest request = new TaskLaunchRequest(
			processorProperties.getUri(), commandLineArgs, properties,
			deploymentProperties);

	return request;
}
 
Example #19
Source File: CommentService.java    From Learning-Spring-Boot-2.0-Second-Edition with MIT License 5 votes vote down vote up
@StreamListener
@Output(Processor.OUTPUT)
public Flux<Void> save(@Input(Processor.INPUT) Flux<Comment> newComment) {
	return repository
		.saveAll(newComment)
		.flatMap(comment -> {
			meterRegistry
				.counter("comments.consumed", "imageId", comment.getImageId())
				.increment();
			return Mono.empty();
		});
}
 
Example #20
Source File: StreamListenerHandlerBeanTests.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@StreamListener(Processor.INPUT)
@SendTo(Processor.OUTPUT)
public StreamListenerTestUtils.BarPojo receive(
		StreamListenerTestUtils.FooPojo fooMessage) {
	this.receivedPojos.add(fooMessage);
	StreamListenerTestUtils.BarPojo barPojo = new StreamListenerTestUtils.BarPojo();
	barPojo.setBar(fooMessage.getFoo());
	return barPojo;
}
 
Example #21
Source File: StreamListenerHandlerMethodTests.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@Test
public void testMethodWithMultipleInputParameters() throws Exception {
	ConfigurableApplicationContext context = SpringApplication.run(
			TestMethodWithMultipleInputParameters.class, "--server.port=0",
			"--spring.jmx.enabled=false");
	Processor processor = context.getBean(Processor.class);
	StreamListenerTestUtils.FooInboundChannel1 inboundChannel2 = context
			.getBean(StreamListenerTestUtils.FooInboundChannel1.class);
	final CountDownLatch latch = new CountDownLatch(2);
	((SubscribableChannel) processor.output()).subscribe(new MessageHandler() {
		@Override
		public void handleMessage(Message<?> message) throws MessagingException {
			Assert.isTrue(
					message.getPayload().equals("footesting")
							|| message.getPayload().equals("BARTESTING"),
					"Assert failed");
			latch.countDown();
		}
	});
	processor.input().send(MessageBuilder.withPayload("{\"foo\":\"fooTESTing\"}")
			.setHeader("contentType", "application/json").build());
	inboundChannel2.input()
			.send(MessageBuilder.withPayload("{\"bar\":\"bartestING\"}")
					.setHeader("contentType", "application/json").build());
	assertThat(latch.await(1, TimeUnit.SECONDS));
	context.close();
}
 
Example #22
Source File: ContentTypeTckTests.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@StreamListener(Processor.INPUT)
@SendTo(Processor.OUTPUT)
public Person echo(Object value) throws Exception {
	ObjectMapper mapper = new ObjectMapper();
	// assume it is string because CT is text/plain
	return mapper.readValue((String) value, Person.class);
}
 
Example #23
Source File: ContentTypeTckTests.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@StreamListener(Processor.INPUT)
@SendTo(Processor.OUTPUT)
public Person echo(Message<?> message) throws Exception {
	ObjectMapper mapper = new ObjectMapper();
	// assume it is string because CT is text/plain
	return mapper.readValue((String) message.getPayload(), Person.class);
}
 
Example #24
Source File: ContentTypeTckTests.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@StreamListener(Processor.INPUT)
@SendTo(Processor.OUTPUT)
public Message<String> echo(Message<Person> value) {
	return MessageBuilder.withPayload(value.getPayload().toString())
			.setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN)
			.build();
}
 
Example #25
Source File: ContentTypeTckTests.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@ServiceActivator(inputChannel = Processor.INPUT, outputChannel = Processor.OUTPUT)
public Message<String> echo(Message<String> value) throws Exception {
	ObjectMapper mapper = new ObjectMapper();
	Person person = mapper.readValue(value.getPayload(), Person.class);
	return MessageBuilder.withPayload(person.toString())
			.setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN)
			.build();
}
 
Example #26
Source File: StreamListenerAnnotatedMethodArgumentsTests.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@StreamListener(Processor.INPUT)
public void receive(@Payload StreamListenerTestUtils.FooPojo fooPojo,
		@Headers Map<String, Object> headers,
		@Header(MessageHeaders.CONTENT_TYPE) String contentType) {
	this.receivedArguments.add(fooPojo);
	this.receivedArguments.add(headers);
	this.receivedArguments.add(contentType);
}
 
Example #27
Source File: StreamListenerAnnotatedMethodArgumentsTests.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@StreamListener
public void receive(
		@Input(Processor.INPUT) @Payload StreamListenerTestUtils.FooPojo fooPojo,
		@Headers Map<String, Object> headers,
		@Header(MessageHeaders.CONTENT_TYPE) String contentType) {
	this.receivedArguments.add(fooPojo);
	this.receivedArguments.add(headers);
	this.receivedArguments.add(contentType);
}
 
Example #28
Source File: StreamListenerMessageArgumentTests.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@StreamListener(Processor.INPUT)
@SendTo(Processor.OUTPUT)
public StreamListenerTestUtils.BarPojo receive(Message<String> fooMessage) {
	this.receivedMessages.add(fooMessage);
	StreamListenerTestUtils.BarPojo barPojo = new StreamListenerTestUtils.BarPojo();
	barPojo.setBar(fooMessage.getPayload());
	return barPojo;
}
 
Example #29
Source File: StreamListenerMethodReturnWithConversionTests.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testReturnConversion() throws Exception {
	ConfigurableApplicationContext context = SpringApplication.run(
			this.configClass,
			"--spring.cloud.stream.bindings.output.contentType=application/json",
			"--server.port=0", "--spring.jmx.enabled=false");
	MessageCollector collector = context.getBean(MessageCollector.class);
	Processor processor = context.getBean(Processor.class);
	String id = UUID.randomUUID().toString();
	processor.input()
			.send(MessageBuilder.withPayload("{\"foo\":\"barbar" + id + "\"}")
					.setHeader("contentType", "application/json").build());
	TestPojoWithMimeType testPojoWithMimeType = context
			.getBean(TestPojoWithMimeType.class);
	Assertions.assertThat(testPojoWithMimeType.receivedPojos).hasSize(1);
	Assertions.assertThat(testPojoWithMimeType.receivedPojos.get(0))
			.hasFieldOrPropertyWithValue("foo", "barbar" + id);
	Message<String> message = (Message<String>) collector
			.forChannel(processor.output()).poll(1, TimeUnit.SECONDS);
	assertThat(message).isNotNull();
	assertThat(new String(message.getPayload()))
			.isEqualTo("{\"bar\":\"barbar" + id + "\"}");
	assertThat(
			message.getHeaders().get(MessageHeaders.CONTENT_TYPE, MimeType.class)
					.includes(MimeTypeUtils.APPLICATION_JSON));
	context.close();
}
 
Example #30
Source File: StreamListenerWithAnnotatedInputOutputArgsTests.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@StreamListener
public void receive(@Input("invalid") SubscribableChannel input,
		@Output(Processor.OUTPUT) final MessageChannel output) {
	input.subscribe(new MessageHandler() {
		@Override
		public void handleMessage(Message<?> message) throws MessagingException {
			output.send(MessageBuilder
					.withPayload(message.getPayload().toString().toUpperCase())
					.build());
		}
	});
}