kagamihogeの日記

kagamihogeの日記です。

spring-integrationでメッセージ送信

spring-integration勉強中です。

非常にシンプルな、spring-integrationのチャネルに文字列メッセージ送信して表示するだけ、を作ってみる。

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>kagamihoge</groupId>
    <artifactId>integratesample0</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>integratesample0</name>
    <url>http://maven.apache.org</url>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath />
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>11</java.version>
        <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-integration</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

IntegrationFlow をつかう

package kagamihoge.integratesample0;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.integration.annotation.Gateway;
import org.springframework.integration.annotation.MessagingGateway;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.dsl.IntegrationFlows;

@SpringBootApplication
public class Application implements CommandLineRunner {
    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(WebApplicationType.NONE).run(args);
    }

    @Override
    public void run(String... args) throws Exception {
        gateway.go("simple message.");
    }

    @Autowired
    SimpleMessageGateway gateway;

    @MessagingGateway
    public interface SimpleMessageGateway {
        @Gateway(requestChannel = "simple.channel")
        void go(String payload);
    }

    @Bean
    public IntegrationFlow flow() {
        return IntegrationFlows //
                .from("simple.channel") //
                .handle(m -> System.out.println("handle:" + m.getPayload())) //
                .get();
    }
}

アノテーションでなんかする

詳しいことはまだ全然分かってないけど。単純なメッセージ送信・受信だけなら、以下のようにアノテーションをつけたbean用意すれば出来る。

package kagamihoge.integratesample1simple;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.integration.annotation.Gateway;
import org.springframework.integration.annotation.MessagingGateway;
import org.springframework.integration.annotation.ServiceActivator;

@SpringBootApplication
public class Application implements CommandLineRunner {
    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(WebApplicationType.NONE).run(args);
    }

    @Override
    public void run(String... args) throws Exception {
        gateway.go("sample message send.");
    }

    @Autowired
    SampleGateway gateway;

    @MessagingGateway
    interface SampleGateway {
        @Gateway(requestChannel = "sample.channel")
        void go(String payload);
    }

    static class SampleActivator {
        @ServiceActivator(inputChannel = "sample.channel")
        public void handle(String message) {
            System.out.println("handle message: " + message);
        }
    };

    @Bean
    SampleActivator receiver() {
        return new SampleActivator();
    }
}