Skip to content

Commit 06f1064

Browse files
committed
Refactor Spring-Reactor samples
1 parent 20bbeb3 commit 06f1064

7 files changed

Lines changed: 102 additions & 99 deletions

File tree

apache-poi/src/main/java/com/baeldung/poi/word/WordDocument.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.baeldung.poi.word;
22

3+
import org.apache.poi.util.Units;
4+
import org.apache.poi.xwpf.usermodel.*;
5+
36
import java.io.FileOutputStream;
47
import java.io.IOException;
58
import java.net.URISyntaxException;
@@ -9,13 +12,6 @@
912
import java.util.stream.Collectors;
1013
import java.util.stream.Stream;
1114

12-
import org.apache.poi.util.Units;
13-
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
14-
import org.apache.poi.xwpf.usermodel.UnderlinePatterns;
15-
import org.apache.poi.xwpf.usermodel.XWPFDocument;
16-
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
17-
import org.apache.poi.xwpf.usermodel.XWPFRun;
18-
1915
public class WordDocument {
2016
public static String logo = "logo-leaf.png";
2117
public static String paragraph1 = "poi-word-para1.txt";

spring-reactor/src/main/java/com/baeldung/Application.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,34 @@
1515

1616
import static reactor.bus.selector.Selectors.$;
1717

18-
1918
@Configuration
2019
@EnableAutoConfiguration
2120
@ComponentScan
2221
public class Application implements CommandLineRunner {
23-
24-
@Autowired
25-
private EventBus eventBus;
26-
27-
@Autowired
28-
private NotificationConsumer notificationConsumer;
29-
22+
23+
@Autowired
24+
private EventBus eventBus;
25+
26+
@Autowired
27+
private NotificationConsumer notificationConsumer;
28+
3029
@Bean
3130
Environment env() {
3231
return Environment.initializeIfEmpty().assignErrorJournal();
3332
}
34-
33+
3534
@Bean
3635
EventBus createEventBus(Environment env) {
37-
return EventBus.create(env, Environment.THREAD_POOL);
36+
return EventBus.create(env, Environment.THREAD_POOL);
3837
}
3938

40-
@Override
41-
public void run(String... args) throws Exception {
42-
eventBus.on($("notificationConsumer"), notificationConsumer);
43-
}
39+
@Override
40+
public void run(String... args) throws Exception {
41+
eventBus.on($("notificationConsumer"), notificationConsumer);
42+
}
4443

45-
public static void main(String[] args){
46-
SpringApplication.run(Application.class, args);
47-
}
44+
public static void main(String[] args) {
45+
SpringApplication.run(Application.class, args);
46+
}
4847

4948
}

spring-reactor/src/main/java/com/baeldung/consumer/NotificationConsumer.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,18 @@
1212
@Service
1313
public class NotificationConsumer implements Consumer<Event<NotificationData>> {
1414

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-
}
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.initiateNotification(notificationData);
24+
} catch (InterruptedException e) {
25+
}
26+
27+
}
2728

2829
}

spring-reactor/src/main/java/com/baeldung/controller/NotificationController.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,25 @@
1313

1414
@Controller
1515
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-
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+
3737
}
Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,42 @@
11
package com.baeldung.doman;
22

33
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-
}
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+
14+
public void setId(long id) {
15+
this.id = id;
16+
}
17+
18+
public String getName() {
19+
return name;
20+
}
21+
22+
public void setName(String name) {
23+
this.name = name;
24+
}
25+
26+
public String getEmail() {
27+
return email;
28+
}
29+
30+
public void setEmail(String email) {
31+
this.email = email;
32+
}
33+
34+
public String getMobile() {
35+
return mobile;
36+
}
37+
38+
public void setMobile(String mobile) {
39+
this.mobile = mobile;
40+
}
3441

3542
}

spring-reactor/src/main/java/com/baeldung/service/NotificationService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44

55
public interface NotificationService {
66

7-
public void initiateNotofication(NotificationData notificationData) throws InterruptedException;
8-
7+
void initiateNotification(NotificationData notificationData) throws InterruptedException;
8+
99
}

spring-reactor/src/main/java/com/baeldung/service/impl/NotificationServiceimpl.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77

88
@Service
99
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-
}
10+
11+
@Override
12+
public void initiateNotification(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+
}
2020

2121
}

0 commit comments

Comments
 (0)