Premiers pas avec l'intégration de printemps

Installation ou configuration

La meilleure façon de commencer à utiliser Spring-Integration dans votre projet est d’utiliser un système de gestion des dépendances, comme gradle.

dependencies {
    compile 'org.springframework.integration:spring-integration-core:4.3.5.RELEASE'
}

Vous trouverez ci-dessous un exemple très simple utilisant la passerelle, service-activator points de terminaison de message.

//these annotations will enable Spring integration and scan for components
@Configuration
@EnableIntegration
@IntegrationComponentScan
public class Application {
    //a channel has two ends, this Messaging Gateway is acting as input from one side of inChannel
    @MessagingGateway
    interface Greeting {
        @Gateway(requestChannel = "inChannel")
        String greet(String name);
    }

    @Component
    static class HelloMessageProvider {
        //a service activator act as a handler when message is received from inChannel, in this example, it is acting as the handler on the output side of inChannel
        @ServiceActivator(inputChannel = "inChannel")
        public String sayHello(String name) {
            return "Hi, " + name;
        }
    }

    @Bean
    MessageChannel inChannel() {
        return new DirectChannel();
    }

    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(Application.class);
        Greeting greeting = context.getBean(Greeting.class);
        //greeting.greet() send a message to the channel, which trigger service activitor to process the incoming message
        System.out.println(greeting.greet("Spring Integration!"));
    }
}

Il affichera la chaîne “Hi, Spring Integration!” dans la console.

Bien sûr, Spring Integration fournit également une configuration de style xml. Pour l’exemple ci-dessus, vous pouvez écrire le fichier de configuration xml suivant.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:int="http://www.springframework.org/schema/integration"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/integration
        http://www.springframework.org/schema/integration/spring-integration.xsd">
    <int:gateway default-request-channel="inChannel"
                 service-interface="spring.integration.stackoverflow.getstarted.Application$Greeting"/>
    <int:channel id="inChannel"/>
    <int:service-activator input-channel="inChannel" method="sayHello">
        <bean class="spring.integration.stackoverflow.getstarted.Application$HelloMessageProvider"/>
    </int:service-activator>
</beans>

Pour exécuter l’application à l’aide du fichier de configuration xml, vous devez remplacer le code new AnnotationConfigApplicationContext(Application.class) dans la classe Application par new ClassPathXmlApplicationContext("classpath:getstarted.xml"). Et exécutez à nouveau cette application, vous pouvez voir la même sortie.

Adaptateur de canal entrant et sortant générique

L’adaptateur de canal est l’un des points de terminaison de message dans Spring Integration. Il est utilisé pour le flux de messages unidirectionnel. Il existe deux types d’adaptateur de canal :

Adaptateur entrant : côté entrée du canal. Écoutez ou lisez activement le message.

Adaptateur sortant : côté sortie du canal. Envoie un message à la classe Java ou au système ou protocole externe.

[![entrez la description de l’image ici][1]][1]

[1] : https://i.stack.imgur.com/aTes7.png

  • Code source.

    public class Application {
        static class MessageProducer {
            public String produce() {
                String[] array = {"first line!", "second line!", "third line!"};
                return array[new Random().nextInt(3)];
            }
        }
    
        static class MessageConsumer {
            public void consume(String message) {
                System.out.println(message);
            }
        }
    
        public static void main(String[] args) {
            new ClassPathXmlApplicationContext("classpath:spring/integration/stackoverflow/ioadapter/ioadapter.xml");
        }
    }
    
  • Fichier de configuration de style XML :

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:int="http://www.springframework.org/schema/integration"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd">
        <int:channel id="channel"/>
        <int:inbound-channel-adapter id="inAdapter" channel="channel" method="produce">
            <bean class="spring.integration.stackoverflow.ioadapter.Application$MessageProducer"/>
            <int:poller fixed-rate="1000"/>
        </int:inbound-channel-adapter>
        <int:outbound-channel-adapter id="outAdapter" channel="channel" method="consume">
            <bean class="spring.integration.stackoverflow.ioadapter.Application$MessageConsumer"/>
        </int:outbound-channel-adapter>
    </beans>
    
  • Flux de messages

    • inAdapter: an inbound channel adapter. Invoke Application$MessageProducer.produce method every 1 second (<int:poller fixed-rate="1000"/>) and send the returned string as message to the channel channel.
    • channel: channel to transfer message.
    • outAdapter: an outbound channel adapter. Once message reached on channel channel, this adapter will receive the message and then send it to Application$MessageConsumer.consume method which print the message on the console.
    • So you can observe that these random choose string will displayed on the console every 1 second.

Exemple d’écho simple avec Spring-Integration-Stream

[![entrez la description de l’image ici][1]][1]

Code Java :

public class StdioApplication {
    public static void main(String[] args) {
        new ClassPathXmlApplicationContext("classpath:spring/integration/stackoverflow/stdio/stdio.xml");
    }
}

Fichier de configuration XML

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:int="http://www.springframework.org/schema/integration"
       xmlns:int-stream="http://www.springframework.org/schema/integration/stream"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/integration/stream
       http://www.springframework.org/schema/integration/stream/spring-integration-stream.xsd
       http://www.springframework.org/schema/integration
       http://www.springframework.org/schema/integration/spring-integration.xsd">
    <int:channel id="channel"/>
    <int-stream:stdin-channel-adapter id="stdin" channel="channel">
        <int:poller fixed-rate="1000"/>
    </int-stream:stdin-channel-adapter>
    <int-stream:stdout-channel-adapter id="stdout" channel="channel"/>
</beans>

Ceci est un exemple d’écho. Lorsque vous exécutez cette application Java, vous pouvez saisir une chaîne, puis elle sera affichée sur la console.

[1] : https://i.stack.imgur.com/wkNYA.png