Use @EnableModule(Channels.class) instead of field declarations

Standard channel interface types of Sink, Source, Processor
are provided, but user can create others with @Input and
@Output MessageChannel methods.

To get the declared channels in an app you can use the
@ModuleChannels qualifier to inject the interface, e.g.
@ModuleChannels(Cafe.class) injects the channels defined
in the Cafe.
This commit is contained in:
Dave Syer
2015-07-16 15:10:41 +01:00
committed by Mark Fisher
parent 460bb140f2
commit c09b292d79
22 changed files with 390 additions and 248 deletions

View File

@@ -18,25 +18,20 @@ package config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.stream.annotation.EnableModule;
import org.springframework.cloud.stream.annotation.Input;
import org.springframework.cloud.stream.annotation.Sink;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.messaging.MessageChannel;
/**
* @author Dave Syer
* @author Marius Bogoevici
*/
@EnableModule
@EnableModule(Sink.class)
public class SinkModuleDefinition {
private static Logger logger = LoggerFactory.getLogger(SinkModuleDefinition.class);
@Input
public MessageChannel input;
@ServiceActivator(inputChannel="input")
@ServiceActivator(inputChannel=Sink.INPUT)
public void loggerSink(Object payload) {
logger.info("Received: " + payload);
}

View File

@@ -21,29 +21,25 @@ import java.util.Date;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.stream.annotation.EnableModule;
import org.springframework.cloud.stream.annotation.Output;
import org.springframework.cloud.stream.annotation.Source;
import org.springframework.context.annotation.Bean;
import org.springframework.integration.annotation.InboundChannelAdapter;
import org.springframework.integration.annotation.Poller;
import org.springframework.integration.core.MessageSource;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.GenericMessage;
/**
* @author Dave Syer
* @author Marius Bogoevici
*/
@EnableModule
@EnableModule(Source.class)
public class SourceModuleDefinition {
@Value("${format:YYYY/MM/dd hh:mm:ss}")
private String format;
@Output
public MessageChannel output;
@Bean
@InboundChannelAdapter(value = "output", autoStartup = "false", poller = @Poller(fixedDelay = "${fixedDelay}", maxMessagesPerPoll = "1"))
@InboundChannelAdapter(value = Source.OUTPUT, autoStartup = "false", poller = @Poller(fixedDelay = "${fixedDelay}", maxMessagesPerPoll = "1"))
public MessageSource<String> timerMessageSource() {
return () -> new GenericMessage<>(new SimpleDateFormat(this.format).format(new Date()));
}

View File

@@ -18,25 +18,20 @@ package sink;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.stream.annotation.EnableModule;
import org.springframework.cloud.stream.annotation.Input;
import org.springframework.cloud.stream.annotation.Sink;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.messaging.MessageChannel;
/**
* @author Dave Syer
*
*/
@EnableModule
@EnableModule(Sink.class)
public class LogSink {
private static Logger logger = LoggerFactory.getLogger(LogSink.class);
@Input
public MessageChannel input;
@ServiceActivator(inputChannel="input")
@ServiceActivator(inputChannel=Sink.INPUT)
public void loggerSink(Object payload) {
logger.info("Received: " + payload);
}

View File

@@ -1,11 +1,21 @@
package demo;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.cloud.stream.annotation.ModuleChannels;
import org.springframework.cloud.stream.annotation.Output;
import org.springframework.cloud.stream.annotation.Sink;
import org.springframework.cloud.stream.annotation.Source;
import org.springframework.messaging.MessageChannel;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import sink.LogSink;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SinkApplication.class)
@@ -13,8 +23,19 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@DirtiesContext
public class ModuleApplicationTests {
@Autowired
@ModuleChannels(LogSink.class)
private Sink sink;
@Autowired
private Sink same;
@Output(Source.OUTPUT)
private MessageChannel output;
@Test
public void contextLoads() {
assertNotNull(this.sink.input());
}
}

View File

@@ -19,12 +19,11 @@ package source;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.stream.annotation.EnableModule;
import org.springframework.cloud.stream.annotation.Output;
import org.springframework.cloud.stream.annotation.Source;
import org.springframework.context.annotation.Bean;
import org.springframework.integration.annotation.InboundChannelAdapter;
import org.springframework.integration.annotation.Poller;
import org.springframework.integration.core.MessageSource;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.GenericMessage;
import java.text.SimpleDateFormat;
@@ -35,18 +34,15 @@ import java.util.Date;
* @author Glenn Renfro
*
*/
@EnableModule
@EnableModule(Source.class)
@EnableConfigurationProperties(TimeSourceOptionsMetadata.class)
public class TimeSource {
@Autowired
private TimeSourceOptionsMetadata options;
@Output
public MessageChannel output;
@Bean
@InboundChannelAdapter(value = "output", autoStartup = "false", poller = @Poller(fixedDelay = "${fixedDelay}", maxMessagesPerPoll = "1"))
@InboundChannelAdapter(value = Source.OUTPUT, autoStartup = "false", poller = @Poller(fixedDelay = "${fixedDelay}", maxMessagesPerPoll = "1"))
public MessageSource<String> timerMessageSource() {
return () -> new GenericMessage<>(new SimpleDateFormat(this.options.getFormat()).format(new Date()));
}

View File

@@ -18,25 +18,20 @@ package config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.stream.annotation.EnableModule;
import org.springframework.cloud.stream.annotation.Input;
import org.springframework.cloud.stream.annotation.Sink;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.messaging.MessageChannel;
/**
* @author Dave Syer
* @author Marius Bogoevici
*/
@EnableModule
@EnableModule(Sink.class)
public class TappingLoggingSink {
private static Logger logger = LoggerFactory.getLogger(TappingLoggingSink.class);
@Input
public MessageChannel input;
@ServiceActivator(inputChannel="input")
@ServiceActivator(inputChannel = Sink.INPUT)
public void loggerSink(Object payload) {
logger.info("Received: " + payload);
}

View File

@@ -2,13 +2,11 @@ package demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.annotation.EnableModule;
import org.springframework.context.annotation.ComponentScan;
import config.TappingLoggingSink;
@SpringBootApplication
@EnableModule
@ComponentScan(basePackageClasses= TappingLoggingSink.class)
public class TapApplication {

View File

@@ -18,20 +18,16 @@ package transform;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.stream.annotation.EnableModule;
import org.springframework.context.annotation.Bean;
import org.springframework.cloud.stream.annotation.Processor;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.SubscribableChannel;
/**
* @author Dave Syer
*
*/
@EnableModule
@EnableModule(Processor.class)
@ConfigurationProperties("module.logging")
public class LoggingTransformer {
@@ -50,17 +46,7 @@ public class LoggingTransformer {
this.name = name;
}
@Bean
public MessageChannel input() {
return new DirectChannel();
}
@Bean
public SubscribableChannel output() {
return new DirectChannel();
}
@ServiceActivator(inputChannel = "input", outputChannel = "output")
@ServiceActivator(inputChannel = Processor.INPUT, outputChannel = Processor.OUTPUT)
public Object transform(Object payload) {
logger.info("Transformed by " + this.name + ": " + payload);
return payload;