왜 내 읽기 시간이 작동하지 않는지 알 수 없습니다. 내가하고 싶은 일은 BlockedQueue<String>
에 메시지를 넣으려면 어떤 스레드가 10 초 동안
을 대기하고 시간 초과가되면 클라이언트에서 응답을 반환하는 것입니다.ReadTimeoutHandler 처리 시간 제한
public class NioAsynChatPipelineFactory implements ChannelPipelineFactory {
private static Timer timer = new HashedWheelTimer();
private final ChannelHandler timeoutHandler = new ReadTimeoutHandler(timer, 10);
@Override
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("decoder", new HttpRequestDecoder());
pipeline.addLast("encoder", new HttpResponseEncoder());
pipeline.addLast("handler", new NioAsynChatHandler());
pipeline.addLast("timeout", this.timeoutHandler);
return pipeline;
}
}
이제 내 처리기는 다음과 같습니다.
public class NioAsynChatHandler extends SimpleChannelUpstreamHandler{
@Override
public void handleUpstream(
ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
super.handleUpstream(ctx, e);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
throws Exception {
System.out.println("Exception");
\\writing some kind of response and closing channel.
}
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
Thread thread = new Thread(new ConsmerTask(e.getChannel()));
thread.start();
}
및 ConsumerTask 내부에 난 그냥 응답을
public class ConsumerTask implements Runnable{
private Channel channel;
public ConsumerTask(Channel channel){
this.channel = channel;
}
@Override
public void run() {
try{
while(true){
String message = queue.take();
}
} catch(InterruptedException ex){
Thread.currentThread.interrupt();
} finally{
//write something to channel and close it
}
}
내 문제를 얻을 수 BlockingQueue를 기다리고 있어요 내가 어떤 excpetion은 아웃 시간에 발생하는 것을 볼 수 없다는 것입니다. 내가 뭘 잘못하고 있니?
업데이트 :
public static final BlockingQueue<String> blockingQueue = new LinkedBlockingQueue<String>();
사실은 내 질문이 외부 스레드에 뭔가를 기다리는 동안, 어떻게 제한 시간에 채널을 닫으려면 더 일반적인입니까?
업데이트 2 : 또 다른 질문 : 때문에 나는 파이프 라인의 OrderedMemoryAwareThreadPoolExecutor
를 사용하는 것이 더 좋을 것이다 차 외부 스레드를 실행하는거야 사실로? 성능이 향상됩니까?
테이크 대신 poll()을 사용하지 않아야합니까? BlockingQueue에 대해 어떤 구현을 선택하셨습니까? –
사실, poll() 또는 take()를 사용하든 관계 없습니다. 설문 조사에서 take()가 항목을 즉시 얻는 동안 시간 제한을 지정하고 while (true) 루프는 필요하지 않습니다. 차단 대기열에 관해서는 내 업데이트를 참조하십시오. –