org.apache.mina.transport.socket.nio.NioSocketConnector类的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(11.4k)|赞(0)|评价(0)|浏览(180)

本文整理了Java中org.apache.mina.transport.socket.nio.NioSocketConnector类的一些代码示例,展示了NioSocketConnector类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。NioSocketConnector类的具体详情如下:
包路径:org.apache.mina.transport.socket.nio.NioSocketConnector
类名称:NioSocketConnector

NioSocketConnector介绍

[英]IoConnector for socket transport (TCP/IP).
[中]用于套接字传输(TCP/IP)的IO连接器。

代码示例

代码示例来源:origin: ltsopensource/light-task-scheduler

@Override
protected void clientStart() throws RemotingException {
  try {
    connector = new NioSocketConnector(); //TCP Connector
    // connector.getFilterChain().addFirst("logging", new MinaLoggingFilter());
    connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new MinaCodecFactory(getCodec())));
    connector.getFilterChain().addLast("mdc", new MdcInjectionFilter());
    connector.setHandler(new MinaHandler(this));
    IoSessionConfig cfg = connector.getSessionConfig();
    cfg.setReaderIdleTime(remotingClientConfig.getReaderIdleTimeSeconds());
    cfg.setWriterIdleTime(remotingClientConfig.getWriterIdleTimeSeconds());
    cfg.setBothIdleTime(remotingClientConfig.getClientChannelMaxIdleTimeSeconds());
  } catch (Exception e) {
    throw new RemotingException("Mina Client start error", e);
  }
}

代码示例来源:origin: ltsopensource/light-task-scheduler

@Override
  protected ChannelFuture connect(SocketAddress socketAddress) {
    ConnectFuture connectFuture = connector.connect(socketAddress);
    return new MinaChannelFuture(connectFuture);
  }
}

代码示例来源:origin: ltsopensource/light-task-scheduler

@Override
protected void clientShutdown() {
  if (connector != null) {
    connector.dispose();
  }
}

代码示例来源:origin: Red5/red5-server

IoConnector connector = new NioSocketConnector();
connector.setHandler(this);
ConnectFuture future = connector.connect(forward);

代码示例来源:origin: jzyong/game-server

/**
 * 初始化tcp连接
 * @param clientProtocolHandler
 */
private void init(IoHandler clientProtocolHandler) {
  connector = new NioSocketConnector();
  DefaultIoFilterChainBuilder chain = connector.getFilterChain();
  chain.addLast("codec", codecFilter);
  if(filters != null){
    filters.forEach((key, filter)->{
      if("ssl".equalsIgnoreCase(key) || "tls".equalsIgnoreCase(key)){	//ssl过滤器必须添加到首部
        chain.addFirst(key, filter);
      }else{
        chain.addLast(key, filter);
      }
    });
  }
  connector.setHandler(clientProtocolHandler);
  connector.setConnectTimeoutMillis(60000L);
  connector.setConnectTimeoutCheckInterval(10000);
}

代码示例来源:origin: OpenNMS/opennms

private static final NioSocketConnector getSocketConnector(long timeout, IoHandler handler) {
  NioSocketConnector connector = new NioSocketConnector();
  connector.setHandler(handler);
  connector.setConnectTimeoutMillis(timeout);
  return connector;
}

代码示例来源:origin: de.dentrassi.eclipse.neoscada.core/org.eclipse.scada.da.server.common.io

this.connector = new NioSocketConnector ( this.processor );
    this.connector = new NioSocketConnector ();
  this.connector.setHandler ( this.handler );
  this.connector.setConnectTimeoutMillis ( this.connectTimeout );
