Refactor Spring-Reactor samples

This commit is contained in:
pivovarit 2016-12-28 08:19:55 +01:00
parent 20bbeb3e65
commit 06f10646b1
7 changed files with 99 additions and 96 deletions

View File

@ -1,5 +1,8 @@
package com.baeldung.poi.word; package com.baeldung.poi.word;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.*;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.net.URISyntaxException; import java.net.URISyntaxException;
@ -9,13 +12,6 @@ import java.nio.file.Paths;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.UnderlinePatterns;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
public class WordDocument { public class WordDocument {
public static String logo = "logo-leaf.png"; public static String logo = "logo-leaf.png";
public static String paragraph1 = "poi-word-para1.txt"; public static String paragraph1 = "poi-word-para1.txt";

View File

@ -15,35 +15,34 @@ import reactor.bus.EventBus;
import static reactor.bus.selector.Selectors.$; import static reactor.bus.selector.Selectors.$;
@Configuration @Configuration
@EnableAutoConfiguration @EnableAutoConfiguration
@ComponentScan @ComponentScan
public class Application implements CommandLineRunner { public class Application implements CommandLineRunner {
@Autowired @Autowired
private EventBus eventBus; private EventBus eventBus;
@Autowired @Autowired
private NotificationConsumer notificationConsumer; private NotificationConsumer notificationConsumer;
@Bean @Bean
Environment env() { Environment env() {
return Environment.initializeIfEmpty().assignErrorJournal(); return Environment.initializeIfEmpty().assignErrorJournal();
} }
@Bean @Bean
EventBus createEventBus(Environment env) { EventBus createEventBus(Environment env) {
return EventBus.create(env, Environment.THREAD_POOL); return EventBus.create(env, Environment.THREAD_POOL);
} }
@Override @Override
public void run(String... args) throws Exception { public void run(String... args) throws Exception {
eventBus.on($("notificationConsumer"), notificationConsumer); eventBus.on($("notificationConsumer"), notificationConsumer);
} }
public static void main(String[] args){ public static void main(String[] args) {
SpringApplication.run(Application.class, args); SpringApplication.run(Application.class, args);
} }
} }

View File

@ -12,17 +12,18 @@ import reactor.fn.Consumer;
@Service @Service
public class NotificationConsumer implements Consumer<Event<NotificationData>> { public class NotificationConsumer implements Consumer<Event<NotificationData>> {
@Autowired @Autowired
private NotificationService notificationService; private NotificationService notificationService;
@Override @Override
public void accept(Event<NotificationData> notificationDataEvent) { public void accept(Event<NotificationData> notificationDataEvent) {
NotificationData notificationData = notificationDataEvent.getData(); NotificationData notificationData = notificationDataEvent.getData();
try { try {
notificationService.initiateNotofication(notificationData); notificationService.initiateNotification(notificationData);
} catch (InterruptedException e) {} } catch (InterruptedException e) {
}
}
}
} }

View File

@ -13,25 +13,25 @@ import reactor.bus.EventBus;
@Controller @Controller
public class NotificationController { public class NotificationController {
@Autowired
private EventBus eventBus;
@RequestMapping(value = "/startNotification/{param}", method = RequestMethod.GET) @Autowired
public void startNotification(@PathVariable("param") String param) { private EventBus eventBus;
int notificationSize = Integer.parseInt(param); @RequestMapping(value = "/startNotification/{param}", method = RequestMethod.GET)
public void startNotification(@PathVariable("param") String param) {
for(int i = 0; i < notificationSize; i++) {
int notificationSize = Integer.parseInt(param);
NotificationData data = new NotificationData();
data.setId(i); for (int i = 0; i < notificationSize; i++) {
eventBus.notify("notificationConsumer",Event.wrap(data)); NotificationData data = new NotificationData();
data.setId(i);
System.out.println("Notification " +i +": notification task submitted successfully");
} eventBus.notify("notificationConsumer", Event.wrap(data));
} System.out.println("Notification " + i + ": notification task submitted successfully");
}
}
} }

View File

@ -1,35 +1,42 @@
package com.baeldung.doman; package com.baeldung.doman;
public class NotificationData { public class NotificationData {
private long id;
private String name;
private String email;
private String mobile;
public long getId() { private long id;
return id; private String name;
} private String email;
public void setId(long id) { private String mobile;
this.id = id;
} public long getId() {
public String getName() { return id;
return name; }
}
public void setName(String name) { public void setId(long id) {
this.name = name; this.id = id;
} }
public String getEmail() {
return email; public String getName() {
} return name;
public void setEmail(String email) { }
this.email = email;
} public void setName(String name) {
public String getMobile() { this.name = name;
return mobile; }
}
public void setMobile(String mobile) { public String getEmail() {
this.mobile = mobile; return email;
} }
public void setEmail(String email) {
this.email = email;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
} }

View File

@ -4,6 +4,6 @@ import com.baeldung.doman.NotificationData;
public interface NotificationService { public interface NotificationService {
public void initiateNotofication(NotificationData notificationData) throws InterruptedException; void initiateNotification(NotificationData notificationData) throws InterruptedException;
} }

View File

@ -7,15 +7,15 @@ import com.baeldung.service.NotificationService;
@Service @Service
public class NotificationServiceimpl implements NotificationService { public class NotificationServiceimpl implements NotificationService {
@Override
public void initiateNotofication(NotificationData notificationData) throws InterruptedException {
System.out.println("Notification service started for Notification ID: " +notificationData.getId()); @Override
public void initiateNotification(NotificationData notificationData) throws InterruptedException {
Thread.sleep(5000);
System.out.println("Notification service started for Notification ID: " + notificationData.getId());
System.out.println("Notification service ended for Notification ID: " +notificationData.getId());
} Thread.sleep(5000);
System.out.println("Notification service ended for Notification ID: " + notificationData.getId());
}
} }