mirror of
https://github.com/OtusTeam/Spring.git
synced 2026-05-30 10:50:42 +00:00
2022-11 spring-30 SI
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
HELP.md
|
||||
target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
!**/src/main/**/target/
|
||||
!**/src/test/**/target/
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
build/
|
||||
!**/src/main/**/build/
|
||||
!**/src/test/**/build/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
@@ -0,0 +1,17 @@
|
||||
<?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-30</artifactId>
|
||||
<version>1.0</version>
|
||||
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<modules>
|
||||
<module>spring-30-exercise</module>
|
||||
<module>spring-30-solution</module>
|
||||
</modules>
|
||||
</project>
|
||||
@@ -0,0 +1,54 @@
|
||||
<?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-30-exercise</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.7.9</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-integration</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-messaging</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
package ru.otus.spring.integration;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.integration.annotation.IntegrationComponentScan;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.dsl.MessageChannels;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.messaging.PollableChannel;
|
||||
import org.springframework.messaging.SubscribableChannel;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
|
||||
@SpringBootApplication
|
||||
@IntegrationComponentScan
|
||||
@Slf4j
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
ConfigurableApplicationContext ctx = SpringApplication.run(App.class, args);
|
||||
|
||||
PollableChannel queueChannel = ctx.getBean("queueChannel", PollableChannel.class);
|
||||
SubscribableChannel directChannel = ctx.getBean("directChannel", SubscribableChannel.class);
|
||||
|
||||
|
||||
log.warn("INIT");
|
||||
for (int i = 0; i < 10; i++) {
|
||||
queueChannel.send(MessageBuilder.withPayload("Start " + i).build());
|
||||
}
|
||||
log.warn("INIT FINISH");
|
||||
Thread.sleep(5000);
|
||||
|
||||
log.warn("START");
|
||||
directChannel.subscribe((msg) -> log.warn("Receive msg: {}", msg));
|
||||
new Thread(() -> {
|
||||
while (true) {
|
||||
directChannel.send(queueChannel.receive());
|
||||
}
|
||||
}).start();
|
||||
log.warn("START FINISH");
|
||||
Thread.sleep(5000);
|
||||
|
||||
log.warn("");
|
||||
queueChannel.send(MessageBuilder.withPayload("Hello").build());
|
||||
Thread.sleep(5000);
|
||||
|
||||
log.warn("");
|
||||
queueChannel.send(MessageBuilder.withPayload("Hello2").build());
|
||||
Thread.sleep(5000);
|
||||
|
||||
log.warn("");
|
||||
queueChannel.send(MessageBuilder.withPayload("Hello3").build());
|
||||
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PollableChannel queueChannel() {
|
||||
return new QueueChannel(10);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SubscribableChannel directChannel() {
|
||||
return MessageChannels.direct("channel2").get();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
logging:
|
||||
level:
|
||||
root: info
|
||||
|
||||
org.springframework.integration: debug
|
||||
+98
@@ -0,0 +1,98 @@
|
||||
package ru.otus.spring.integration;
|
||||
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageHeaders;
|
||||
import org.springframework.messaging.support.ErrorMessage;
|
||||
import org.springframework.messaging.support.GenericMessage;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
|
||||
@SuppressWarnings("all")
|
||||
public class MessagesTest {
|
||||
|
||||
@Test
|
||||
public void testCreateSimpleGenericMessage() {
|
||||
// TODO: Создайте сообщение с payload-ом "Hello" с помощью конструктора
|
||||
Message<String> message = null;
|
||||
|
||||
assertNotNull(message);
|
||||
assertEquals(GenericMessage.class, message.getClass());
|
||||
assertNotNull(message.getPayload());
|
||||
assertEquals("Hello", message.getPayload());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateGenericMessage() {
|
||||
// TODO: Создайте сообщение с пользователем с помощью конструктора
|
||||
Message<User> message = null;
|
||||
|
||||
assertNotNull(message);
|
||||
assertEquals(GenericMessage.class, message.getClass());
|
||||
assertNotNull(message.getPayload());
|
||||
assertEquals(new User("John", 23), message.getPayload());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenericMessageWithHeaders() {
|
||||
// TODO: Создайте сообщение с payload-ом "Hello" и header-ом "to":"World"
|
||||
Map<String, Object> headers = null;
|
||||
Message<String> message = null;
|
||||
|
||||
assertNotNull(message);
|
||||
assertEquals("Hello", message.getPayload());
|
||||
assertEquals("World", message.getHeaders().get("to", String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenericMessageWithMessageHeaders() {
|
||||
// TODO: Создайте сообщение с payload-ом "Hello" и header-ом "to":"World"
|
||||
MessageHeaders headers = null;
|
||||
Message<String> message = null;
|
||||
|
||||
assertNotNull(message);
|
||||
assertEquals("Hello", message.getPayload());
|
||||
assertEquals("World", message.getHeaders().get("to", String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testErrorMessage() {
|
||||
// TODO: Создайте сообщение об ошибки с объектом NullPointerException внутри
|
||||
Message errorMessage = null;
|
||||
|
||||
assertNotNull(errorMessage);
|
||||
assertEquals(ErrorMessage.class, errorMessage.getClass());
|
||||
assertEquals(NullPointerException.class, errorMessage.getPayload().getClass());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMessageBuilder() {
|
||||
// TODO: Создайте сообщение с payload-ом "Hello" и header-ом "to":"World" с помощью MessageBuilder
|
||||
Message message = null;
|
||||
|
||||
assertNotNull(message);
|
||||
assertEquals("Hello", message.getPayload());
|
||||
assertEquals("World", message.getHeaders().get("to", String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBuildFromMessage() {
|
||||
Message<User> original = MessageBuilder
|
||||
.withPayload(new User("Kate", 30))
|
||||
.setHeader("processor", "userService")
|
||||
.build();
|
||||
|
||||
// TODO: Создайте новое сообщение с теми же payload и header-ами c помощью MessageBuilder
|
||||
Message<User> newMessage = null;
|
||||
|
||||
assertNotNull(newMessage);
|
||||
assertEquals(original.getPayload(), newMessage.getPayload());
|
||||
assertEquals(original.getHeaders().get("processor"), newMessage.getHeaders().get("processor"));
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package ru.otus.spring.integration;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class User {
|
||||
|
||||
private final String name;
|
||||
private final int age;
|
||||
|
||||
public User(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof User)) return false;
|
||||
User user = (User) o;
|
||||
return age == user.age &&
|
||||
Objects.equals(name, user.name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(name, age);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?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-30-solution</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.7.9</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-integration</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-messaging</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
package ru.otus.spring.integration;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.integration.annotation.IntegrationComponentScan;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.dsl.MessageChannels;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.messaging.PollableChannel;
|
||||
import org.springframework.messaging.SubscribableChannel;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
|
||||
@SpringBootApplication
|
||||
@IntegrationComponentScan
|
||||
@Slf4j
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
ConfigurableApplicationContext ctx = SpringApplication.run(App.class, args);
|
||||
|
||||
PollableChannel queueChannel = ctx.getBean("queueChannel", PollableChannel.class);
|
||||
SubscribableChannel directChannel = ctx.getBean("directChannel", SubscribableChannel.class);
|
||||
|
||||
|
||||
log.warn("INIT");
|
||||
for (int i = 0; i < 10; i++) {
|
||||
queueChannel.send(MessageBuilder.withPayload("Start " + i).build());
|
||||
}
|
||||
log.warn("INIT FINISH");
|
||||
Thread.sleep(5000);
|
||||
|
||||
log.warn("START");
|
||||
directChannel.subscribe((msg) -> log.warn("Receive msg: {}", msg));
|
||||
new Thread(() -> {
|
||||
while (true) {
|
||||
directChannel.send(queueChannel.receive());
|
||||
}
|
||||
}).start();
|
||||
log.warn("START FINISH");
|
||||
Thread.sleep(5000);
|
||||
|
||||
log.warn("");
|
||||
queueChannel.send(MessageBuilder.withPayload("Hello").build());
|
||||
Thread.sleep(5000);
|
||||
|
||||
log.warn("");
|
||||
queueChannel.send(MessageBuilder.withPayload("Hello2").build());
|
||||
Thread.sleep(5000);
|
||||
|
||||
log.warn("");
|
||||
queueChannel.send(MessageBuilder.withPayload("Hello3").build());
|
||||
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PollableChannel queueChannel() {
|
||||
return new QueueChannel(10);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SubscribableChannel directChannel() {
|
||||
return MessageChannels.direct("channel2").get();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
logging:
|
||||
level:
|
||||
root: info
|
||||
|
||||
org.springframework.integration: debug
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
package ru.otus.spring.integration;
|
||||
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageHeaders;
|
||||
import org.springframework.messaging.support.ErrorMessage;
|
||||
import org.springframework.messaging.support.GenericMessage;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
|
||||
@SuppressWarnings("all")
|
||||
public class MessagesTest {
|
||||
|
||||
@Test
|
||||
public void testCreateSimpleGenericMessage() {
|
||||
// TODO: Создайте сообщение с payload-ом "Hello" с помощью конструктора
|
||||
Message<String> message = new GenericMessage<String>("Hello");
|
||||
|
||||
assertNotNull(message);
|
||||
assertEquals(GenericMessage.class, message.getClass());
|
||||
assertNotNull(message.getPayload());
|
||||
assertEquals("Hello", message.getPayload());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateGenericMessage() {
|
||||
// TODO: Создайте сообщение с пользователем с помощью конструктора
|
||||
Message<User> message = new GenericMessage<User>(new User("John", 23));
|
||||
|
||||
assertNotNull(message);
|
||||
assertEquals(GenericMessage.class, message.getClass());
|
||||
assertNotNull(message.getPayload());
|
||||
assertEquals(new User("John", 23), message.getPayload());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenericMessageWithHeaders() {
|
||||
// TODO: Создайте сообщение с payload-ом "Hello" и header-ом "to":"World"
|
||||
Map<String, Object> headers = Collections.singletonMap("to", "World");
|
||||
Message<String> message = new GenericMessage<>("Hello", headers);
|
||||
|
||||
assertNotNull(message);
|
||||
assertEquals("Hello", message.getPayload());
|
||||
assertEquals("World", message.getHeaders().get("to", String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenericMessageWithMessageHeaders() {
|
||||
// TODO: Создайте сообщение с payload-ом "Hello" и header-ом "to":"World"
|
||||
MessageHeaders headers = new MessageHeaders(Collections.singletonMap("to", "World"));
|
||||
Message<String> message = new GenericMessage<>("Hello", headers);
|
||||
|
||||
assertNotNull(message);
|
||||
assertEquals("Hello", message.getPayload());
|
||||
assertEquals("World", message.getHeaders().get("to", String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testErrorMessage() {
|
||||
// TODO: Создайте сообщение об ошибки с объектом NullPointerException внутри
|
||||
Message errorMessage = new ErrorMessage(new NullPointerException());
|
||||
|
||||
assertNotNull(errorMessage);
|
||||
assertEquals(ErrorMessage.class, errorMessage.getClass());
|
||||
assertEquals(NullPointerException.class, errorMessage.getPayload().getClass());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMessageBuilder() {
|
||||
// TODO: Создайте сообщение с payload-ом "Hello" и header-ом "to":"World" с помощью MessageBuilder
|
||||
Message message = MessageBuilder.withPayload("Hello")
|
||||
.setHeader("to", "World")
|
||||
.build();
|
||||
|
||||
assertNotNull(message);
|
||||
assertEquals("Hello", message.getPayload());
|
||||
assertEquals("World", message.getHeaders().get("to", String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBuildFromMessage() {
|
||||
Message<User> original = MessageBuilder
|
||||
.withPayload(new User("Kate", 30))
|
||||
.setHeader("processor", "userService")
|
||||
.build();
|
||||
|
||||
// TODO: Создайте новое сообщение с теми же payload и header-ами c помощью MessageBuilder
|
||||
Message<User> newMessage = MessageBuilder.fromMessage(original).build();
|
||||
|
||||
assertNotNull(newMessage);
|
||||
assertEquals(original.getPayload(), newMessage.getPayload());
|
||||
assertEquals(original.getHeaders().get("processor"), newMessage.getHeaders().get("processor"));
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package ru.otus.spring.integration;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class User {
|
||||
|
||||
private final String name;
|
||||
private final int age;
|
||||
|
||||
public User(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof User)) return false;
|
||||
User user = (User) o;
|
||||
return age == user.age &&
|
||||
Objects.equals(name, user.name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(name, age);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user