org.apache.mina.transport.socket.SocketConnector Java Examples

The following examples show how to use org.apache.mina.transport.socket.SocketConnector. 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: ProxyConnector.java    From neoscada with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Sets the {@link SocketConnector} to be used for connections
 * to the proxy server.
 * 
 * @param connector the connector to use
 */
private final void setConnector(final SocketConnector connector) {
    if (connector == null) {
        throw new IllegalArgumentException("connector cannot be null");
    }

    this.connector = connector;
    String className = ProxyFilter.class.getName();

    // Removes an old ProxyFilter instance from the chain
    if (connector.getFilterChain().contains(className)) {
        connector.getFilterChain().remove(className);
    }

    // Insert the ProxyFilter as the first filter in the filter chain builder        
    connector.getFilterChain().addFirst(className, proxyFilter);
}
 
Example #2
Source File: SocketConnectorSupplier.java    From sumk with Apache License 2.0 6 votes vote down vote up
private synchronized SocketConnector create() {
	if (connector != null && !connector.isDisposing() && !connector.isDisposed()) {
		return connector;
	}
	try {
		NioSocketConnector con = new NioSocketConnector(
				AppInfo.getInt("sumk.rpc.client.poolsize", Runtime.getRuntime().availableProcessors() + 1));
		con.setConnectTimeoutMillis(AppInfo.getInt("sumk.rpc.connect.timeout", 5000));
		con.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, AppInfo.getInt(Const.SOA_SESSION_IDLE, 600));
		con.setHandler(createClientHandler());
		con.getFilterChain().addLast("codec", new ProtocolCodecFilter(IOC.get(SumkCodecFactory.class)));
		if (AppInfo.getBoolean("sumk.rpc.client.threadpool.enable", true)) {
			con.getFilterChain().addLast("threadpool", new ExecutorFilter(SoaExcutors.getClientThreadPool()));
		}
		this.connector = con;
		return con;
	} catch (Exception e) {
		Logs.rpc().error(e.getMessage(), e);
		throw new SumkException(5423654, "create connector error", e);
	}
}
 
Example #3
Source File: ReqSession.java    From sumk with Apache License 2.0 6 votes vote down vote up
private boolean ensureSession() {
	if (session != null && !session.isClosing()) {
		return true;
	}
	try {
		SocketConnector connector = this.getConnector();
		if (lock.tryLock(connector.getConnectTimeoutMillis() + 2000, TimeUnit.MILLISECONDS)) {
			try {
				if (session != null && !session.isClosing()) {
					return true;
				}
				connect(connector);
			} finally {
				lock.unlock();
			}
		}
	} catch (Exception e1) {
		Logs.rpc().error(this.addr + " - " + e1.toString(), e1);
		HostChecker.get().addDownUrl(addr);
	}

	if (session == null || session.isClosing()) {
		return false;
	}
	return true;
}
 
Example #4
Source File: ReqSession.java    From sumk with Apache License 2.0 6 votes vote down vote up
private void connect(SocketConnector connector) throws InterruptedException {

		if (session == null || session.isClosing()) {
			Logs.rpc().debug("create session for {}", addr);
			ConnectFuture cf = connector.connect(addr.toInetSocketAddress());

			cf.await(connector.getConnectTimeoutMillis() + 1);
			IoSession se = cf.getSession();
			if (se != null) {
				this.session = se;
				return;
			}
			cf.cancel();

		}
	}
 
Example #5
Source File: SocketConnectorSupplier.java    From sumk with Apache License 2.0 5 votes vote down vote up
@Override
public SocketConnector get() {
	SocketConnector con = this.connector;
	if (con != null && !con.isDisposing() && !con.isDisposed()) {
		return con;
	}
	return this.create();
}
 
Example #6
Source File: TestReloadClassLoader.java    From gameserver with Apache License 2.0 5 votes vote down vote up
@Test
	public void testClassLoaderLeak2() throws Exception {
		int max = 10000;
		File sourceFile = new File(reloadSourceDir);
		URL[] urls = new URL[]{sourceFile.toURL()};
//		ReloadProtocolCodecFilter filter = ReloadProtocolCodecFilter.getInstance(
//				GameServer.PROTOCOL_CODEC, GameServer.PROTOCOL_HANDLER, urls);
		SocketConnector connector = new NioSocketConnector();
//		connector.getFilterChain().addLast("codec", filter);
		connector.setHandler(new ClientHandler());
    //Send 1000 connections.
    try {
			for ( int i=0; i<Integer.MAX_VALUE; i++ ) {
				ConnectFuture future = connector.connect(new InetSocketAddress("localhost", 3443));
				future.awaitUninterruptibly();
				IoSession session = future.getSession();
				IoBuffer buffer = IoBuffer.allocate(8);
				buffer.putShort((short)8);
				buffer.putShort((short)0);
				buffer.putInt(i);
				WriteFuture wfuture = session.write(buffer);
				wfuture.awaitUninterruptibly();
			}
		} catch (Exception e) {
			e.printStackTrace();
			fail();
		}
	}
 
Example #7
Source File: ProxyConnector.java    From neoscada with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Creates a new proxy connector. 
 * @see AbstractIoConnector(IoSessionConfig, Executor).
 */
public ProxyConnector(final SocketConnector connector, IoSessionConfig config, Executor executor) {
    super(config, executor);
    setConnector(connector);
}
 
Example #8
Source File: ReqSession.java    From sumk with Apache License 2.0 4 votes vote down vote up
public static void setConnectorSupplier(Supplier<SocketConnector> connectorSupplier) {
	ReqSession.connectorSupplier = Objects.requireNonNull(connectorSupplier);
}
 
Example #9
Source File: ReqSession.java    From sumk with Apache License 2.0 4 votes vote down vote up
public static Supplier<SocketConnector> getConnectorSupplier() {
	return connectorSupplier;
}
 
Example #10
Source File: ReqSession.java    From sumk with Apache License 2.0 4 votes vote down vote up
public SocketConnector getConnector() {
	return connectorSupplier.get();
}
 
Example #11
Source File: ProxyConnector.java    From neoscada with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Creates a new proxy connector.
 * 
 * @param connector Connector used to establish proxy connections.
 */
public ProxyConnector(final SocketConnector connector) {
    this(connector, new DefaultSocketSessionConfig(), null);
}
 
Example #12
Source File: ProxyConnector.java    From neoscada with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Get the {@link SocketConnector} to be used for connections
 * to the proxy server.
 */
public final SocketConnector getConnector() {
    return connector;
}
 
Example #13
Source File: StreamBaseDevice.java    From neoscada with Eclipse Public License 1.0 votes vote down vote up
protected abstract void setupConnector ( SocketConnector connector );