Summary of Spring Cloud Stream

Posted by selenin on Sun, 11 Aug 2019 16:10:38 +0200

concept

1,group:

There is only one instance consumption in the group. If no group is set, stream automatically creates anonymous and independent groups for each instance -- and each instance consumes them.

There is only one instance consumed per group and load balancing is polled. Usually, when binding an application to a given target, it's best to always specify consumer group s

2,destination binder:

Components that communicate with external messaging systems provide two ways to construct Binding: bindConsumer and bindProducer, which are used to construct producers and consumers respectively. Binder enables Spring Cloud Stream applications to connect flexibly to middleware. spring currently provides binder for kafka and rabbitmq.

3,destination binding:

Binding is a bridge between application and message middleware for message consumption and production, created by binder

4,partition

One or more producers send data to multiple consumers and ensure that data with common identity is processed by the same consumer. The default is to hashCode the message and then take the remainder according to the number of partitions, so for the same message, it always falls on the same consumer.

Note: Strictly speaking, partition is not a concept, but a way for Stream to improve scalability and throughput.

annotation

1. @Input, using an example:

public interface MySink {
    @Input("my-input")
    SubscribableChannel input();
}

Effect:

  • Used to receive messages
  • Generate channel instances for each binding
  • Specify the name of input channel
  • Generate a bean named my-input in the spring container of type Subscribable Channel
  • Generate a class in spring container to implement MySink interface.

2. @Output, using an example:

public interface MySource {
    @Output("my-output")
    MessageChannel output();
}

Effect:

  • Similar to @Input, it's just used to produce messages

3. @StreamListener, using an example:

@StreamListener(value = Sink.INPUT, condition = "headers['type']=='dog'")
public void receive(String messageBody) {
    log.info("Received: {}", messageBody);
}

Effect:

  • Used for consumer messages
  • Function of condition: Used to filter messages, only messages that conform to conditional expressions are processed
  • The two conditions under which condition works are:
    • The annotated method has no return value
    • The method is a stand-alone approach and does not support Reactive API

4. @SendTo, using an example:

// Receive the INPUT channel message and send the return value to the OUTPUT channel
@StreamListener(Sink.INPUT)
@SendTo(Source.OUTPUT)
public String receive(String receiveMsg) {
   return "handle...";
}

Effect:

  • Used to send messages

4. @InboundChannel Adapter, using an example:

@Bean
@InboundChannelAdapter(value = Source.OUTPUT,
        poller = @Poller(fixedDelay = "10", maxMessagesPerPoll = "1"))
public MessageSource<String> producer() {
    return () -> new GenericMessage<>("Hello Spring Cloud Stream");
}

Effect:

  • Let the annotated method produce the message
  • Fixed Delay: How many milliseconds to send once
  • MaxMessages PerPoll: How many messages are sent at a time

5. @Service Activator, using an example:

@ServiceActivator(inputChannel = Sink.INPUT, outputChannel = Source.OUTPUT)
public String transform(String payload) {
    return payload.toUpperCase();
}

Effect:

  • The annotation method can process the message or the valid content of the message. By listening for the input message, the annotation can be processed by the method body code and output to the output.

6. @Transformer, using an example:

@Transformer(inputChannel = Processor.INPUT, outputChannel = Processor.OUTPUT)
public Object transform(String message) {
  return message.toUpperCase();
}

Effect:

  • Similar to @Service Activator, annotating this annotation can transform messages, message headers, or message valid content.

PollableMessageSource

Pollable Message Source allows consumers to control consumption rates. For example, to illustrate briefly, we first define an interface:

public interface PolledProcessor {
    @Input("pollable-input")
    PollableMessageSource input();
}

Use examples:

@Autowired
private PolledProcessor polledProcessor;

@Scheduled(fixedDelay = 5_000)
public void poll() {
    polledProcessor.input().poll(message -> {
        byte[] bytes = (byte[]) message.getPayload();
        String payload = new String(bytes);
        System.out.println(payload);
    });
}

Reference resources:

https://spring.io/blog/2018/02/27/spring-cloud-stream-2-0-polled-consumers

Topics: Java Spring kafka RabbitMQ