spring-integration Zelfstudie
Aan de slag met lente-integratie
Zoeken…
Opmerkingen
Deze sectie geeft een overzicht van wat lente-integratie is en waarom een ontwikkelaar het misschien wil gebruiken.
Het moet ook alle grote onderwerpen binnen de lente-integratie vermelden en een link naar de gerelateerde onderwerpen. Aangezien de Documentatie voor lente-integratie nieuw is, moet u mogelijk eerste versies van die gerelateerde onderwerpen maken.
versies
| Versie | Publicatiedatum |
|---|---|
| 4.3.x | 2016/11/07 |
| 4.2.x | 2016/11/07 |
| 4.1.x | 2016/07/25 |
| 4.0.x | 2016/07/26 |
| 3.0.x | 2015/10/27 |
| 2.2.x | 2016/01/27 |
| 2.1.x | 2013/06/10 |
| 2.0.x | 2013/04/11 |
| 1.0.x | 2010-04-16 |
Installatie of instellingen
De beste manier om aan de slag te gaan met Spring-integratie in uw project is met een afhankelijkheidsbeheersysteem, zoals gradle.
dependencies {
compile 'org.springframework.integration:spring-integration-core:4.3.5.RELEASE'
}
Hieronder is een heel eenvoudig voorbeeld met behulp van de gateway , service-activator bericht eindpunten.
//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!"));
}
}
Het toont de tekenreeks Hi, Spring Integration! in de console.
Natuurlijk biedt Spring Integration ook een configuratie in xml-stijl. Voor het bovenstaande voorbeeld kunt u het volgende XML-configuratiebestand schrijven.
<?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>
Als u de toepassing wilt uitvoeren met het xml-configuratiebestand, moet u de code new AnnotationConfigApplicationContext(Application.class) in Application class wijzigen in new ClassPathXmlApplicationContext("classpath:getstarted.xml") . En voer deze applicatie opnieuw uit, u ziet dezelfde uitvoer.
Generieke inkomende en uitgaande kanaaladapter
Kanaaladapter is een van de eindpunten van berichten in Spring Integration. Het wordt gebruikt voor unidirectionele berichtenstroom. Er zijn twee soorten kanaaladapters:
Inkomende adapter : invoerzijde van het kanaal. Luister of lees actief bericht.
Uitgaande adapter : uitgangszijde van het kanaal. Stuur bericht naar Java-klasse of extern systeem of protocol.
Broncode.
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"); } }XML-stijl configuratiebestand:
<?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>Berichtenstroom
-
inAdapter: een inkomende kanaaladapter. RoepApplication$MessageProducer.producemethode elke 1 seconde op (<int:poller fixed-rate="1000"/>) en stuur de geretourneerde string als bericht naar hetchannel. -
channel: kanaal om bericht over te dragen. -
outAdapter: een uitgaande kanaaladapter. Eenmaal bericht op kanaal bereiktchannel, zal deze adapter het bericht en stuur het dan naarApplication$MessageConsumer.consumemethode die het bericht af te drukken op de console. - Je ziet dus dat deze willekeurige tekenreeks elke seconde op de console wordt weergegeven.
-
Simple Echo Excample met Spring-Integration-Stream
Java-code:
public class StdioApplication {
public static void main(String[] args) {
new ClassPathXmlApplicationContext("classpath:spring/integration/stackoverflow/stdio/stdio.xml");
}
}
XML-configuratiebestand
<?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>
Dit is een echo-voorbeeld. Wanneer u deze Java-toepassing uitvoert, kunt u een string invoeren en deze wordt vervolgens op de console weergegeven.

