2010-09-16 4 views
12

पर सॉकेट टाइमआउट सेट करना मेरे पास नेटटी चैनल है और मैं अंतर्निहित सॉकेट पर एक टाइमआउट सेट करना चाहता हूं (यह डिफ़ॉल्ट रूप से 0 पर सेट है)।नेटटी चैनल

टाइमआउट का उद्देश्य यह है कि उदाहरण के लिए 15 मिनट के लिए कुछ भी नहीं होने पर उपयोग नहीं किया गया चैनल बंद हो जाएगा।

हालांकि मुझे ऐसा करने के लिए कोई कॉन्फ़िगरेशन नहीं दिखाई देता है, और सॉकेट स्वयं भी मुझसे छिपा हुआ है।

धन्यवाद

उत्तर

13

ReadTimeoutHandler वर्ग प्रयोग किया जाता है, तो टाइम-आउट से नियंत्रित किया जा सकता है।

Javadoc से उद्धरण निम्नलिखित है।

public class MyPipelineFactory implements ChannelPipelineFactory { 
    private final Timer timer; 
    public MyPipelineFactory(Timer timer) { 
     this.timer = timer; 
    } 

    public ChannelPipeline getPipeline() { 
     // An example configuration that implements 30-second read timeout: 
     return Channels.pipeline(
      new ReadTimeoutHandler(timer, 30), // timer must be shared. 
      new MyHandler()); 
    } 
} 


ServerBootstrap bootstrap = ...; 
Timer timer = new HashedWheelTimer(); 
... 
bootstrap.setPipelineFactory(new MyPipelineFactory(timer)); 
... 

जब यह एक टाइम-आउट का कारण होगा, MyHandler.exceptionCaught (ChannelHandlerContext ctx, ExceptionEvent ई) ReadTimeoutException साथ कहा जाता है।

@Override 
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) { 
    if (e.getCause() instanceof ReadTimeoutException) { 
     // NOP 
    } 
    ctx.getChannel().close(); 
}