标签:
1:引入jar包,这里使用的是maven,只需要引用一个jar包就行了
<DEPENDENCY>
<GROUPID>org.projectreactor</GROUPID>
reactor-spring</ARTIFACTID>
<VERSION>1.0.0.RELEASE</VERSION>
</DEPENDENCY>
2:写一个reactor的配置的bean
@Configuration
@EnableReactor
public class ReactorConfig {
@Bean(name = "rootReactor")
public Reactor rootReactor(Environment env) {
return Reactors.reactor()。env(env)。get();
}
@Bean(name = "reportReactor")
public Reactor reportReactor(Environment env) {
return Reactors.reactor()。env(env)。get();
}
}
3:事件的处理类,一般是以Hander结尾,方便区分:
@Component
public class IndexHandler {
@Autowired
@Qualifier("rootReactor")
private Reactor reactor;
@Selector(value = "hello", reactor = "@rootReactor")
public void handleTestTopic(Event<STRING> evt) throws Exception {
System.out.println("************");
}
}
4:最后就是在controller或者service里面通知新开线程了:
@Controller
public class IndexController {
@Autowired
@Qualifier("rootReactor")
private Reactor r;
@RequestMapping("chen")
@Transactional
public void chen() {
r.notify("hello", Event.wrap("你好"));
}
}
---------------------------------------------------------------------------------------
Environment env = new Environment(); Reactor reactor = Reactors.reactor() .env(env) .dispatcher(RING_BUFFER) .get(); reactor.on($(“topic”), (Event<String> ev) → { System.out.println(“Hello “ + ev.getData()); }); reactor.notify(“topic”, Event.wrap(“John Doe”)); |
Stream<String> str; str.map(String::toUpperCase) .filter(new Predicate<String>() { public boolean test(String s) { … } }) .consume(s → log.info(“consumed string {}”, s)); |
Promise<String> p; String s = p .onSuccess(s → log.info(“consumed string {}”, s)) .onFailure(t → log.error(t.getMessage(), t)) .onComplete(t → log.info(“complete”)) .await(5, SECONDS); p.map(String::toUpperCase).consume(s → log.info(“UC: {}”, s)); |
Processor<Buffer> proc; Operation<Buffer> op = proc.prepare(); op.get().append(data).flip(); op.commit(); proc.batch(512, buff → buff.append(data).flip()); |
@Configuration @EnableReactor public class ReactorConfiguration { @Bean public Reactor input(Environment env) { return Reactors.reactor().env(env) .dispatcher(RING_BUFFER).get(); } @Bean public Reactor output(Environment env) { return Reactors.reactor().env(env) .dispatcher(RING_BUFFER).get(); } |
@Component public class SimpleHandler { @Autowired private Reactor reactor; @Selector(“test.topic”) public void onTestTopic(String s) { // Handle data } } |
@CompileStatic def welcome(){ reactor.on(‘greetings‘) { String s -> reply “hello $s” reply “how are you?” } reactor.notify ‘greetings‘, ‘Jon‘ reactor.send(‘greetings‘, ‘Stephane‘){ println it cancel() } } |
标签:
原文地址:http://my.oschina.net/u/2273085/blog/505422