使用Spring Boot和WebFlux发送服务器事件

时间:2018-08-20 11:22:39

标签: spring-boot server-sent-events spring-webflux

我正在处理服务器发送事件。我得到

的帮助

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

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

以上示例的问题是所有内容都在一个类中定义。我试图用不同的类来做,但是失败并带有异常:

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]

代码:

    <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);
   }
   }

请帮助。

1 个答案:

答案 0 :(得分:0)

问题出在注释上。 @SpringBootApplication