1 factory的本质
就是把channel的构造函数包起来
1 2 3 4 5 6 7 8
| public ReflectiveChannelFactory(Class<? extends T> clazz) { try { this.constructor = clazz.getConstructor(); } catch (NoSuchMethodException e) { throw new IllegalArgumentException("Class " + StringUtil.simpleClassName(clazz) + " does not have a public non-arg constructor", e); } }
|
然后把这个factory保存起来 需要channel的实例的时候就用这个factory构造一个出来
1 2 3 4 5 6 7 8 9 10 11 12
| public B channelFactory(ChannelFactory<? extends C> channelFactory) { if (this.channelFactory != null) throw new IllegalStateException("channelFactory set already");
this.channelFactory = channelFactory; return self(); }
|
2 构造实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| @Override public T newChannel() { try {
return this.constructor.newInstance(); } catch (Throwable t) { throw new ChannelException("Unable to create Channel from class " + constructor.getDeclaringClass(), t); } }
|