List of usage examples for java.nio.channels SelectionKey attach
public final Object attach(Object ob)
From source file:MainClass.java
public static void main(String[] args) throws IOException { for (int i = 0; i < data.length; i++) data[i] = (byte) i; ServerSocketChannel server = ServerSocketChannel.open(); server.configureBlocking(false);// w w w.j a v a 2 s. com server.socket().bind(new InetSocketAddress(9000)); Selector selector = Selector.open(); server.register(selector, SelectionKey.OP_ACCEPT); while (true) { selector.select(); Set readyKeys = selector.selectedKeys(); Iterator iterator = readyKeys.iterator(); while (iterator.hasNext()) { SelectionKey key = (SelectionKey) iterator.next(); iterator.remove(); if (key.isAcceptable()) { SocketChannel client = server.accept(); System.out.println("Accepted connection from " + client); client.configureBlocking(false); ByteBuffer source = ByteBuffer.wrap(data); SelectionKey key2 = client.register(selector, SelectionKey.OP_WRITE); key2.attach(source); } else if (key.isWritable()) { SocketChannel client = (SocketChannel) key.channel(); ByteBuffer output = (ByteBuffer) key.attachment(); if (!output.hasRemaining()) { output.rewind(); } client.write(output); } key.channel().close(); } } }
From source file:MainClass.java
public static void main(String[] args) throws IOException { Charset charset = Charset.forName("ISO-8859-1"); CharsetEncoder encoder = charset.newEncoder(); CharsetDecoder decoder = charset.newDecoder(); ByteBuffer buffer = ByteBuffer.allocate(512); Selector selector = Selector.open(); ServerSocketChannel server = ServerSocketChannel.open(); server.socket().bind(new java.net.InetSocketAddress(8000)); server.configureBlocking(false);//from ww w .j a v a 2 s .co m SelectionKey serverkey = server.register(selector, SelectionKey.OP_ACCEPT); for (;;) { selector.select(); Set keys = selector.selectedKeys(); for (Iterator i = keys.iterator(); i.hasNext();) { SelectionKey key = (SelectionKey) i.next(); i.remove(); if (key == serverkey) { if (key.isAcceptable()) { SocketChannel client = server.accept(); client.configureBlocking(false); SelectionKey clientkey = client.register(selector, SelectionKey.OP_READ); clientkey.attach(new Integer(0)); } } else { SocketChannel client = (SocketChannel) key.channel(); if (!key.isReadable()) continue; int bytesread = client.read(buffer); if (bytesread == -1) { key.cancel(); client.close(); continue; } buffer.flip(); String request = decoder.decode(buffer).toString(); buffer.clear(); if (request.trim().equals("quit")) { client.write(encoder.encode(CharBuffer.wrap("Bye."))); key.cancel(); client.close(); } else { int num = ((Integer) key.attachment()).intValue(); String response = num + ": " + request.toUpperCase(); client.write(encoder.encode(CharBuffer.wrap(response))); key.attach(new Integer(num + 1)); } } } } }
From source file:eu.stratosphere.nephele.taskmanager.bytebuffered.IncomingConnectionThread.java
private void doRead(SelectionKey key) { final IncomingConnection incomingConnection = (IncomingConnection) key.attachment(); try {/* w ww . j a v a 2s .co m*/ incomingConnection.read(); } catch (EOFException eof) { if (incomingConnection.isCloseUnexpected()) { final SocketChannel socketChannel = (SocketChannel) key.channel(); LOG.error("Connection from " + socketChannel.socket().getRemoteSocketAddress() + " was closed unexpectedly"); incomingConnection.reportTransmissionProblem(key, eof); } else { incomingConnection.closeConnection(key); } } catch (IOException ioe) { incomingConnection.reportTransmissionProblem(key, ioe); } catch (InterruptedException e) { // Nothing to do here } catch (NoBufferAvailableException e) { // There are no buffers available, unsubscribe from read event final SocketChannel socketChannel = (SocketChannel) key.channel(); try { final SelectionKey newKey = socketChannel.register(this.selector, 0); newKey.attach(incomingConnection); } catch (ClosedChannelException e1) { incomingConnection.reportTransmissionProblem(key, e1); } final BufferAvailabilityListener bal = new IncomingConnectionBufferAvailListener( this.pendingReadEventSubscribeRequests, key); if (!e.getBufferProvider().registerBufferAvailabilityListener(bal)) { // In the meantime, a buffer has become available again, subscribe to read event again try { final SelectionKey newKey = socketChannel.register(this.selector, SelectionKey.OP_READ); newKey.attach(incomingConnection); } catch (ClosedChannelException e1) { incomingConnection.reportTransmissionProblem(key, e1); } } } }
From source file:eu.stratosphere.nephele.taskmanager.bytebuffered.OutgoingConnectionThread.java
public void unsubscribeFromWriteEvent(SelectionKey selectionKey) throws IOException { final SocketChannel socketChannel = (SocketChannel) selectionKey.channel(); final OutgoingConnection outgoingConnection = (OutgoingConnection) selectionKey.attachment(); final SelectionKey newSelectionKey = socketChannel.register(this.selector, SelectionKey.OP_READ); newSelectionKey.attach(outgoingConnection); outgoingConnection.setSelectionKey(newSelectionKey); synchronized (this.connectionsToClose) { this.connectionsToClose.put(outgoingConnection, Long.valueOf(System.currentTimeMillis())); }//from w w w .j a v a 2s. co m }
From source file:eu.stratosphere.nephele.taskmanager.bytebuffered.IncomingConnectionThread.java
@Override public void run() { while (!this.isInterrupted()) { synchronized (this.pendingReadEventSubscribeRequests) { while (!this.pendingReadEventSubscribeRequests.isEmpty()) { final SelectionKey key = this.pendingReadEventSubscribeRequests.poll(); final IncomingConnection incomingConnection = (IncomingConnection) key.attachment(); final SocketChannel socketChannel = (SocketChannel) key.channel(); try { final SelectionKey newKey = socketChannel.register(this.selector, SelectionKey.OP_READ); newKey.attach(incomingConnection); } catch (ClosedChannelException e) { incomingConnection.reportTransmissionProblem(key, e); }//w w w . j a va 2 s . c om } } try { this.selector.select(500); } catch (IOException e) { LOG.error(e); } final Iterator<SelectionKey> iter = this.selector.selectedKeys().iterator(); while (iter.hasNext()) { final SelectionKey key = iter.next(); iter.remove(); if (key.isValid()) { if (key.isReadable()) { doRead(key); } else if (key.isAcceptable()) { doAccept(key); } else { LOG.error("Unknown key: " + key); } } else { LOG.error("Received invalid key: " + key); } } } // Do cleanup, if necessary if (this.listeningSocket != null) { try { this.listeningSocket.close(); } catch (IOException ioe) { // Actually, we can ignore this exception LOG.debug(ioe); } } // Finally, close the selector try { this.selector.close(); } catch (IOException ioe) { LOG.debug(StringUtils.stringifyException(ioe)); } }
From source file:eu.stratosphere.nephele.taskmanager.bytebuffered.IncomingConnectionThread.java
private void doAccept(SelectionKey key) { SocketChannel clientSocket = null; try {//from w w w. ja v a2 s .c o m clientSocket = this.listeningSocket.accept(); if (clientSocket == null) { LOG.error("Client socket is null"); return; } } catch (IOException ioe) { LOG.error(ioe); return; } final IncomingConnection incomingConnection = new IncomingConnection(this.byteBufferedChannelManager, clientSocket); SelectionKey clientKey = null; try { clientSocket.configureBlocking(false); clientKey = clientSocket.register(this.selector, SelectionKey.OP_READ); clientKey.attach(incomingConnection); } catch (IOException ioe) { incomingConnection.reportTransmissionProblem(clientKey, ioe); } }
From source file:eu.stratosphere.nephele.taskmanager.bytebuffered.OutgoingConnectionThread.java
private void doConnect(SelectionKey key) { final OutgoingConnection outgoingConnection = (OutgoingConnection) key.attachment(); final SocketChannel socketChannel = (SocketChannel) key.channel(); try {//from www . j av a2 s . c o m while (!socketChannel.finishConnect()) { try { Thread.sleep(100); } catch (InterruptedException e1) { LOG.error(e1); } } final SelectionKey channelKey = socketChannel.register(selector, SelectionKey.OP_WRITE | SelectionKey.OP_READ); outgoingConnection.setSelectionKey(channelKey); channelKey.attach(outgoingConnection); } catch (IOException ioe) { outgoingConnection.reportConnectionProblem(ioe); } }
From source file:oz.hadoop.yarn.api.net.ApplicationContainerServerImpl.java
/** * // ww w.j av a 2 s . c om */ void doWrite(SelectionKey selectionKey, ByteBuffer buffer) { ByteBuffer message = ByteBufferUtils.merge(ByteBuffer.allocate(4).putInt(buffer.limit() + 4), buffer); message.flip(); selectionKey.attach(message); selectionKey.interestOps(SelectionKey.OP_WRITE); }
From source file:eu.stratosphere.nephele.taskmanager.bytebuffered.OutgoingConnectionThread.java
@Override public void run() { while (!isInterrupted()) { synchronized (this.pendingConnectionRequests) { if (!this.pendingConnectionRequests.isEmpty()) { final OutgoingConnection outgoingConnection = this.pendingConnectionRequests.poll(); try { final SocketChannel socketChannel = SocketChannel.open(); socketChannel.configureBlocking(false); final SelectionKey key = socketChannel.register(this.selector, SelectionKey.OP_CONNECT); socketChannel.connect(outgoingConnection.getConnectionAddress()); key.attach(outgoingConnection); } catch (final IOException ioe) { // IOException is reported by separate thread to avoid deadlocks final Runnable reporterThread = new Runnable() { @Override public void run() { outgoingConnection.reportConnectionProblem(ioe); }// w w w. java 2 s . co m }; new Thread(reporterThread).start(); } } } synchronized (this.pendingWriteEventSubscribeRequests) { if (!this.pendingWriteEventSubscribeRequests.isEmpty()) { final SelectionKey oldSelectionKey = this.pendingWriteEventSubscribeRequests.poll(); final OutgoingConnection outgoingConnection = (OutgoingConnection) oldSelectionKey.attachment(); final SocketChannel socketChannel = (SocketChannel) oldSelectionKey.channel(); try { final SelectionKey newSelectionKey = socketChannel.register(this.selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE); newSelectionKey.attach(outgoingConnection); outgoingConnection.setSelectionKey(newSelectionKey); } catch (final IOException ioe) { // IOException is reported by separate thread to avoid deadlocks final Runnable reporterThread = new Runnable() { @Override public void run() { outgoingConnection.reportTransmissionProblem(ioe); } }; new Thread(reporterThread).start(); } } } synchronized (this.connectionsToClose) { final Iterator<Map.Entry<OutgoingConnection, Long>> closeIt = this.connectionsToClose.entrySet() .iterator(); final long now = System.currentTimeMillis(); while (closeIt.hasNext()) { final Map.Entry<OutgoingConnection, Long> entry = closeIt.next(); if ((entry.getValue().longValue() + MIN_IDLE_TIME_BEFORE_CLOSE) < now) { final OutgoingConnection outgoingConnection = entry.getKey(); closeIt.remove(); // Create new thread to close connection to avoid deadlocks final Runnable closeThread = new Runnable() { @Override public void run() { try { outgoingConnection.closeConnection(); } catch (IOException ioe) { outgoingConnection.reportTransmissionProblem(ioe); } } }; new Thread(closeThread).start(); } } } try { this.selector.select(10); } catch (IOException e) { LOG.error(e); } final Iterator<SelectionKey> iter = this.selector.selectedKeys().iterator(); while (iter.hasNext()) { final SelectionKey key = iter.next(); iter.remove(); if (key.isValid()) { if (key.isConnectable()) { doConnect(key); } else { if (key.isReadable()) { doRead(key); // A read will always result in an exception, so the write key will not be valid anymore continue; } if (key.isWritable()) { doWrite(key); } } } else { LOG.error("Received invalid key: " + key); } } } // Finally, try to close the selector try { this.selector.close(); } catch (IOException ioe) { LOG.debug(StringUtils.stringifyException(ioe)); } }
From source file:com.app.services.ExecutorServiceThread.java
public void run() { // create a selector that will by used for multiplexing. The selector // registers the socketserverchannel as // well as all socketchannels that are created String CLIENTCHANNELNAME = "clientChannel"; String SERVERCHANNELNAME = "serverChannel"; String channelType = "channelType"; ConcurrentHashMap<SelectionKey, Object> resultMap = new ConcurrentHashMap<SelectionKey, Object>(); ClassLoader classLoader = null; ByteArrayOutputStream bstr;//from w ww .j a va 2s. c o m ByteBuffer buffer = null; ByteBuffer lengthBuffer = ByteBuffer.allocate(4); int bytesRead; InputStream bais; ObjectInputStream ois; Object object; ExecutorServiceInfo executorServiceInfo = null; Random random = new Random(System.currentTimeMillis()); // register the serversocketchannel with the selector. The OP_ACCEPT // option marks // a selection key as ready when the channel accepts a new connection. // When the // socket server accepts a connection this key is added to the list of // selected keys of the selector. // when asked for the selected keys, this key is returned and hence we // know that a new connection has been accepted. try { Selector selector = Selector.open(); SelectionKey socketServerSelectionKey = serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); // SelectionKey socketServerSelectionKey1 = // channel.register(selector1, // SelectionKey.OP_ACCEPT); // set property in the key that identifies the channel Map<String, String> properties = new ConcurrentHashMap<String, String>(); properties.put(channelType, SERVERCHANNELNAME); socketServerSelectionKey.attach(properties); // wait for the selected keys SelectionKey key = null; Set<SelectionKey> selectedKeys; // logger.info("Instance Number"+instanceNumber); Iterator<SelectionKey> iterator = null; SocketChannel clientChannel = null; while (true) { try { // the select method is a blocking method which returns when // atleast // one of the registered // channel is selected. In this example, when the socket // accepts // a // new connection, this method // will return. Once a socketclient is added to the list of // registered channels, then this method // would also return when one of the clients has data to be // read // or // written. It is also possible to perform a nonblocking // select // using the selectNow() function. // We can also specify the maximum time for which a select // function // can be blocked using the select(long timeout) function. if (selector.select() >= 0) { selectedKeys = selector.selectedKeys(); iterator = selectedKeys.iterator(); } while (iterator.hasNext()) { try { key = iterator.next(); // the selection key could either by the // socketserver // informing // that a new connection has been made, or // a socket client that is ready for read/write // we use the properties object attached to the // channel // to // find // out the type of channel. if (((Map) key.attachment()).get(channelType).equals(SERVERCHANNELNAME)) { // a new connection has been obtained. This // channel // is // therefore a socket server. ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel(); // accept the new connection on the server // socket. // Since // the // server socket channel is marked as non // blocking // this channel will return null if no client is // connected. SocketChannel clientSocketChannel = serverSocketChannel.accept(); if (clientSocketChannel != null) { // set the client connection to be non // blocking clientSocketChannel.configureBlocking(false); SelectionKey clientKey = clientSocketChannel.register(selector, SelectionKey.OP_READ, SelectionKey.OP_WRITE); Map<String, String> clientproperties = new ConcurrentHashMap<String, String>(); clientproperties.put(channelType, CLIENTCHANNELNAME); clientKey.attach(clientproperties); clientKey.interestOps(SelectionKey.OP_READ); // clientSocketChannel.close(); // write something to the new created client /* * CharBuffer buffer = * CharBuffer.wrap("Hello client"); while * (buffer.hasRemaining()) { * clientSocketChannel.write * (Charset.defaultCharset() * .encode(buffer)); } * clientSocketChannel.close(); * buffer.clear(); */ } } else { // data is available for read // buffer for reading clientChannel = (SocketChannel) key.channel(); if (key.isReadable()) { // the channel is non blocking so keep it // open // till // the // count is >=0 clientChannel = (SocketChannel) key.channel(); if (resultMap.get(key) == null) { //log.info(key); bstr = new ByteArrayOutputStream(); object = null; clientChannel.read(lengthBuffer); int length = lengthBuffer.getInt(0); lengthBuffer.clear(); //log.info(length); buffer = ByteBuffer.allocate(length); if ((bytesRead = clientChannel.read(buffer)) > 0) { // buffer.flip(); // System.out // .println(bytesRead); bstr.write(buffer.array(), 0, bytesRead); buffer.clear(); } buffer.clear(); //log.info("Message1"+new String(bstr // .toByteArray())); bais = new ByteArrayInputStream(bstr.toByteArray()); ois = new ObjectInputStream(bais); // Offending // line. // Produces // the // StreamCorruptedException. //log.info("In read obect"); object = ois.readObject(); //log.info("Class Cast"); //log.info("Class Cast1"); ois.close(); byte[] params = bstr.toByteArray(); bstr.close(); //log.info("readObject"); //log.info("After readObject"); if (object instanceof CloseSocket) { resultMap.remove(key); clientChannel.close(); key.cancel(); } // clientChannel.close(); String serviceurl = (String) object; String[] serviceRegistry = serviceurl.split("/"); //log.info("classLoaderMap" // + urlClassLoaderMap); //log.info(deployDirectory // + "/" + serviceRegistry[0]); int servicenameIndex; //log.info(earServicesDirectory // + "/" + serviceRegistry[0] // + "/" + serviceRegistry[1]); if (serviceRegistry[0].endsWith(".ear")) { classLoader = (VFSClassLoader) urlClassLoaderMap.get(deployDirectory + "/" + serviceRegistry[0] + "/" + serviceRegistry[1]); servicenameIndex = 2; } else if (serviceRegistry[0].endsWith(".jar")) { classLoader = (WebClassLoader) urlClassLoaderMap .get(deployDirectory + "/" + serviceRegistry[0]); servicenameIndex = 1; } else { classLoader = (WebClassLoader) urlClassLoaderMap .get(deployDirectory + "/" + serviceRegistry[0]); servicenameIndex = 1; } String serviceName = serviceRegistry[servicenameIndex]; // log.info("servicename:"+serviceName);; synchronized (executorServiceMap) { executorServiceInfo = (ExecutorServiceInfo) executorServiceMap .get(serviceName.trim()); } ExecutorServiceInfoClassLoader classLoaderExecutorServiceInfo = new ExecutorServiceInfoClassLoader(); classLoaderExecutorServiceInfo.setClassLoader(classLoader); classLoaderExecutorServiceInfo.setExecutorServiceInfo(executorServiceInfo); resultMap.put(key, classLoaderExecutorServiceInfo); // key.interestOps(SelectionKey.OP_READ); // log.info("Key interested Ops"); // continue; } //Thread.sleep(100); /* * if (classLoader == null) throw new * Exception( * "Could able to obtain deployed class loader" * ); */ /* * log.info( * "current context classloader" + * classLoader); */ //log.info("In rad object"); bstr = new ByteArrayOutputStream(); lengthBuffer.clear(); int numberofDataRead = clientChannel.read(lengthBuffer); //log.info("numberofDataRead" // + numberofDataRead); int length = lengthBuffer.getInt(0); if (length <= 0) { iterator.remove(); continue; } lengthBuffer.clear(); //log.info(length); buffer = ByteBuffer.allocate(length); buffer.clear(); if ((bytesRead = clientChannel.read(buffer)) > 0) { // buffer.flip(); // System.out // .println(bytesRead); bstr.write(buffer.array(), 0, bytesRead); buffer.clear(); } if (bytesRead <= 0 || bytesRead < length) { //log.info("bytesRead<length"); iterator.remove(); continue; } //log.info(new String(bstr // .toByteArray())); bais = new ByteArrayInputStream(bstr.toByteArray()); ExecutorServiceInfoClassLoader classLoaderExecutorServiceInfo = (ExecutorServiceInfoClassLoader) resultMap .get(key); ois = new ClassLoaderObjectInputStream( (ClassLoader) classLoaderExecutorServiceInfo.getClassLoader(), bais); // Offending // line. // Produces // the // StreamCorruptedException. object = ois.readObject(); ois.close(); bstr.close(); executorServiceInfo = classLoaderExecutorServiceInfo.getExecutorServiceInfo(); //System.out // .println("inputStream Read Object"); //log.info("Object=" // + object.getClass()); // Thread.currentThread().setContextClassLoader(currentContextLoader); if (object instanceof ExecutorParams) { ExecutorParams exeParams = (ExecutorParams) object; Object returnValue = null; //log.info("test socket1"); String ataKey; ATAConfig ataConfig; ConcurrentHashMap ataServicesMap; Enumeration<NodeResourceInfo> noderesourceInfos = addressmap.elements(); NodeResourceInfo noderesourceinfo = null; String ip = ""; int port = 1000; long memavailable = 0; long memcurr = 0; if (noderesourceInfos.hasMoreElements()) { noderesourceinfo = noderesourceInfos.nextElement(); if (noderesourceinfo.getMax() != null) { ip = noderesourceinfo.getHost(); port = Integer.parseInt(noderesourceinfo.getPort()); memavailable = Long.parseLong(noderesourceinfo.getMax()) - Long.parseLong(noderesourceinfo.getUsed()); ; } } while (noderesourceInfos.hasMoreElements()) { noderesourceinfo = noderesourceInfos.nextElement(); if (noderesourceinfo.getMax() != null) { memcurr = Long.parseLong(noderesourceinfo.getMax()) - Long.parseLong(noderesourceinfo.getUsed()); if (memavailable <= memcurr) { ip = noderesourceinfo.getHost(); port = Integer.parseInt(noderesourceinfo.getPort()); memavailable = memcurr; } } } ATAExecutorServiceInfo servicesAvailable; Socket sock1 = new Socket(ip, port); OutputStream outputStr = sock1.getOutputStream(); ObjectOutputStream objOutputStream = new ObjectOutputStream(outputStr); NodeInfo nodeInfo = new NodeInfo(); nodeInfo.setClassNameWithPackage( executorServiceInfo.getExecutorServicesClass().getName()); nodeInfo.setMethodName(executorServiceInfo.getMethod().getName()); nodeInfo.setWebclassLoaderURLS(((WebClassLoader) classLoader).geturlS()); NodeInfoMethodParam nodeInfoMethodParam = new NodeInfoMethodParam(); nodeInfoMethodParam.setMethodParams(exeParams.getParams()); nodeInfoMethodParam .setMethodParamTypes(executorServiceInfo.getMethodParams()); //log.info("Serializable socket="+sock); //nodeInfo.setSock(sock); //nodeInfo.setOstream(sock.getOutputStream()); objOutputStream.writeObject(nodeInfo); objOutputStream = new ObjectOutputStream(outputStr); objOutputStream.writeObject(nodeInfoMethodParam); ObjectInputStream objInputStream1 = new ObjectInputStream( sock1.getInputStream()); returnValue = objInputStream1.readObject(); objOutputStream.close(); objInputStream1.close(); sock1.close(); /*returnValue = executorServiceInfo .getMethod() .invoke(executorServiceInfo .getExecutorServicesClass() .newInstance(), exeParams.getParams());*/ // Thread.currentThread().setContextClassLoader(oldCL); // log.info("Written Value=" // + returnValue.toString()); resultMap.put(key, returnValue); } key.interestOps(SelectionKey.OP_WRITE); //log.info("Key interested Ops1"); } else if (key.isWritable()) { // the channel is non blocking so keep it // open // till the // count is >=0 //log.info("In write"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); // make // a // BAOS // stream ObjectOutputStream oos = new ObjectOutputStream(baos); // wrap and OOS around the // stream Object result = resultMap.get(key); oos.writeObject(result); // write an object // to // the stream oos.flush(); oos.close(); byte[] objData = baos.toByteArray(); // get // the // byte // array baos.close(); buffer = ByteBuffer.wrap(objData); // wrap // around // the // data buffer.rewind(); // buffer.flip(); //prep for writing //log.info(new String(objData)); //while (buffer.hasRemaining()) clientChannel.write(buffer); // write resultMap.remove(key); buffer.clear(); key.cancel(); clientChannel.close(); //log.info("In write1"); numberOfServicesRequests++; //log.info("Key interested Ops2"); } } iterator.remove(); } catch (Exception ex) { log.error("Error in executing the executor services thread", ex); //ex.printStackTrace(); key.cancel(); clientChannel.close(); resultMap.remove(key); //ex.printStackTrace(); } } } catch (Exception ex) { log.error("Error in executing the executor services thread", ex); //ex.printStackTrace(); } } } catch (Exception ex) { log.error("Error in executing the executor services thread", ex); } }