當前位置: 首頁>>代碼示例>>Java>>正文


Java ConnectFuture.addListener方法代碼示例

本文整理匯總了Java中org.apache.mina.core.future.ConnectFuture.addListener方法的典型用法代碼示例。如果您正苦於以下問題:Java ConnectFuture.addListener方法的具體用法?Java ConnectFuture.addListener怎麽用?Java ConnectFuture.addListener使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.apache.mina.core.future.ConnectFuture的用法示例。


在下文中一共展示了ConnectFuture.addListener方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: retryAttemptListener

import org.apache.mina.core.future.ConnectFuture; //導入方法依賴的package包/類
/**
 * Handles the retry attempts. Listens to see when the ConnectFuture is finished and checks if there was 
 * an exception thrown. If so, it then attempts a retry if there are more retries.
 * 
 * @param connector
 * @param detectFuture
 * @param address
 * @param retryAttempt
 * @return IoFutureListener<ConnectFuture>
 */
private final IoFutureListener<ConnectFuture> retryAttemptListener(final DetectFutureMinaImpl detectFuture, final InetSocketAddress address, final IoSessionInitializer<ConnectFuture> init, final int retryAttempt) {
    return new IoFutureListener<ConnectFuture>() {

        @Override
        public void operationComplete(ConnectFuture future) {
            final Throwable cause = future.getException();
           
            if (cause instanceof IOException) {
                if(retryAttempt == 0) {
                    LogUtils.infof(this, "Service %s detected false: %s: %s",getServiceName(), cause.getClass().getName(), cause.getMessage());
                    detectFuture.setServiceDetected(false);
                }else {
                    LogUtils.infof(this, "Connection exception occurred: %s for service %s, retrying attempt %d", cause, getServiceName(), retryAttempt);
                    future = m_connectionFactory.reConnect(address, init, createDetectorHandler(detectFuture));
                    future.addListener(retryAttemptListener(detectFuture, address, init, retryAttempt - 1));
                }
            }else if(cause instanceof Throwable) {
                LogUtils.infof(this, cause, "Threw a Throwable and detection is false for service %s", getServiceName());
                detectFuture.setServiceDetected(false);
            }
        }
        
    };
}
 
開發者ID:qoswork,項目名稱:opennmszh,代碼行數:35,代碼來源:AsyncBasicDetectorMinaImpl.java

示例2: connect

import org.apache.mina.core.future.ConnectFuture; //導入方法依賴的package包/類
/**
 * <p>Connect to a remote socket. If org.opennms.netmgt.provision.maxConcurrentConnections
 * is set, this may block until a connection slot is available.</p>
 * 
 * <p>You must dispose both the {@link ConnectionFactoryNewConnectorImpl} and {@link ConnectFuture} when done
 * by calling {@link #dispose(ConnectionFactoryNewConnectorImpl, ConnectFuture)}.</p>
 * 
 * @param remoteAddress
 * 		Destination address
 * @param init
 * 		Initialiser for the IoSession
 * @return
 * 		ConnectFuture from a Mina connect call
 */
@Override
public ConnectFuture connect(SocketAddress remoteAddress, IoSessionInitializer<? extends ConnectFuture> init, IoHandler handler) {
    SocketConnector connector = getSocketConnector(getTimeout(), handler);
    InetSocketAddress localAddress = null;
    synchronized (m_portMutex) {
        if (m_port.get() == null) {
            // Fetch a new ephemeral port
            localAddress = new InetSocketAddress(0);
            m_port.set(localAddress.getPort());
        } else {
            localAddress = new InetSocketAddress(m_port.get());
        }
    }
    final ConnectFuture cf = connector.connect(remoteAddress, localAddress, init);
    cf.addListener(portSwitcher(connector, remoteAddress, init, handler));
    cf.addListener(connectorDisposer(connector));
    return cf;
}
 
開發者ID:vishwaabhinav,項目名稱:OpenNMS,代碼行數:33,代碼來源:ConnectionFactoryNewConnectorImpl.java

示例3: connect

import org.apache.mina.core.future.ConnectFuture; //導入方法依賴的package包/類
@Override
public synchronized void connect ()
{
    if ( isConnected () )
    {
        logger.info ( "Already connected" );
        return;
    }

    if ( this.connector == null )
    {
        this.connector = new NioSocketConnector ();
        this.connector.setHandler ( this );
        if ( Boolean.getBoolean ( "org.eclipse.scada.da.server.io.common.trace" ) )
        {
            this.connector.getFilterChain ().addLast ( "logger", new LoggingFilter () );
        }
        setupConnector ( this.connector );
    }

    final ConnectFuture cu = this.connector.connect ( this.address );
    cu.addListener ( new IoFutureListener<ConnectFuture> () {

        @Override
        public void operationComplete ( final ConnectFuture future )
        {
            try
            {
                future.getSession ();
            }
            catch ( final Throwable e )
            {
                StreamBaseDevice.this.fireConnectionFailed ( e );
            }
        }
    } );
}
 
