Skip to content

Commit 20bbeb3

Browse files
AbhinabKanrarpivovarit
authored andcommitted
commiting spring reactor (eugenp#922)
* commiting spring reactor * updating exception handling
1 parent b1d2595 commit 20bbeb3

8 files changed

Lines changed: 247 additions & 0 deletions

File tree

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@
163163
<module>spring-thymeleaf</module>
164164
<module>spring-userservice</module>
165165
<module>spring-zuul</module>
166+
<module>spring-reactor</module>
166167

167168
<module>testing</module>
168169

‎spring-reactor/pom.xml‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>spring-reactor</artifactId>
6+
<packaging>jar</packaging>
7+
<version>1.0-SNAPSHOT</version>
8+
<name>spring-reactor</name>
9+
<url>http://maven.apache.org</url>
10+
11+
<parent>
12+
<groupId>org.springframework.boot</groupId>
13+
<artifactId>spring-boot-starter-parent</artifactId>
14+
<version>1.4.2.RELEASE</version>
15+
</parent>
16+
17+
<properties>
18+
<java.version>1.8</java.version>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-web</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.springframework</groupId>
32+
<artifactId>spring-web</artifactId>
33+
</dependency>
34+
<dependency>
35+
<groupId>io.projectreactor</groupId>
36+
<artifactId>reactor-bus</artifactId>
37+
</dependency>
38+
</dependencies>
39+
40+
<build>
41+
<plugins>
42+
<plugin>
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-maven-plugin</artifactId>
45+
</plugin>
46+
</plugins>
47+
</build>
48+
49+
<repositories>
50+
<repository>
51+
<id>spring-snapshots</id>
52+
<name>Spring Snapshots</name>
53+
<url>https://repo.spring.io/libs-snapshot</url>
54+
<snapshots>
55+
<enabled>true</enabled>
56+
</snapshots>
57+
</repository>
58+
</repositories>
59+
<pluginRepositories>
60+
<pluginRepository>
61+
<id>spring-releases</id>
62+
<name>Spring Releases</name>
63+
<url>https://repo.spring.io/libs-release</url>
64+
</pluginRepository>
65+
</pluginRepositories>
66+
67+
</project>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.baeldung;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.boot.CommandLineRunner;
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.ComponentScan;
9+
import org.springframework.context.annotation.Configuration;
10+
11+
import com.baeldung.consumer.NotificationConsumer;
12+
13+
import reactor.Environment;
14+
import reactor.bus.EventBus;
15+
16+
import static reactor.bus.selector.Selectors.$;
17+
18+
19+
@Configuration
20+
@EnableAutoConfiguration
21+
@ComponentScan
22+
public class Application implements CommandLineRunner {
23+
24+
@Autowired
25+
private EventBus eventBus;
26+
27+
@Autowired
28+
private NotificationConsumer notificationConsumer;
29+
30+
@Bean
31+
Environment env() {
32+
return Environment.initializeIfEmpty().assignErrorJournal();
33+
}
34+
35+
@Bean
36+
EventBus createEventBus(Environment env) {
37+
return EventBus.create(env, Environment.THREAD_POOL);
38+
}
39+
40+
@Override
41+
public void run(String... args) throws Exception {
42+
eventBus.on($("notificationConsumer"), notificationConsumer);
43+
}
44+
45+
public static void main(String[] args){
46+
SpringApplication.run(Application.class, args);
47+
}
48+
49+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.consumer;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Service;
5+
6+
import com.baeldung.doman.NotificationData;
7+
import com.baeldung.service.NotificationService;
8+
9+
import reactor.bus.Event;
10+
import reactor.fn.Consumer;
11+
12+
@Service
13+
public class NotificationConsumer implements Consumer<Event<NotificationData>> {
14+
15+
@Autowired
16+
private NotificationService notificationService;
17+
18+
@Override
19+
public void accept(Event<NotificationData> notificationDataEvent) {
20+
21+
NotificationData notificationData = notificationDataEvent.getData();
22+
try {
23+
notificationService.initiateNotofication(notificationData);
24+
} catch (InterruptedException e) {}
25+
26+
}
27+
28+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.controller;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Controller;
5+
import org.springframework.web.bind.annotation.PathVariable;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RequestMethod;
8+
9+
import com.baeldung.doman.NotificationData;
10+
11+
import reactor.bus.Event;
12+
import reactor.bus.EventBus;
13+
14+
@Controller
15+
public class NotificationController {
16+
17+
@Autowired
18+
private EventBus eventBus;
19+
20+
@RequestMapping(value = "/startNotification/{param}", method = RequestMethod.GET)
21+
public void startNotification(@PathVariable("param") String param) {
22+
23+
int notificationSize = Integer.parseInt(param);
24+
25+
for(int i = 0; i < notificationSize; i++) {
26+
27+
NotificationData data = new NotificationData();
28+
data.setId(i);
29+
30+
eventBus.notify("notificationConsumer",Event.wrap(data));
31+
32+
System.out.println("Notification " +i +": notification task submitted successfully");
33+
}
34+
35+
}
36+
37+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.doman;
2+
3+
public class NotificationData {
4+
5+
private long id;
6+
private String name;
7+
private String email;
8+
private String mobile;
9+
10+
public long getId() {
11+
return id;
12+
}
13+
public void setId(long id) {
14+
this.id = id;
15+
}
16+
public String getName() {
17+
return name;
18+
}
19+
public void setName(String name) {
20+
this.name = name;
21+
}
22+
public String getEmail() {
23+
return email;
24+
}
25+
public void setEmail(String email) {
26+
this.email = email;
27+
}
28+
public String getMobile() {
29+
return mobile;
30+
}
31+
public void setMobile(String mobile) {
32+
this.mobile = mobile;
33+
}
34+
35+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.service;
2+
3+
import com.baeldung.doman.NotificationData;
4+
5+
public interface NotificationService {
6+
7+
public void initiateNotofication(NotificationData notificationData) throws InterruptedException;
8+
9+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.service.impl;
2+
3+
import org.springframework.stereotype.Service;
4+
5+
import com.baeldung.doman.NotificationData;
6+
import com.baeldung.service.NotificationService;
7+
8+
@Service
9+
public class NotificationServiceimpl implements NotificationService {
10+
11+
@Override
12+
public void initiateNotofication(NotificationData notificationData) throws InterruptedException {
13+
14+
System.out.println("Notification service started for Notification ID: " +notificationData.getId());
15+
16+
Thread.sleep(5000);
17+
18+
System.out.println("Notification service ended for Notification ID: " +notificationData.getId());
19+
}
20+
21+
}

0 commit comments

Comments
 (0)