Java Code Examples for javax.net.SocketFactory#hashCode()

The following examples show how to use javax.net.SocketFactory#hashCode() . 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: ClientInterfaceFactory.java    From nettythrift with Apache License 2.0 5 votes vote down vote up
/**
 * 获得与服务端通信的接口对象
 * <p>
 * 调用者可以实现自定义的 SocketFactory来内部配置Socket参数(如超时时间,SSL等),也可以通过返回包装的Socket来实现连接池<br/>
 * SocketFactory::createSocket(String host,int ip)//NOTE: 实际传入createSocket(methodName,flag)
 * 
 * @param ifaceClass
 *            - 接口class
 * @param factory
 *            - 套接字工厂类, 注意:需要实现 createSocket() 方法,需要实现hashCode()方法来区分factory
 * @return 接口对象
 */
@SuppressWarnings("unchecked")
public static <INTERFACE> INTERFACE getClientInterface(Class<INTERFACE> ifaceClass, SocketFactory factory) {
	long part1 = ifaceClass.getName().hashCode();
	final Long KEY = (part1 << 32) | factory.hashCode();
	INTERFACE iface = (INTERFACE) ifaceCache.get(KEY);
	if (iface == null) {
		iface = (INTERFACE) Proxy.newProxyInstance(ifaceClass.getClassLoader(), new Class[] { ifaceClass },
				new Handler(factory));
		ifaceCache.putIfAbsent(KEY, iface);
	}
	return iface;
}
 
Example 2
Source File: ClientInterfaceFactory.java    From nettythrift with Apache License 2.0 3 votes vote down vote up
/**
 * 获得与服务端通信的接口对象
 * <p>
 * 调用者可以实现自定义的 SocketFactory来内部配置Socket参数(如超时时间,SSL等),也可以通过返回包装的Socket来实现连接池
 * <br/><pre>
 *  class SocketPool extends javax.net.SocketFactory{
 *    public Socket createSocket(String methodName,int flag)throws IOException{
 *      return getSocketFromPool();
 *    }
 *    private MySocketWrapper getSocketFromPool(){
 *     ...
 *    }
 *    private void returnToPool(MySocketWrapper socket){
 *    ...
 *    }
 *  }
 *  class MySocketWrapper extends Socket{
 *    private Socket target;
 *    private SocketPool pool;
 *    // 包装close()方法,归还连接到连接池
 *    public void close()throws IOException{
 *      pool.returnToPool(this);
 *    }
 *    public OutputStream getOutputStream()throws IOException{
 *        return target.getOutputStream();
 *    }
 *    public InputStream getInputStream()throws IOException{
 *        return target.getInputStream();
 *    }
 *  }
 * </pre>
 * <br/>
 * 
 * 
 * @param ifaceClass
 *            - 接口class
 * @param factory
 *            - 套接字工厂类, 注意:需要实现 createSocket() 方法,需要实现hashCode()方法来区分factory
 * @return 接口对象
 */
@SuppressWarnings("unchecked")
public static <INTERFACE> INTERFACE getClientInterface(Class<INTERFACE> ifaceClass, SocketFactory factory) {
	long part1 = ifaceClass.getName().hashCode();
	final Long KEY = (part1 << 32) | factory.hashCode();
	INTERFACE iface = (INTERFACE) ifaceCache.get(KEY);
	if (iface == null) {
		iface = (INTERFACE) Proxy.newProxyInstance(ifaceClass.getClassLoader(), new Class[] { ifaceClass },
				new Handler(factory));
		ifaceCache.putIfAbsent(KEY, iface);
	}
	return iface;
}
 
Example 3
Source File: ClientInterfaceFactory.java    From nettythrift with Apache License 2.0 3 votes vote down vote up
/**
 * 获得与服务端通信的接口对象
 * <p>
 * 调用者可以实现自定义的
 * SocketFactory来内部配置Socket参数(如超时时间,SSL等),也可以通过返回包装的Socket来实现连接池,
 * 也可以使用内置的连接池类:{@link io.client.thrift.pool.SocketConnectionPool} <br/>
 * 使用例子:<br/>
 * {@code SocketFactory tcpfac = new TcpSocketFactory("localhost", 8080);}
 * <br/>
 * {@code  SocketFactory pool = new SocketConnectionPool(tcpfac);} <br/>
 * {@code SomeIface service = ClientInterfaceFactory.getClientInterface(SomeIface.class, pool); }
 * <br/>
 * 
 * 
 * @param ifaceClass
 *            - 接口class
 * @param factory
 *            - 套接字工厂类, 注意:需要实现 createSocket() 方法,需要实现hashCode()方法来区分factory
 * @return 接口对象
 */
@SuppressWarnings("unchecked")
public static <INTERFACE> INTERFACE getClientInterface(Class<INTERFACE> ifaceClass, SocketFactory factory) {
	long part1 = ifaceClass.getName().hashCode();
	final Long KEY = (part1 << 32) | factory.hashCode();
	INTERFACE iface = (INTERFACE) ifaceCache.get(KEY);
	if (iface == null) {
		iface = (INTERFACE) Proxy.newProxyInstance(ifaceClass.getClassLoader(), new Class[] { ifaceClass },
				new Handler(factory));
		ifaceCache.putIfAbsent(KEY, iface);
	}
	return iface;
}