final ConnectFuture future = this.connector.connect ( new InetSocketAddress ( this.host, this.port ) );
future.addListener ( new IoFutureListener<IoFuture> () {

代码示例来源:origin: com.eas.platypus/platypus-js-core

new PlatypusThreadFactory("polling-", false));
ioProcessorExecutor.allowCoreThreadTimeOut(true);
NioSocketConnector lconnector = new NioSocketConnector(aProcessor, new NioProcessor(ioProcessorExecutor));
lconnector.setDefaultRemoteAddress(new InetSocketAddress(host, port));
  lconnector.getFilterChain().addLast("executor", new ExecutorFilter(aProcessor));
  lconnector.getFilterChain().addLast("executor", new ExecutorFilter(aProcessor));
lconnector.getFilterChain().addLast("platypusCodec", new ProtocolCodecFilter(new RequestEncoder(), new ResponseDecoder()));
lconnector.setHandler(new IoHandlerAdapter() {

代码示例来源:origin: com.github.mrstampy/esp

private void initConnector() {
  connector = new NioSocketConnector();
  DefaultIoFilterChainBuilder chain = connector.getFilterChain();
  MdcInjectionFilter mdcInjectionFilter = new MdcInjectionFilter();
  chain.addLast("mdc", mdcInjectionFilter);
  chain.addLast("codec", new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
  connector.setHandler(new IoHandlerAdapter() {
    @SuppressWarnings("unchecked")
    public void messageReceived(IoSession session, Object message) throws Exception {
      if (message instanceof AbstractMultiConnectionEvent<?>) {
        AbstractMultiConnectionEvent<E> event = (AbstractMultiConnectionEvent<E>) message;
        Observable.just(event).subscribe(new Action1<AbstractMultiConnectionEvent<E>>() {
          @Override
          public void call(AbstractMultiConnectionEvent<E> t1) {
            processEvent(t1);
          }
        });
      }
    }
  });
}

代码示例来源:origin: org.apache.sshd/sshd-mina

protected IoConnector createConnector() {
  NioSocketConnector connector = new NioSocketConnector(ioProcessor);
  configure(connector.getSessionConfig());
  return connector;
}

代码示例来源:origin: org.apache.vysper/vysper-core

private NioSocketConnector createConnector(CountDownLatch authenticatedLatch) {
  NioSocketConnector connector = new NioSocketConnector();
  DefaultIoFilterChainBuilder filterChainBuilder = new DefaultIoFilterChainBuilder();
  filterChainBuilder.addLast("xmppCodec", new ProtocolCodecFilter(new XMPPProtocolCodecFactory()));
  filterChainBuilder.addLast("loggingFilter", new StanzaLoggingFilter());
  connector.setFilterChainBuilder(filterChainBuilder);
  connector.setHandler(new ConnectorIoHandler(authenticatedLatch));
  return connector;
}

代码示例来源:origin: kaazing/gateway

/**
 * {@inheritDoc}
 */
@Override
protected SocketChannel newHandle(SocketAddress localAddress)
    throws Exception {
  SocketChannel ch = SocketChannel.open();
  int receiveBufferSize =
    (getSessionConfig()).getReceiveBufferSize();
  if (receiveBufferSize > 65535) {
    if (AbstractIoSessionConfig.ENABLE_BUFFER_SIZE) {
      System.out.println("NioSocketConnector.newHandle("+receiveBufferSize+")");
      ch.socket().setReceiveBufferSize(receiveBufferSize);
    }
  }
  if (localAddress != null) {
    ch.socket().bind(localAddress);
  }
  ch.configureBlocking(false);
  return ch;
}

代码示例来源:origin: OpenNMS/opennms

m_connector.setHandler(handler);
  ConnectFuture cf = m_connector.connect(remoteAddress, localAddress, init);
  cf.addListener(portSwitcher(m_connector, remoteAddress, init, handler));
  return cf;
} catch (Throwable e) {
  LOG.debug("Caught exception on factory {}, retrying: {}", this, e);
  m_connector.dispose();
  m_connector = getSocketConnector(getTimeout(), handler);
  continue;

代码示例来源:origin: com.github.mrstampy/esp

/**
 * Connects to the {@link AbstractMultiConnectionSocket} instance. The
 * listeners will not be notified of events until
 * {@link #subscribe(EventType...)} or {@link #subscribeAll()} has been
 * called.
 * 
 * @throws MultiConnectionSocketException
 *           if unable to connect to a {@link AbstractMultiConnectionSocket}
 */
public void connect() throws MultiConnectionSocketException {
  if (isConnected()) {
    log.error("Already connected");
    return;
  }
  initConnector();
  ConnectFuture cf = connector.connect(new InetSocketAddress(socketBroadcasterHost, BROADCASTER_PORT));
  cf.awaitUninterruptibly(2000);
  if (cf.isConnected()) {
    log.info("Connected to AbstractMultiConnectionSocket on host {} and port {}", socketBroadcasterHost,
        BROADCASTER_PORT);
  } else {
    connector.dispose(true);
    String msg = format("Could not connect to AbstractMultiConnectionSocket on host {0} and port {1}",
        socketBroadcasterHost, BROADCASTER_PORT);
    log.error(msg);
    throw new MultiConnectionSocketException(msg);
  }
}

代码示例来源:origin: r17171709/android_demo

/**
 * 连接
 * @return
 */
public boolean connect() {
  if (connector!=null && connector.isActive() &&
      connectFuture!=null && connectFuture.isConnected() &&
      ioSession!=null && ioSession.isConnected()) {
    return true;
  }
  try {
    connectFuture=connector.connect(new InetSocketAddress(Params.HOSTNAME, Params.PORT));
    //等待是否连接成功,相当于是转异步执行为同步执行。
    connectFuture.awaitUninterruptibly();
    //连接成功后获取会话对象。如果没有上面的等待,由于connect()方法是异步的,session 可能会无法获取。
    ioSession=connectFuture.getSession();
    sendMessage("Hello Client");
    return true;
  } catch (Exception e) {
    e.printStackTrace();
    Log.d("PushManager", "服务器与客户端连接异常...");
  }
  return false;
}

代码示例来源:origin: org.eclipse.neoscada.core/org.eclipse.scada.da.server.osgi.modbus

@Override
protected void configureConnector ( final NioSocketConnector connector )
{
  logger.debug ( "Configuring connector: {}", connector );
  switch ( this.protocolType )
  {
    case TYPE_TCP:
      connector.getFilterChain ().addLast ( "modbusPdu", new ProtocolCodecFilter ( new ModbusTcpEncoder (), new ModbusTcpDecoder () ) );
      connector.getFilterChain ().addLast ( "modbus", new ModbusMasterProtocolFilter () );
      break;
    case TYPE_RTU:
      // convert milliseconds to microseconds to allow more accurate timing
      final ModbusRtuDecoder rtuDecoder = new ModbusRtuDecoder ( getExecutor (), Double.valueOf ( this.interFrameDelay * 1000 ).longValue (), TimeUnit.MICROSECONDS );
      connector.getFilterChain ().addLast ( "modbusPdu", new ModbusRtuProtocolCodecFilter ( new ModbusRtuEncoder (), rtuDecoder ) );
      connector.getFilterChain ().addLast ( "modbus", new ModbusMasterProtocolFilter () );
      break;
    default:
      throw new IllegalArgumentException ( String.format ( "'%s' is not an allowed modbus device type", this.protocolType ) );
  }
  if ( Boolean.getBoolean ( "org.eclipse.scada.da.server.osgi.modbus.trace" ) )
  {
    connector.getFilterChain ().addFirst ( "logger", new LoggingFilter ( ModbusMaster.class.getName () + ".protocol" ) );
  }
}

代码示例来源:origin: r17171709/android_demo

/**
 * 关闭
 */
public void close() {
  if(ioSession!=null && ioSession.isConnected()){
    ioSession.close(false);
  }
  if(connectFuture!=null && connectFuture.isConnected()) {
    connectFuture.cancel();
  }
  if(connector!=null && !connector.isDisposed()) {
    connector.dispose();
  }
}

代码示例来源:origin: com.github.kmbulebu.dsc/dsc-it100-library

/**
 * Use a TCP connection for remotely connecting to the IT-100.
 * 
 * Typically used with a utility such as ser2net for connecting to a remote serial port.
 * @param host Hostname or IP address of the remote device.
 * @param port TCP port of the remote device.
 * @return This builder instance.
 */
public ConfigurationBuilder withRemoteSocket(String host, int port) {
  configuration.connector = new NioSocketConnector();
  configuration.address = new InetSocketAddress(host, port);
  return this;
}

代码示例来源:origin: org.apache.mina/mina-core

/**
 * {@inheritDoc}
 */
@Override
protected SocketChannel newHandle(SocketAddress localAddress) throws Exception {
  SocketChannel ch = SocketChannel.open();
  int receiveBufferSize = (getSessionConfig()).getReceiveBufferSize();
  if (receiveBufferSize > 65535) {
    ch.socket().setReceiveBufferSize(receiveBufferSize);
  }
  if (localAddress != null) {
    try {
      ch.socket().bind(localAddress);
    } catch (IOException ioe) {
      // Add some info regarding the address we try to bind to the
      // message
      String newMessage = "Error while binding on " + localAddress + "\n" + "original message : "
          + ioe.getMessage();
      Exception e = new IOException(newMessage);
      e.initCause(ioe.getCause());
      // Preemptively close the channel
      ch.close();
      throw e;
    }
  }
  ch.configureBlocking(false);
  return ch;
}

代码示例来源:origin: ltsopensource/light-task-scheduler

@Override
protected void clientStart() throws RemotingException {
  try {
    connector = new NioSocketConnector(); //TCP Connector
    // connector.getFilterChain().addFirst("logging", new MinaLoggingFilter());
    connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new MinaCodecFactory(getCodec())));
    connector.getFilterChain().addLast("mdc", new MdcInjectionFilter());
    connector.setHandler(new MinaHandler(this));
    IoSessionConfig cfg = connector.getSessionConfig();
    cfg.setReaderIdleTime(remotingClientConfig.getReaderIdleTimeSeconds());
    cfg.setWriterIdleTime(remotingClientConfig.getWriterIdleTimeSeconds());
    cfg.setBothIdleTime(remotingClientConfig.getClientChannelMaxIdleTimeSeconds());
  } catch (Exception e) {
    throw new RemotingException("Mina Client start error", e);
  }
}

相关文章