问题描述
我应该在netty4中将业务逻辑放在哪里?
where should I put business logic in netty4?
我应该将代码放入@Override channelRead0()
还是在其中运行任务?我知道这两者都会在同一个IO eventLoop中运行,但是它们之间有什么区别?哪个更好?
Should I just put my code in @Override channelRead0()
or run a task in it?I know both of that will run into the same IO eventLoop, but what's the differences between them? which one is better?
解决方案1:
@Override
channelRead0(){
business code;
}
解决方案2:
@Override
channelRead0(){
ctx.executor().submit(new task(){business code});
//or ctx.channel().eventLoop().submit(new task(){business code});
}
推荐答案
只要您不使用任何阻塞操作(例如,从DB读取),channelRead0
就可以了.通常,由您决定. Submit
方法在需要执行异步操作或定时事件的情况下更可能出现.对于阻止事件,最好创建单独的执行程序.
As long as you don't use any blocking operations (read from DB for example) channelRead0
would be totally fine. In general it is up to you to decide. Submit
method is more likely for cases when you need to execute asynchronous operation or timed event. For blocking events it is better to create separate executor.
这篇关于我应该在netty4的哪里放置业务逻辑?在chanelRead0()或运行任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!