Search code examples
spring-bootserver-sent-eventsspring-webflux

Server Sent Events with Spring Boot and WebFlux


I am working on Server Sent Events. I took help from

http://sinhamohit.com/writing/spring-boot-reactive-sse

https://github.com/mohitsinha/spring-boot-reactive-sse

The issue with the above example is everything is defined in one class. I am trying to do it different classes but it fails with exception:

2018-08-20 17:03:15.521  WARN 10964 --- [           main] 
onfigReactiveWebServerApplicationContext : Exception encountered during 
context initialization - cancelling refresh attempt: 
org.springframework.context.ApplicationContextException: Unable to start 
reactive web server; nested exception is 
org.springframework.context.ApplicationContextException: Unable to start 
ReactiveWebApplicationContext due to missing ReactiveWebServerFactory bean.
2018-08-20 17:03:15.599 ERROR 10964 --- [           main] 
o.s.boot.SpringApplication               : Application run failed

org.springframework.context.ApplicationContextException: Unable to start 
reactive web server; nested exception is 
org.springframework.context.ApplicationContextException: Unable to start 
ReactiveWebApplicationContext due to missing ReactiveWebServerFactory bean.
at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.onRefresh(ReactiveWebServerApplicationContext.java:76) ~[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:544) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.refresh(ReactiveWebServerApplicationContext.java:61) ~[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:398) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:330) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1258) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at hello.SpringBootApplication.main(SpringBootApplication.java:8) [classes/:na]

Code:

    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.4.RELEASE</version>
    </parent>

   <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
   </dependencies>


   @RestController
   @RequestMapping("/stock/transaction")
   public class StockTransactionController {

       @Autowired
       StockTransactionService stockTransactionService;

       @GetMapping(produces = MediaType.APPLICATION_STREAM_JSON_VALUE)
       public Flux<StockTransaction> stockTransactionEvents() {
           return stockTransactionService.getStockTransactions();
      }
   }

   @Service
   public class StockTransactionService {

     List<Stock> stockList = new ArrayList<>();
     List<String> stockNames = 
          Arrays.asList("mango,banana,guava,infinity".split(","));

     @Bean
     CommandLineRunner commandLineRunner() {
        return args -> {
        createRandomStock();
        stockList.forEach(System.out::println);
     };
     }

    public Flux<StockTransaction> getStockTransactions() {

    Flux<Long> interval = Flux.interval(Duration.ofSeconds(1));
    interval.subscribe((i) -> stockList.forEach(stock -> 
    stock.setPrice(changePrice(stock.getPrice()))));

    Flux<StockTransaction> stockTransactionFlux = Flux
            .fromStream(Stream.generate(() -> new 
    StockTransaction(getRandomUser(), getRandomStock(), new Date())));
    return Flux.zip(interval, stockTransactionFlux).map(Tuple2::getT2);
   }
   }

Please help.


Solution

  • The problem was with annotation. @SpringBootApplication