0

I am working on Schedulers in reactive streams and using a Flux and Scheduler on this Flux using the publishOn method as follows:

    System.out.println("*********Calling Concurrency************");
    List<Integer> elements = new ArrayList<>();
    Flux.range(1, 1000)
      .log()
      .map(i -> i * 2)
      .publishOn(Schedulers.parallel())
      //.subscribeOn(Schedulers.parallel())
      .subscribe(elements::add);
    System.out.println("-------------------------------------");

for which i got the following info logs:

*********Calling Concurrency************
[info] | onSubscribe([Synchronous Fuseable] FluxRange.RangeSubscription)
[info] | request(256)
[info] | onNext(1)
[info] | onNext(2)
[info] | onNext(3)
[info] | onNext(4)
[info] | onNext(5)
[info] | onNext(6)
[info] | onNext(7)
[info] | onNext(8)
[info] | onNext(9)
.....
.....
[info] | onNext(444)
[info] | onNext(445)
[info] | onNext(446)
[info] | onNext(447)
[info] | onNext(448)
-------------------------------------
[info] | request(192)
.....
.....
[info] | onNext(999)
[info] | onNext(1000)
[info] | onComplete()
[info] | request(192)

and there is no information about the thread execution and processing. Also, sometimes 192 elements are requested and sometimes 256 elements are requested.

Here is the dependency which i am using:

<dependency> 
     <groupId>com.googlecode.slf4j-maven-plugin-log</groupId>
     <artifactId>slf4j-maven-plugin-log</artifactId> 

     <version>1.0.0</version> 
</dependency>

How can i get the log information about the current thread/parallel thread execution? Please suggest.

KayV
  • 12,987
  • 11
  • 98
  • 148

2 Answers2

2

To get all required information just put .log() after .publishOn()

    Flux.range(1, 1000)
        .map(i -> i * 2)
        .publishOn(Schedulers.parallel())
        //.subscribeOn(Schedulers.parallel())
        .log()
        .blockLast();

the final output will look next:

        12:13:21.964 [main] DEBUG reactor.util.Loggers$LoggerFactory - Using Slf4j logging framework
        12:13:21.997 [main] INFO reactor.Flux.PublishOn.1 - | onSubscribe([Fuseable] FluxPublishOn.PublishOnSubscriber)
        12:13:21.999 [main] INFO reactor.Flux.PublishOn.1 - | request(unbounded)
        12:13:22.002 [parallel-1] INFO reactor.Flux.PublishOn.1 - | onNext(2)
        12:13:22.002 [parallel-1] INFO reactor.Flux.PublishOn.1 - | onNext(4)
        12:13:22.002 [parallel-1] INFO reactor.Flux.PublishOn.1 - | onNext(6)
        12:13:22.002 [parallel-1] INFO reactor.Flux.PublishOn.1 - | onNext(8)
        12:13:22.002 [parallel-1] INFO reactor.Flux.PublishOn.1 - | onNext(10)
        12:13:22.002 [parallel-1] INFO reactor.Flux.PublishOn.1 - | onNext(12)

so in that case information about Thread will be there

Note: Output might varying depends on used Logger library

Oleh Dokuka
  • 11,613
  • 5
  • 40
  • 65
2

This looks like a logger misconfiguration on your side.

Reactor will pick up that you use SLF4J but you still need a correctly configured logging implementation, like Logback,and associated appenders that will log the thread name.

If you don't want to bother at all with a logging framework, you can call Loggers.useConsoleLoggers() and it will print on the console in a simplified format that includes the current thread name. I recommend to only do that for a 1 shot run (like a debugging session) though, not in production code...

Simon Baslé
  • 27,105
  • 5
  • 69
  • 70