Skip to content

Commit

Permalink
2023-07 spring-30-endpoints-flow-components
Browse files Browse the repository at this point in the history
  • Loading branch information
vlad-iv committed Nov 15, 2023
1 parent 8e58e52 commit 2839a0b
Show file tree
Hide file tree
Showing 31 changed files with 960 additions and 0 deletions.
24 changes: 24 additions & 0 deletions 2023-07/spring-30-endpoints-flow-components/.gitignore
Original file line number Diff line number Diff line change
@@ -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/
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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>endpoints-flow-components-exercise</artifactId>
<version>1.0-SNAPSHOT</version>

<parent>
<groupId>ru.otus</groupId>
<artifactId>endpoints-flow-components</artifactId>
<version>1.0</version>
</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>${apache.commons.version}</version>
</dependency>

<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>${snakeyaml.version}</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ru.otus.spring.integration.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.dsl.MessageChannelSpec;
import org.springframework.integration.dsl.MessageChannels;

@SuppressWarnings("unused")
@Configuration
public class IntegrationConfig {
@Bean
public MessageChannelSpec<?, ?> itemsChannel() {
return MessageChannels.queue(10);
}

@Bean
public MessageChannelSpec<?, ?> foodChannel() {
return MessageChannels.publishSubscribe();
}

// TODO: create default poller

@Bean
public IntegrationFlow cafeFlow() {
return IntegrationFlow.from(itemsChannel())
// TODO: cook OrderItem in the kitchen
// TODO*: add splitter and aggregator
// TODO: forward it to the publish subscriber channel
.get();
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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 Cafe {

// TODO: add gateway annotation with required channels
Food process(OrderItem orderItem);
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ru.otus.spring.integration.services;


import java.util.List;
import java.util.Map;

import org.springframework.integration.annotation.Gateway;
import org.springframework.integration.annotation.MessagingGateway;

import ru.otus.spring.integration.domain.OrderItem;

@MessagingGateway
public interface CafeService {

@Gateway(requestChannel = "itemsChannel", replyChannel = "foodChannel")
List<Map<String, Object>> process(List<OrderItem> orderItem);
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package ru.otus.spring.integration.services;

import org.springframework.stereotype.Service;

import lombok.extern.slf4j.Slf4j;
import ru.otus.spring.integration.domain.Food;
import ru.otus.spring.integration.domain.OrderItem;

@Service
@Slf4j
public class KitchenServiceImpl implements KitchenService {

@Override
public Food cook(OrderItem orderItem) {
log.info("Cooking {}", orderItem.getItemName());
delay();
log.info("Cooking {} done", orderItem.getItemName());

return new Food(orderItem.getItemName());
}

private static void delay() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package ru.otus.spring.integration.services;

public interface OrderService {
void startGenerateOrdersLoop();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package ru.otus.spring.integration.services;

import org.apache.commons.lang3.RandomUtils;
import org.springframework.stereotype.Service;

import lombok.extern.slf4j.Slf4j;
import ru.otus.spring.integration.domain.Food;
import ru.otus.spring.integration.domain.OrderItem;

@Service
@Slf4j
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;
}

@Override
public void startGenerateOrdersLoop() {
for (int i = 0; i < 10; i++) {
OrderItem orderItem = generateOrderItem();
int num = i + 1;
log.info("{}, New orderItem: {}", num, orderItem.getItemName());
Food food = cafe.process(orderItem);
log.info("{}, Ready food: {}", num, food.getName());
delay();
}
}

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);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
logging:
level:
root: info

org.springframework.integration: debug
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?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>endpoints-flow-components-solution</artifactId>
<version>1.0-SNAPSHOT</version>

<parent>
<groupId>ru.otus</groupId>
<artifactId>endpoints-flow-components</artifactId>
<version>1.0</version>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-integration</artifactId>
</dependency>

<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>${snakeyaml.version}</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${apache.commons.version}</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>ru.otus.spring.integration.App</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>${checkstyle-plugin.version}</version>
<dependencies>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>${checkstyle.version}</version>
</dependency>
</dependencies>
<configuration>
<configLocation>${checkstyle.config.url}</configLocation>
</configuration>
<executions>
<execution>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ru.otus.spring.integration;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import lombok.extern.slf4j.Slf4j;
import ru.otus.spring.integration.services.OrderService;

@Slf4j
@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();
}
}
Loading

0 comments on commit 2839a0b

Please sign in to comment.