開發者ID:eclipse,項目名稱:neoscada,代碼行數:38,代碼來源:StreamBaseDevice.java

示例4: messageReceived

import org.apache.mina.core.future.ConnectFuture; //導入方法依賴的package包/類
@Override
public void messageReceived(IoSession session, Object message) throws Exception {
    TftpPacket packet = (TftpPacket) message;

    SocketAddress address = session.getRemoteAddress();
    LOG.info("Address is " + address);

    switch (packet.getOpcode()) {
        case RRQ: {
            TftpRequestPacket request = (TftpRequestPacket) packet;
            TftpData source = provider.open(request.getFilename());
            if (source == null) {
                session.write(new TftpErrorPacket(address, TftpErrorCode.FILE_NOT_FOUND), address);
                session.close(false);
            } else {
                final NioDatagramConnector connector = new NioDatagramConnector();
                TftpTransfer transfer = new TftpReadTransfer(address, source, request.getBlockSize());
                TftpTransferProtocolHandler handler = new TftpTransferProtocolHandler(connector, transfer);
                connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TftpProtocolCodecFactory()));
                connector.getFilterChain().addLast("logger-packet", new LoggingFilter("tftp-transfer-packet"));
                connector.setHandler(handler);
                ConnectFuture future = connector.connect(address);
                future.addListener(handler);
            }
            break;
        }
        case WRQ: {
            session.write(new TftpErrorPacket(address, TftpErrorCode.PERMISSION_DENIED), address);
            session.close(false);
            break;
        }
        case ACK: {
            break;
        }
        case DATA: {
            LOG.warn("Unexpected TFTP " + packet.getOpcode() + " packet: " + packet);
            session.write(new TftpErrorPacket(address, TftpErrorCode.ILLEGAL_OPERATION), address);
            session.close(false);
            break;
        }
        case ERROR: {
            TftpErrorPacket error = (TftpErrorPacket) packet;
            LOG.error("Received TFTP error packet: " + error);
            session.close(true);
            break;
        }
    }
}
 
開發者ID:shevek,項目名稱:tftp4j,代碼行數:49,代碼來源:TftpServerProtocolHandler.java

示例5: connect

import org.apache.mina.core.future.ConnectFuture; //導入方法依賴的package包/類
/**
 * <p>Connect to a remote socket. If org.opennms.netmgt.provision.maxConcurrentConnections
 * is set, this may block until a connection slot is available.</p>
 * 
 * <p>You must dispose both the {@link ConnectionFactoryConnectorPoolImpl} and {@link ConnectFuture} when done
 * by calling {@link #dispose(ConnectionFactoryConnectorPoolImpl, ConnectFuture)}.</p>
 * 
 * @param remoteAddress
 * 		Destination address
 * @param init
 * 		Initialiser for the IoSession
 * @return
 * 		ConnectFuture from a Mina connect call
 */
@Override
public ConnectFuture connect(SocketAddress remoteAddress, IoSessionInitializer<? extends ConnectFuture> init, IoHandler handler) {
    for (int retries = 0; retries < 3; retries++) { 
        synchronized (m_connectorMutex) {
            if (m_connector == null) {
                // Sanity check for null connector instance
                LogUtils.debugf(this, "Found a null NioSocketConnector, creating a new one with timeout %d", getTimeout());
                m_connector = getSocketConnector(getTimeout(), handler);
            }

            try {
                /*
                 * Set the handler each time since we are reusing this connector for every incoming
                 * connect() call.
                 */
                m_connector.setHandler(handler);

                InetSocketAddress localAddress = null;
                synchronized (m_portMutex) {
                    if (m_port == null) {
                        // Fetch a new ephemeral port
                        localAddress = new InetSocketAddress(InetAddressUtils.getLocalHostAddress(), 0);
                        m_port = localAddress.getPort();
                    } else {
                        localAddress = new InetSocketAddress(InetAddressUtils.getLocalHostAddress(), m_port);
                    }
                }

                /*
                 * Use the 3-argument call to connect(). If you use the 2-argument version without
                 * the localhost port, the call will end up doing a name lookup which seems to fail
                 * intermittently in unit tests.
                 *
                 * @see http://issues.opennms.org/browse/NMS-5309
                 */
                ConnectFuture cf = m_connector.connect(remoteAddress, localAddress, init);
                cf.addListener(portSwitcher(m_connector, remoteAddress, init, handler));
                return cf;
            } catch (Throwable e) {
                LogUtils.debugf(this, e, "Caught exception on factory %s, retrying: %s", this, e);
                m_connector.dispose();
                m_connector = getSocketConnector(getTimeout(), handler);
                continue;
            }
        }
    }
    throw new IllegalStateException("Could not connect to socket because of excessive RejectedExecutionExceptions");
}
 
開發者ID:qoswork,項目名稱:opennmszh,代碼行數:63,代碼來源:ConnectionFactoryConnectorPoolImpl.java


注:本文中的org.apache.mina.core.future.ConnectFuture.addListener方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。