1 /* 2 Copyright 2010 Ramon Servadei 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 package mirrormap.nio; 17 18 import java.nio.channels.Channel; 19 import java.nio.channels.SelectableChannel; 20 import java.nio.channels.SelectionKey; 21 22 import mirrormap.lifecycle.IDestroyable; 23 24 /** 25 * A task that is associated with a {@link SelectionKey}. This task is 26 * registered with the {@link SelectorTasks}. Upon registration, this receives 27 * the selection key via the {@link #setSelectionKey(SelectionKey)} method. 28 * <p> 29 * When the registered NIO {@link Channel} has an operation ready for 30 * processing, the {@link #run()} method of this will be executed by the thread 31 * running the {@link SelectorTasks#process()} method. 32 * <p> 33 * Implementations must be efficient; any thread blocking will prevent other 34 * {@link Channel} ready operations from being processed by the 35 * {@link SelectorTasks#process()}. 36 * 37 * @author Ramon Servadei 38 */ 39 public interface ISelectionKeyTask extends IDestroyable 40 { 41 /** 42 * Run the task associated with the operation and selection key 43 */ 44 public void run(); 45 46 /** 47 * Receives the selection key returned from the 48 * {@link SelectableChannel#register(java.nio.channels.Selector, int)} 49 * method called during the 50 * {@link SelectorTasks#register(int, SelectableChannel, ISelectionKeyTask)} 51 * method. This is the key associated with this task. 52 * 53 * @param selectionKey 54 * the selection key associated with this task 55 */ 56 public void setSelectionKey(SelectionKey selectionKey); 57 }