2

I am new to Reactive programming and Spring GraphQL subscription. I want to make my App 1 publish resultSet from JPA findAll() method through Flux and my client listen to it continuously.

I want Flux publish-on-demand (e.g. when a message comes from a NATS message broker). Below is my code so far to try to achieve it. Currently, the subscriber is not able to receive data from the server.

data was emitted successfully but I doubt that the problem comes from my Resolver. Appreciate a lot for your help.

SubscribeConfiguration

@Bean
public Sinks.Many<List<ToolOrder>> valueSink() {
    return Sinks.many().multicast().onBackpressureBuffer();
}

@Bean
public Flux<List<ToolOrder>> valueFlux() {
    return valueSink().asFlux();
}

SubscriptionResolver

public Publisher<List<ToolOrder>> getAllToolOrder() {
    System.out.println("reach");
    return subscriptionConfiguration.valueFlux().map(e -> e);
}

NatsService

    public void handleData(Message info) {
    String message = new String(info.getData());
    log.info("received message from nats: {}, topic: {}", message, info.getSubject());
    try {
        List<ToolOrder> toolOrders = toolOrderRepository.findAll();
        Sinks.EmitResult result = subscriptionConfiguration.valueSink().tryEmitNext(toolOrders);
        log.info(String.valueOf(result));
        if (result.isFailure()) {
            // do something here, since emission failed
            log.info(String.valueOf(result));
        }
    } catch (JsonProcessingException e) {
        throw new RuntimeException("Could not convert json", e);
    }
}
Shido
  • 21
  • 2

0 Answers0