mirror of
https://github.com/OtusTeam/Spring.git
synced 2026-05-30 10:50:42 +00:00
Примеры к занятию
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
target/
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/build/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
@@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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>ru.otus</groupId>
|
||||
<artifactId>spring-31</artifactId>
|
||||
<version>1.0</version>
|
||||
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<modules>
|
||||
<module>spring-31-exercise</module>
|
||||
<module>spring-31-solution</module>
|
||||
|
||||
</modules>
|
||||
</project>
|
||||
@@ -0,0 +1,38 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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>ru.otus</groupId>
|
||||
<artifactId>spring-31-exercise</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.7.8</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-integration</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>3.7</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package ru.otus.spring.integration;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import ru.otus.spring.integration.services.OrderService;
|
||||
|
||||
|
||||
@SpringBootApplication
|
||||
public class App {
|
||||
public static void main(String[] args) {
|
||||
ConfigurableApplicationContext ctx = SpringApplication.run(App.class, args);
|
||||
OrderService orderService = ctx.getBean(OrderService.class);
|
||||
orderService.startGenerateOrdersLoop();
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package ru.otus.spring.integration.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.integration.channel.PublishSubscribeChannel;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.dsl.IntegrationFlow;
|
||||
import org.springframework.integration.dsl.IntegrationFlows;
|
||||
import org.springframework.integration.dsl.MessageChannels;
|
||||
|
||||
@Configuration
|
||||
public class IntegrationConfig {
|
||||
@Bean
|
||||
public QueueChannel itemsChannel() {
|
||||
return MessageChannels.queue(10).get();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PublishSubscribeChannel foodChannel() {
|
||||
return MessageChannels.publishSubscribe().get();
|
||||
}
|
||||
|
||||
// TODO: create default poller
|
||||
|
||||
@Bean
|
||||
public IntegrationFlow cafeFlow() {
|
||||
return IntegrationFlows.from("itemsChannel")
|
||||
// TODO: cook OrderItem in the kitchen
|
||||
// TODO*: add splitter and aggregator
|
||||
// TODO: forward it to the publish subscriber channel
|
||||
.get();
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package ru.otus.spring.integration.domain;
|
||||
|
||||
|
||||
public class Food {
|
||||
|
||||
private final String name;
|
||||
|
||||
public Food(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package ru.otus.spring.integration.domain;
|
||||
|
||||
public class OrderItem {
|
||||
|
||||
private final String itemName;
|
||||
|
||||
public OrderItem(String itemName) {
|
||||
this.itemName = itemName;
|
||||
}
|
||||
|
||||
public String getItemName() {
|
||||
return itemName;
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package ru.otus.spring.integration.services;
|
||||
|
||||
|
||||
import ru.otus.spring.integration.domain.Food;
|
||||
import ru.otus.spring.integration.domain.OrderItem;
|
||||
|
||||
// TODO: add messaging gateway annotation
|
||||
public interface CafeGateway {
|
||||
|
||||
// TODO: add gateway annotation with required channels
|
||||
Food process(OrderItem orderItem);
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package ru.otus.spring.integration.services;
|
||||
|
||||
import ru.otus.spring.integration.domain.Food;
|
||||
import ru.otus.spring.integration.domain.OrderItem;
|
||||
|
||||
public interface KitchenService {
|
||||
|
||||
Food cook(OrderItem orderItem);
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package ru.otus.spring.integration.services;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.otus.spring.integration.domain.Food;
|
||||
import ru.otus.spring.integration.domain.OrderItem;
|
||||
|
||||
@Service
|
||||
public class KitchenServiceImpl implements KitchenService {
|
||||
|
||||
@Override
|
||||
public Food cook(OrderItem orderItem) {
|
||||
System.out.println("Cooking " + orderItem.getItemName());
|
||||
delay();
|
||||
System.out.println("Cooking " + orderItem.getItemName() + " done");
|
||||
return new Food(orderItem.getItemName());
|
||||
}
|
||||
|
||||
private static void delay() {
|
||||
try {
|
||||
Thread.sleep(3000);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package ru.otus.spring.integration.services;
|
||||
|
||||
public interface OrderService {
|
||||
void startGenerateOrdersLoop();
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package ru.otus.spring.integration.services;
|
||||
|
||||
import org.apache.commons.lang3.RandomUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.otus.spring.integration.domain.Food;
|
||||
import ru.otus.spring.integration.domain.OrderItem;
|
||||
|
||||
@Service
|
||||
public class OrderServiceImpl implements OrderService {
|
||||
private static final String[] MENU = {"coffee", "tea", "smoothie", "whiskey", "beer", "cola", "water"};
|
||||
|
||||
private final CafeGateway cafe;
|
||||
|
||||
public OrderServiceImpl(CafeGateway cafe) {
|
||||
this.cafe = cafe;
|
||||
}
|
||||
|
||||
public void startGenerateOrdersLoop() {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
delay();
|
||||
|
||||
OrderItem orderItem = generateOrderItem();
|
||||
System.out.println("New orderItem: " + orderItem.getItemName());
|
||||
Food food = cafe.process(orderItem);
|
||||
System.out.println("Ready food: " + food.getName());
|
||||
}
|
||||
}
|
||||
|
||||
private static OrderItem generateOrderItem() {
|
||||
return new OrderItem(MENU[RandomUtils.nextInt(0, MENU.length)]);
|
||||
}
|
||||
|
||||
private void delay() {
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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>ru.otus</groupId>
|
||||
<artifactId>spring-31-solution</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.7.8</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-integration</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>3.7</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package ru.otus.spring.integration;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import ru.otus.spring.integration.services.OrderService;
|
||||
|
||||
|
||||
@SpringBootApplication
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ConfigurableApplicationContext ctx = SpringApplication.run(App.class, args);
|
||||
OrderService orderService = ctx.getBean(OrderService.class);
|
||||
orderService.startGenerateOrdersLoop();
|
||||
}
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package ru.otus.spring.integration.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.integration.channel.PublishSubscribeChannel;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
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.services.KitchenServiceImpl;
|
||||
|
||||
@Configuration
|
||||
public class IntegrationConfig {
|
||||
|
||||
@Bean
|
||||
public QueueChannel itemsChannel() {
|
||||
return MessageChannels.queue(10).get();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PublishSubscribeChannel foodChannel() {
|
||||
return MessageChannels.publishSubscribe().get();
|
||||
}
|
||||
|
||||
@Bean(name = PollerMetadata.DEFAULT_POLLER)
|
||||
public PollerMetadata poller() {
|
||||
return Pollers.fixedRate(100).maxMessagesPerPoll(2).get();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public IntegrationFlow cafeFlow(KitchenServiceImpl kitchenService) {
|
||||
return IntegrationFlows.from(itemsChannel())
|
||||
.split()
|
||||
.handle(kitchenService, "cook")
|
||||
.aggregate()
|
||||
.channel(foodChannel())
|
||||
.get();
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package ru.otus.spring.integration.domain;
|
||||
|
||||
|
||||
public class Food {
|
||||
|
||||
private final String name;
|
||||
|
||||
public Food(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package ru.otus.spring.integration.domain;
|
||||
|
||||
public class OrderItem {
|
||||
|
||||
private final String itemName;
|
||||
|
||||
public OrderItem(String itemName) {
|
||||
this.itemName = itemName;
|
||||
}
|
||||
|
||||
public String getItemName() {
|
||||
return itemName;
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package ru.otus.spring.integration.services;
|
||||
|
||||
|
||||
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;
|
||||
|
||||
@MessagingGateway
|
||||
public interface CafeGateway {
|
||||
|
||||
@Gateway(requestChannel = "itemsChannel", replyChannel = "foodChannel")
|
||||
Collection<Food> process(Collection<OrderItem> orderItem);
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package ru.otus.spring.integration.services;
|
||||
|
||||
import ru.otus.spring.integration.domain.Food;
|
||||
import ru.otus.spring.integration.domain.OrderItem;
|
||||
|
||||
public interface KitchenService {
|
||||
|
||||
Food cook(OrderItem orderItem);
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package ru.otus.spring.integration.services;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.otus.spring.integration.domain.Food;
|
||||
import ru.otus.spring.integration.domain.OrderItem;
|
||||
|
||||
@Service
|
||||
public class KitchenServiceImpl implements KitchenService {
|
||||
|
||||
@Override
|
||||
public Food cook(OrderItem orderItem) {
|
||||
System.out.println("Cooking " + orderItem.getItemName());
|
||||
delay();
|
||||
System.out.println("Cooking " + orderItem.getItemName() + " done");
|
||||
return new Food(orderItem.getItemName());
|
||||
}
|
||||
|
||||
private static void delay() {
|
||||
try {
|
||||
Thread.sleep(3000);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package ru.otus.spring.integration.services;
|
||||
|
||||
public interface OrderService {
|
||||
void startGenerateOrdersLoop();
|
||||
}
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
package ru.otus.spring.integration.services;
|
||||
|
||||
import org.apache.commons.lang3.RandomUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.otus.spring.integration.domain.Food;
|
||||
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;
|
||||
|
||||
@Service
|
||||
public class OrderServiceImpl implements OrderService {
|
||||
private static final String[] MENU = {"coffee", "tea", "smoothie", "whiskey", "beer", "cola", "water"};
|
||||
|
||||
private final CafeGateway cafe;
|
||||
|
||||
public OrderServiceImpl(CafeGateway cafe) {
|
||||
this.cafe = cafe;
|
||||
}
|
||||
|
||||
public void startGenerateOrdersLoop() {
|
||||
ForkJoinPool pool = ForkJoinPool.commonPool();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
delay();
|
||||
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)]);
|
||||
}
|
||||
|
||||
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 void delay() {
|
||||
try {
|
||||
Thread.sleep(7000);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user