最近工作中用到一个非常好用的框架spring reactor,可以帮助我们新开一个异步的线程来处理一些比如记录日志的功能,这样就能节约后台相应的时间,之前在网上也没有找到完全能用的列子,这里记录一下,方便以后用,配置也非常的简单:
1:引入jar包,这里使用的是maven,只需要引用一个jar包就行了
org.projectreactor
reactor-spring
1.0.0.RELEASE
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 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("你好"));
}
}
大功告成,非常的简单