mirror of
https://github.com/OtusTeam/Spring.git
synced 2026-05-30 10:50:42 +00:00
Пример к занятию Spring Integration. Messages и Channels
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,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-27</artifactId>
|
||||
<version>1.0</version>
|
||||
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<modules>
|
||||
<module>spring-27-exercise</module>
|
||||
<module>spring-27-solution</module>
|
||||
</modules>
|
||||
</project>
|
||||
@@ -0,0 +1,47 @@
|
||||
<?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-27-exercise</artifactId>
|
||||
<version>1.0</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.3.3.RELEASE</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>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
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.messaging.PollableChannel;
|
||||
import org.springframework.messaging.SubscribableChannel;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
|
||||
@SpringBootApplication
|
||||
@IntegrationComponentScan
|
||||
public class App {
|
||||
|
||||
public static void main( String[] args ) throws InterruptedException {
|
||||
ConfigurableApplicationContext ctx = SpringApplication.run( App.class, args );
|
||||
|
||||
PollableChannel channel1 = ctx.getBean( "channel1", PollableChannel.class );
|
||||
SubscribableChannel channel2 = ctx.getBean( "channel2", SubscribableChannel.class );
|
||||
|
||||
channel2.subscribe( System.out::println );
|
||||
new Thread( () -> {
|
||||
while ( true ) {
|
||||
channel2.send( channel1.receive() );
|
||||
}
|
||||
} ).start();
|
||||
|
||||
channel1.send( MessageBuilder.withPayload( "Hello" ).build() );
|
||||
channel1.send( MessageBuilder.withPayload( "Hello2" ).build() );
|
||||
|
||||
Thread.sleep( 2000 );
|
||||
|
||||
channel1.send( MessageBuilder.withPayload( "Hello3" ).build() );
|
||||
|
||||
Thread.sleep( 100000 );
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PollableChannel channel1() {
|
||||
return new QueueChannel( 100 );
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SubscribableChannel channel2() {
|
||||
return MessageChannels.direct( "channel2" ).get();
|
||||
}
|
||||
}
|
||||
+97
@@ -0,0 +1,97 @@
|
||||
package ru.otus.spring.integration;
|
||||
|
||||
import org.junit.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.Assert.assertEquals;
|
||||
import static org.junit.Assert.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,47 @@
|
||||
<?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-27-solution</artifactId>
|
||||
<version>1.0</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.3.3.RELEASE</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>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
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.messaging.PollableChannel;
|
||||
import org.springframework.messaging.SubscribableChannel;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
|
||||
@SpringBootApplication
|
||||
@IntegrationComponentScan
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
ConfigurableApplicationContext ctx = SpringApplication.run(App.class, args);
|
||||
|
||||
PollableChannel channel1 = ctx.getBean("channel1", PollableChannel.class);
|
||||
SubscribableChannel channel2 = ctx.getBean("channel2", SubscribableChannel.class);
|
||||
|
||||
channel2.subscribe(System.out::println);
|
||||
|
||||
new Thread(() -> {
|
||||
while (true) {
|
||||
channel2.send(channel1.receive());
|
||||
}
|
||||
}).start();
|
||||
|
||||
channel1.send(MessageBuilder.withPayload("Hello").build());
|
||||
channel1.send(MessageBuilder.withPayload("Hello2").build());
|
||||
|
||||
Thread.sleep(2000);
|
||||
|
||||
channel1.send(MessageBuilder.withPayload("Hello3").build());
|
||||
|
||||
Thread.sleep(100000);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PollableChannel channel1() {
|
||||
return new QueueChannel(100);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SubscribableChannel channel2() {
|
||||
return MessageChannels.direct("channel2").get();
|
||||
}
|
||||
}
|
||||
+99
@@ -0,0 +1,99 @@
|
||||
package ru.otus.spring.integration;
|
||||
|
||||
import org.junit.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.Assert.assertEquals;
|
||||
import static org.junit.Assert.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