@SuppressWarnings("unchecked") void check() { watchDogRuns++; long current = System.currentTimeMillis(); ArrayList<WeakReference<TimeoutMgmHandle>> connectionsCopy = null; synchronized (handles) { connectionsCopy = (ArrayList<WeakReference<TimeoutMgmHandle>>) handles.clone(); } for (WeakReference<TimeoutMgmHandle> handleRef : connectionsCopy) { TimeoutMgmHandle handle = handleRef.get(); if (handle == null) { remove(handleRef); } else { NonBlockingConnection con = handle.getConnection(); if (con.isOpen()) { checkTimeout(con, current); } else { remove(handleRef); } } } computeSize(); }
private void remove(WeakReference<TimeoutMgmHandle> handleRef) { synchronized (handles) { handles.remove(handleRef); } currentSize.decrementAndGet(); if (LOG.isLoggable(Level.FINE)) { TimeoutMgmHandle hdl = handleRef.get(); if (hdl != null) { INonBlockingConnection con = hdl.getConnection(); if (con != null) { LOG.fine( "[" + con.getId() + "] handle deregistered (connections size=" + computeSize() + ")"); } } } }
@SuppressWarnings("unchecked") Set<NonBlockingConnection> getConnections() { final Set<NonBlockingConnection> cons = new HashSet<NonBlockingConnection>(); ArrayList<WeakReference<TimeoutMgmHandle>> connectionsCopy = null; synchronized (handles) { connectionsCopy = (ArrayList<WeakReference<TimeoutMgmHandle>>) handles.clone(); } for (WeakReference<TimeoutMgmHandle> handleRef : connectionsCopy) { TimeoutMgmHandle handle = handleRef.get(); if (handle != null) { NonBlockingConnection con = handle.getConnection(); if (con != null) { cons.add(con); } } } return cons; }
/** * {@link NonBlockingConnection#NonBlockingConnection(ConnectionManager, HandlerAdapter)} 处被调用. * </br> 一个连接注册一个. </br> */ public TimeoutMgmHandle register(NonBlockingConnection connection) { TimeoutMgmHandle mgnCon = new TimeoutMgmHandle(connection); WeakReference<TimeoutMgmHandle> ref = mgnCon.getWeakRef(); System.out.println("ref:" + ref); if (ref != null) { synchronized (handles) { handles.add(ref); } } else { if (LOG.isLoggable(Level.FINE)) { LOG.fine("did not get the weak ref"); } } currentSize.incrementAndGet(); if (LOG.isLoggable(Level.FINE)) { LOG.fine("connection registered"); } return mgnCon; }