home

Spring Integration

Integration Fundamentals

Integration Strategies

Messaging Patterns

Building Blocks

Example

# Count occurence of the word "Integration" in the file JustSpringNotes.md
kt$ cat JustSpringNotes.md | grep "Integration" | wc -l

Here cat, grep and wc are endpoints and the pipe (|) is the channel. JustSpringNotes.md is the Message. Filename itself can be considered as the MessageHeader and the contents of the file is Payload.

Messages

Message Payload and Message Header

Example: Implementing the Message Interface

import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

public class MyFirstMessage implements Message<MyFirstMessage> {

    private final String data;

    public MyFirstMessage(String data) {
        this.data = data;
    }

    @Override
    public MyFirstMessage getPayload() {
        return new MyFirstMessage("Hello World!");
    }

    @Override
    public MessageHeaders getHeaders() {
        final HashMap<String, Object> msgHeadersMap = new HashMap<>();
        final Calendar calendarInstance = Calendar.getInstance();
        calendarInstance.add(Calendar.YEAR, 10);
        msgHeadersMap.put("validUntil", Date.from(calendarInstance.toInstant()));
        return new MessageHeaders(msgHeadersMap);
    }

    public String getData() {
        return data;
    }
}

Example: Building a Message using the MessageBuilder

import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

public class MyFirstMessage {

    private final String data;

    public MyFirstMessage(String data) {
        this.data = data;
    }

    public static Message<MyFirstMessage> getMyFirstMessage() {
        return MessageBuilder
                .withPayload(new MyFirstMessage("HelloWorld"))
                .setHeader("validUntil", validUntil())
                .build();
    }

    public static Date validUntil() {
        final HashMap<String, Object> msgHeadersMap = new HashMap<>();
        final Calendar calendarInstance = Calendar.getInstance();
        calendarInstance.add(Calendar.YEAR, 10);
        return Date.from(calendarInstance.toInstant());
    }

    public String getData() {
        return data;
    }
}

Message Channels

Various Channel Types

Endpoints

Out-of-the-Box Endpoints