Reactor Scheduler with anonymous thread
I'm testing how reactor works, created such code which is quite similar to what can be found in reactor documentation.
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Scheduler;
import reactor.core.scheduler.Schedulers;
@SpringBootTest
@RunWith(SpringRunner.class)
public class ReactorApplicationTests {
@Test
public void publishOnThreadTest() {
Scheduler s = Schedulers.newParallel("parallel-scheduler", 4);
final Mono<String> mono = Mono.just("Publish on test: n")
.map(msg -> msg + "before: " + Thread.currentThread() )
.publishOn(s)
.map(msg -> msg + "nafter: " + Thread.currentThread());
new Thread(() -> mono.subscribe(System.out::println)).start();
}
}
And I cannot make it run, what am I doing wrong? With just subscribe it works but I wanted to look at thread used and play a bit with it.
java multithreading project-reactor reactor
add a comment |
I'm testing how reactor works, created such code which is quite similar to what can be found in reactor documentation.
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Scheduler;
import reactor.core.scheduler.Schedulers;
@SpringBootTest
@RunWith(SpringRunner.class)
public class ReactorApplicationTests {
@Test
public void publishOnThreadTest() {
Scheduler s = Schedulers.newParallel("parallel-scheduler", 4);
final Mono<String> mono = Mono.just("Publish on test: n")
.map(msg -> msg + "before: " + Thread.currentThread() )
.publishOn(s)
.map(msg -> msg + "nafter: " + Thread.currentThread());
new Thread(() -> mono.subscribe(System.out::println)).start();
}
}
And I cannot make it run, what am I doing wrong? With just subscribe it works but I wanted to look at thread used and play a bit with it.
java multithreading project-reactor reactor
1
1. "I cannot make it run" - what happens? 2. Add import statement so we could reproduce the behaviour.
– Alexei Kaigorodov
Jan 20 at 11:09
By I cannot run this i mean there is nothing printed as result. It works with above subscribe being outside of new Thread.
– ppysz
Jan 20 at 11:18
add a comment |
I'm testing how reactor works, created such code which is quite similar to what can be found in reactor documentation.
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Scheduler;
import reactor.core.scheduler.Schedulers;
@SpringBootTest
@RunWith(SpringRunner.class)
public class ReactorApplicationTests {
@Test
public void publishOnThreadTest() {
Scheduler s = Schedulers.newParallel("parallel-scheduler", 4);
final Mono<String> mono = Mono.just("Publish on test: n")
.map(msg -> msg + "before: " + Thread.currentThread() )
.publishOn(s)
.map(msg -> msg + "nafter: " + Thread.currentThread());
new Thread(() -> mono.subscribe(System.out::println)).start();
}
}
And I cannot make it run, what am I doing wrong? With just subscribe it works but I wanted to look at thread used and play a bit with it.
java multithreading project-reactor reactor
I'm testing how reactor works, created such code which is quite similar to what can be found in reactor documentation.
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Scheduler;
import reactor.core.scheduler.Schedulers;
@SpringBootTest
@RunWith(SpringRunner.class)
public class ReactorApplicationTests {
@Test
public void publishOnThreadTest() {
Scheduler s = Schedulers.newParallel("parallel-scheduler", 4);
final Mono<String> mono = Mono.just("Publish on test: n")
.map(msg -> msg + "before: " + Thread.currentThread() )
.publishOn(s)
.map(msg -> msg + "nafter: " + Thread.currentThread());
new Thread(() -> mono.subscribe(System.out::println)).start();
}
}
And I cannot make it run, what am I doing wrong? With just subscribe it works but I wanted to look at thread used and play a bit with it.
java multithreading project-reactor reactor
java multithreading project-reactor reactor
edited Jan 20 at 12:35
Gonzalo Matheu
3,36332132
3,36332132
asked Jan 20 at 9:35
ppyszppysz
133116
133116
1
1. "I cannot make it run" - what happens? 2. Add import statement so we could reproduce the behaviour.
– Alexei Kaigorodov
Jan 20 at 11:09
By I cannot run this i mean there is nothing printed as result. It works with above subscribe being outside of new Thread.
– ppysz
Jan 20 at 11:18
add a comment |
1
1. "I cannot make it run" - what happens? 2. Add import statement so we could reproduce the behaviour.
– Alexei Kaigorodov
Jan 20 at 11:09
By I cannot run this i mean there is nothing printed as result. It works with above subscribe being outside of new Thread.
– ppysz
Jan 20 at 11:18
1
1
1. "I cannot make it run" - what happens? 2. Add import statement so we could reproduce the behaviour.
– Alexei Kaigorodov
Jan 20 at 11:09
1. "I cannot make it run" - what happens? 2. Add import statement so we could reproduce the behaviour.
– Alexei Kaigorodov
Jan 20 at 11:09
By I cannot run this i mean there is nothing printed as result. It works with above subscribe being outside of new Thread.
– ppysz
Jan 20 at 11:18
By I cannot run this i mean there is nothing printed as result. It works with above subscribe being outside of new Thread.
– ppysz
Jan 20 at 11:18
add a comment |
1 Answer
1
active
oldest
votes
The reason your test program prints nothing is because it exits too early. It should wait until substriber's method is called:
@Test
public void publishOnThreadTest() throws InterruptedException {
Scheduler s = Schedulers.newParallel("parallel-scheduler", 4);
CountDownLatch latch = new CountDownLatch(1);
final Mono<String> mono = Mono.just("Publish on test: n")
.map(msg -> msg + "before: " + Thread.currentThread() )
.publishOn(s)
.map(msg -> msg + "nafter: " + Thread.currentThread());
new Thread(() -> mono.subscribe((String str) ->{
System.out.println(str);
latch.countDown();
})).start();
latch.await();
}
Thank you very much!
– ppysz
Jan 20 at 12:28
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54275155%2freactor-scheduler-with-anonymous-thread%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The reason your test program prints nothing is because it exits too early. It should wait until substriber's method is called:
@Test
public void publishOnThreadTest() throws InterruptedException {
Scheduler s = Schedulers.newParallel("parallel-scheduler", 4);
CountDownLatch latch = new CountDownLatch(1);
final Mono<String> mono = Mono.just("Publish on test: n")
.map(msg -> msg + "before: " + Thread.currentThread() )
.publishOn(s)
.map(msg -> msg + "nafter: " + Thread.currentThread());
new Thread(() -> mono.subscribe((String str) ->{
System.out.println(str);
latch.countDown();
})).start();
latch.await();
}
Thank you very much!
– ppysz
Jan 20 at 12:28
add a comment |
The reason your test program prints nothing is because it exits too early. It should wait until substriber's method is called:
@Test
public void publishOnThreadTest() throws InterruptedException {
Scheduler s = Schedulers.newParallel("parallel-scheduler", 4);
CountDownLatch latch = new CountDownLatch(1);
final Mono<String> mono = Mono.just("Publish on test: n")
.map(msg -> msg + "before: " + Thread.currentThread() )
.publishOn(s)
.map(msg -> msg + "nafter: " + Thread.currentThread());
new Thread(() -> mono.subscribe((String str) ->{
System.out.println(str);
latch.countDown();
})).start();
latch.await();
}
Thank you very much!
– ppysz
Jan 20 at 12:28
add a comment |
The reason your test program prints nothing is because it exits too early. It should wait until substriber's method is called:
@Test
public void publishOnThreadTest() throws InterruptedException {
Scheduler s = Schedulers.newParallel("parallel-scheduler", 4);
CountDownLatch latch = new CountDownLatch(1);
final Mono<String> mono = Mono.just("Publish on test: n")
.map(msg -> msg + "before: " + Thread.currentThread() )
.publishOn(s)
.map(msg -> msg + "nafter: " + Thread.currentThread());
new Thread(() -> mono.subscribe((String str) ->{
System.out.println(str);
latch.countDown();
})).start();
latch.await();
}
The reason your test program prints nothing is because it exits too early. It should wait until substriber's method is called:
@Test
public void publishOnThreadTest() throws InterruptedException {
Scheduler s = Schedulers.newParallel("parallel-scheduler", 4);
CountDownLatch latch = new CountDownLatch(1);
final Mono<String> mono = Mono.just("Publish on test: n")
.map(msg -> msg + "before: " + Thread.currentThread() )
.publishOn(s)
.map(msg -> msg + "nafter: " + Thread.currentThread());
new Thread(() -> mono.subscribe((String str) ->{
System.out.println(str);
latch.countDown();
})).start();
latch.await();
}
answered Jan 20 at 12:20
Alexei KaigorodovAlexei Kaigorodov
10.1k11029
10.1k11029
Thank you very much!
– ppysz
Jan 20 at 12:28
add a comment |
Thank you very much!
– ppysz
Jan 20 at 12:28
Thank you very much!
– ppysz
Jan 20 at 12:28
Thank you very much!
– ppysz
Jan 20 at 12:28
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54275155%2freactor-scheduler-with-anonymous-thread%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
1. "I cannot make it run" - what happens? 2. Add import statement so we could reproduce the behaviour.
– Alexei Kaigorodov
Jan 20 at 11:09
By I cannot run this i mean there is nothing printed as result. It works with above subscribe being outside of new Thread.
– ppysz
Jan 20 at 11:18