Примеры к занятию 28 + ForkJoinPool

This commit is contained in:
kataus
2020-12-09 21:12:19 +03:00
parent dbe60af08e
commit a3f2cdc0b5
@@ -7,7 +7,6 @@ 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;
@@ -15,7 +14,6 @@ import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.dsl.IntegrationFlows;
import org.springframework.integration.dsl.MessageChannels;
import org.springframework.integration.dsl.Pollers;
import org.springframework.integration.scheduling.PollerMetadata;
import ru.otus.spring.integration.domain.Food;
import ru.otus.spring.integration.domain.OrderItem;
@@ -23,20 +21,21 @@ import ru.otus.spring.integration.domain.OrderItem;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ForkJoinPool;
import java.util.stream.Collectors;
@IntegrationComponentScan
@SuppressWarnings({"resource", "Duplicates", "InfiniteLoopStatement"})
@SuppressWarnings({ "resource", "Duplicates", "InfiniteLoopStatement" })
@ComponentScan
@Configuration
@EnableIntegration
public class App {
private static final String[] MENU = {"coffee", "tea", "smoothie", "whiskey", "beer", "cola", "water"};
private static final String[] MENU = { "coffee", "tea", "smoothie", "whiskey", "beer", "cola", "water" };
@Bean
public QueueChannel itemsChannel() {
return MessageChannels.queue(10).get();
return MessageChannels.queue( 10 ).get();
}
@Bean
@@ -44,49 +43,53 @@ public class App {
return MessageChannels.publishSubscribe().get();
}
@Bean (name = PollerMetadata.DEFAULT_POLLER )
public PollerMetadata poller () {
return Pollers.fixedRate(100).maxMessagesPerPoll(2).get() ;
@Bean(name = PollerMetadata.DEFAULT_POLLER)
public PollerMetadata poller() {
return Pollers.fixedRate( 100 ).maxMessagesPerPoll( 2 ).get();
}
@Bean
public IntegrationFlow cafeFlow() {
return IntegrationFlows.from("itemsChannel")
return IntegrationFlows.from( "itemsChannel" )
.split()
.handle("kitchenService", "cook")
.handle( "kitchenService", "cook" )
.aggregate()
.channel("foodChannel")
.channel( "foodChannel" )
.get();
}
public static void main(String[] args) throws Exception {
AbstractApplicationContext ctx = new AnnotationConfigApplicationContext(App.class);
public static void main( String[] args ) throws Exception {
AbstractApplicationContext ctx = new AnnotationConfigApplicationContext( App.class );
// here we works with cafe using interface
Cafe cafe = ctx.getBean(Cafe.class);
Cafe cafe = ctx.getBean( Cafe.class );
while (true) {
Thread.sleep(1000);
ForkJoinPool pool = ForkJoinPool.commonPool();
Collection<OrderItem> items = generateOrderItems();
System.out.println("New orderItems: " +
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(",")));
while ( true ) {
Thread.sleep( 7000 );
pool.execute( () -> {
Collection<OrderItem> items = generateOrderItems();
System.out.println( "New orderItems: " +
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 OrderItem generateOrderItem() {
return new OrderItem(MENU[RandomUtils.nextInt(0, MENU.length)]);
return new OrderItem( MENU[ RandomUtils.nextInt( 0, MENU.length ) ] );
}
private static Collection<OrderItem> generateOrderItems() {
List<OrderItem> items = new ArrayList<>();
for (int i = 0; i < RandomUtils.nextInt(1, 5); ++i) {
items.add(generateOrderItem());
for ( int i = 0; i < RandomUtils.nextInt( 1, 5 ); ++ i ) {
items.add( generateOrderItem() );
}
return items;
}