Финальная версия примеров на занятие 26 (SI: Endpoints и Flow Components) для группы 2019-08

This commit is contained in:
kataus
2019-12-04 18:28:48 +03:00
parent 22e5a2b6ab
commit 91021e6c1e
2 changed files with 36 additions and 14 deletions
@@ -7,12 +7,14 @@ import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.integration.annotation.IntegrationComponentScan;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.channel.PublishSubscribeChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.config.EnableIntegration;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.dsl.IntegrationFlows;
import org.springframework.integration.dsl.Pollers;
import org.springframework.integration.dsl.channel.MessageChannels;
import org.springframework.integration.scheduling.PollerMetadata;
import ru.otus.spring.integration.domain.Food;
import ru.otus.spring.integration.domain.OrderItem;
@@ -29,25 +31,29 @@ import java.util.stream.Collectors;
public class App {
private static final String[] MENU = { "coffee", "tea", "smoothie", "whiskey", "beer", "cola", "water" };
@Bean
public DirectChannel itemsChannel() {
return MessageChannels.direct().datatype( OrderItem.class ).get();
public QueueChannel itemsChannel() {
return MessageChannels.queue( 10 ).get();
}
@Bean
public PublishSubscribeChannel foodChannel() {
return MessageChannels.publishSubscribe().datatype( Food.class ).get();
return MessageChannels.publishSubscribe().get();
}
// TODO: create default poller
@Bean(name = PollerMetadata.DEFAULT_POLLER)
public PollerMetadata poller() {
return Pollers.fixedRate( 100 ).maxMessagesPerPoll( 2 ).get();
}
@Bean
public IntegrationFlow cafeFlow() {
return IntegrationFlows.from( "itemsChannel" )
// TODO: cook OrderItem in the kitchen
// TODO*: add router and subflows to process iced and usual items
// TODO*: add splitter and aggregator
// TODO: forward it to the publish subscriber channel
.split()
.handle( "kitchenService", "cook" )
.aggregate()
.channel( "foodChannel" )
.get();
}
@@ -60,14 +66,25 @@ public class App {
while ( true ) {
Thread.sleep( 1000 );
OrderItem items = generateOrderItem();
Collection<OrderItem> items = generateOrderItems();
System.out.println( "New orderItems: " +
items.getItemName() );
Food food = cafe.process( items );
System.out.println( "Ready food: " + food.getName() );
items.stream().map( OrderItem::getItemName )
.collect( Collectors.joining( "," ) ) );
Collection<Food> food = cafe.process( items );
System.out.println( "Ready food: " + food.stream()
.map( Food::getName )
.collect( Collectors.joining( "," ) ) );
}
}
private static Collection<OrderItem> generateOrderItems() {
List<OrderItem> items = new ArrayList<>();
for ( int i = 0; i < RandomUtils.nextInt( 1, 5 ); ++ i ) {
items.add( generateOrderItem() );
}
return items;
}
private static OrderItem generateOrderItem() {
return new OrderItem( MENU[ RandomUtils.nextInt( 0, MENU.length ) ] );
}
@@ -1,13 +1,18 @@
package ru.otus.spring.integration;
import org.springframework.integration.annotation.Gateway;
import org.springframework.integration.annotation.MessagingGateway;
import ru.otus.spring.integration.domain.Food;
import ru.otus.spring.integration.domain.OrderItem;
import java.util.Collection;
// TODO: add messaging gateway annotation
@MessagingGateway
public interface Cafe {
// TODO: add gateway annotation with required channels
Food process( OrderItem orderItem);
@Gateway(requestChannel = "itemsChannel", replyChannel = "foodChannel")
Collection<Food> process( Collection<OrderItem> orderItem);
}