org.apache.mina.core.future.ReadFuture Java Examples
The following examples show how to use
org.apache.mina.core.future.ReadFuture.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractIoSession.java From neoscada with Eclipse Public License 1.0 | 6 votes |
/** * {@inheritDoc} */ public final ReadFuture read() { if (!getConfig().isUseReadOperation()) { throw new IllegalStateException("useReadOperation is not enabled."); } Queue<ReadFuture> readyReadFutures = getReadyReadFutures(); ReadFuture future; synchronized (readyReadFutures) { future = readyReadFutures.poll(); if (future != null) { if (future.isClosed()) { // Let other readers get notified. readyReadFutures.offer(future); } } else { future = new DefaultReadFuture(this); getWaitingReadFutures().offer(future); } } return future; }
Example #2
Source File: MinaUtil.java From seed with Apache License 2.0 | 5 votes |
/** * 发送TCP消息 * <p> * 当通信发生异常时(Fail to get session...),返回“MINA_SERVER_ERROR”字符串 * </p> * @param message 待发送报文的中文字符串形式 * @param ipAddress 远程主机的IP地址 * @param port 远程主机的端口号 * @param charset 该方法与远程主机间通信报文为编码字符集(编码为byte[]发送到Server) * @return 远程主机响应报文的字符串形式 */ public static String sendTCPMessage(String message, String ipAddress, int port, String charset){ IoConnector connector = new NioSocketConnector(); connector.setConnectTimeoutMillis(DEFAULT_CONNECTION_TIMEOUT); //同步的客户端,必须设置此项,其默认为false connector.getSessionConfig().setUseReadOperation(true); connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ClientProtocolEncoder(charset), new ClientProtocolDecode(charset))); //作为同步的客户端,可以不需要IoHandler,Mina会自动添加一个默认的IoHandler实现(即AbstractIoConnector) //connector.setHandler(this); IoSession session = null; Object respData = null; try{ ConnectFuture connectFuture = connector.connect(new InetSocketAddress(ipAddress, port)); connectFuture.awaitUninterruptibly(); //等待连接成功,相当于将异步执行转为同步执行 session = connectFuture.getSession(); //获取连接成功后的会话对象 session.write(message).awaitUninterruptibly(); //由于上面已经设置setUseReadOperation(true),故IoSession.read()方法才可用 ReadFuture readFuture = session.read(); //因其内部使用BlockingQueue,故Server端用之可能会内存泄漏,但Client端可适当用之 //Wait until the message is received if(readFuture.awaitUninterruptibly(DEFAULT_BOTHIDLE_TIMEOUT, TimeUnit.SECONDS)){ //Get the received message respData = readFuture.getMessage(); }else{ LogUtil.getLogger().warn("读取[/" + ipAddress + ":" + port + "]超时"); } }catch(Exception e){ LogUtil.getLogger().error("请求通信[/" + ipAddress + ":" + port + "]偶遇异常,堆栈轨迹如下", e); }finally{ if(session != null){ //关闭IoSession,该操作是异步的,true为立即关闭,false为所有写操作都flush后关闭 //这里仅仅是关闭了TCP的连接通道,并未关闭Client端程序 //session.close(true); session.closeNow(); //客户端发起连接时,会请求系统分配相关的文件句柄,而在连接失败时记得释放资源,否则会造成文件句柄泄露 //当总的文件句柄数超过系统设置值时[ulimit -n],则抛异常"java.io.IOException: Too many open files",导致新连接无法创建,服务器挂掉 //所以:若不关闭的话,其运行一段时间后可能抛出too many open files异常,导致无法连接 session.getService().dispose(); } } return respData==null ? "MINA_SERVER_ERROR" : respData.toString(); }
Example #3
Source File: AbstractIoSession.java From neoscada with Eclipse Public License 1.0 | 5 votes |
/** * TODO Add method documentation */ public final void offerClosedReadFuture() { Queue<ReadFuture> readyReadFutures = getReadyReadFutures(); synchronized (readyReadFutures) { newReadFuture().setClosed(); } }
Example #4
Source File: AbstractIoSession.java From neoscada with Eclipse Public License 1.0 | 5 votes |
/** * TODO Add method documentation */ private ReadFuture newReadFuture() { Queue<ReadFuture> readyReadFutures = getReadyReadFutures(); Queue<ReadFuture> waitingReadFutures = getWaitingReadFutures(); ReadFuture future; synchronized (readyReadFutures) { future = waitingReadFutures.poll(); if (future == null) { future = new DefaultReadFuture(this); readyReadFutures.offer(future); } } return future; }
Example #5
Source File: AbstractIoSession.java From neoscada with Eclipse Public License 1.0 | 5 votes |
/** * TODO Add method documentation */ private Queue<ReadFuture> getReadyReadFutures() { Queue<ReadFuture> readyReadFutures = (Queue<ReadFuture>) getAttribute(READY_READ_FUTURES_KEY); if (readyReadFutures == null) { readyReadFutures = new ConcurrentLinkedQueue<ReadFuture>(); Queue<ReadFuture> oldReadyReadFutures = (Queue<ReadFuture>) setAttributeIfAbsent(READY_READ_FUTURES_KEY, readyReadFutures); if (oldReadyReadFutures != null) { readyReadFutures = oldReadyReadFutures; } } return readyReadFutures; }
Example #6
Source File: AbstractIoSession.java From neoscada with Eclipse Public License 1.0 | 5 votes |
/** * TODO Add method documentation */ private Queue<ReadFuture> getWaitingReadFutures() { Queue<ReadFuture> waitingReadyReadFutures = (Queue<ReadFuture>) getAttribute(WAITING_READ_FUTURES_KEY); if (waitingReadyReadFutures == null) { waitingReadyReadFutures = new ConcurrentLinkedQueue<ReadFuture>(); Queue<ReadFuture> oldWaitingReadyReadFutures = (Queue<ReadFuture>) setAttributeIfAbsent( WAITING_READ_FUTURES_KEY, waitingReadyReadFutures); if (oldWaitingReadyReadFutures != null) { waitingReadyReadFutures = oldWaitingReadyReadFutures; } } return waitingReadyReadFutures; }
Example #7
Source File: MockIOSession.java From mapleLemon with GNU General Public License v2.0 | 4 votes |
@Override public ReadFuture read() { return null; }
Example #8
Source File: WebSocketServerTest.java From red5-websocket with Apache License 2.0 | 4 votes |
@Override public ReadFuture read() { // TODO Auto-generated method stub return null; }
Example #9
Source File: IoSession.java From neoscada with Eclipse Public License 1.0 | 2 votes |
/** * TODO This javadoc is wrong. The return tag should be short. * * @return a {@link ReadFuture} which is notified when a new message is * received, the connection is closed or an exception is caught. This * operation is especially useful when you implement a client application. * TODO : Describe here how we enable this feature. * However, please note that this operation is disabled by default and * throw {@link IllegalStateException} because all received events must be * queued somewhere to support this operation, possibly leading to memory * leak. This means you have to keep calling {@link #read()} once you * enabled this operation. To enable this operation, please call * {@link IoSessionConfig#setUseReadOperation(boolean)} with <tt>true</tt>. * * @throws IllegalStateException if * {@link IoSessionConfig#setUseReadOperation(boolean) useReadOperation} * option has not been enabled. */ ReadFuture read();