@Override protected void doStart() throws Exception { if (timer == null) { HashedWheelTimer hashedWheelTimer = new HashedWheelTimer(); hashedWheelTimer.start(); timer = hashedWheelTimer; } if (configuration == null) { configuration = new NettyConfiguration(); } if (configuration.isOrderedThreadPoolExecutor()) { executorService = createExecutorService(); } super.doStart(); }
@Test public void testRemoveMessageFuture() throws Exception { HashedWheelTimer timer = getTimer(); RequestManager requestManager = new RequestManager(timer); try { RequestPacket packet = new RequestPacket(1, new byte[0]); DefaultFuture future = requestManager.register(packet, 2000); future.setFailure(new RuntimeException()); Future nullFuture = requestManager.removeMessageFuture(packet.getRequestId()); Assert.assertNull(nullFuture); } finally { requestManager.close(); timer.stop(); } }
@Test public void testRegisterRequest() throws Exception { HashedWheelTimer timer = getTimer(); RequestManager requestManager = new RequestManager(timer); try { RequestPacket packet = new RequestPacket(new byte[0]); Future future = requestManager.register(packet, 50); Thread.sleep(200); Assert.assertTrue(future.isReady()); Assert.assertFalse(future.isSuccess()); Assert.assertTrue(future.getCause().getMessage().contains("timeout")); logger.debug(future.getCause().getMessage()); } finally { requestManager.close(); timer.stop(); } }
/** terminate this context */ public synchronized void term() { clientScheduleService.stop(); for (IConnection conn : connections.values()) { conn.close(); } connections = null; // we need to release resources associated with client channel factory clientChannelFactory.releaseExternalResources(); }
private long scheduleTimeout(long id, final Context context, final Runnable task, long delay) { TimerTask ttask = new TimerTask() { public void run(Timeout timeout) throws Exception { context.execute(task); } }; if (id != -1 && timeouts.get(id) == null) { // Been cancelled return -1; } Timeout timeout = timer.newTimeout(ttask, delay, TimeUnit.MILLISECONDS); id = id != -1 ? id : timeoutCounter.getAndIncrement(); timeouts.put(id, new TimeoutHolder(timeout)); return id; }
/** {@inheritDoc} */ public void destroy() { if (isClosed.getAndSet(true)) return; timer.stop(); for (Map.Entry<Channel, Timeout> e : trackedIdleConnections.entrySet()) { close(e.getKey()); e.getValue().cancel(); } trackedIdleConnections.clear(); try { Iterator<Map.Entry<String, ConcurrentLinkedQueue<Channel>>> i = connectionsPool.entrySet().iterator(); while (i.hasNext()) { for (Channel channel : i.next().getValue()) { close(channel); } } } finally { connectionsPool.clear(); } }
/** {@inheritDoc} */ public boolean offer(String uri, Channel channel) { if (!provider.getConfig().isSslConnectionPoolEnabled() && uri.startsWith("https")) { return false; } log.debug("Adding uri: {} for channel {}", uri, channel); channel .getPipeline() .getContext(NettyAsyncHttpProvider.class) .setAttachment(new NettyAsyncHttpProvider.DiscardEvent()); ConcurrentLinkedQueue<Channel> pooledConnectionForHost = connectionsPool.get(uri); if (pooledConnectionForHost == null) { ConcurrentLinkedQueue<Channel> newPool = new ConcurrentLinkedQueue<Channel>(); connectionsPool.putIfAbsent(uri, newPool); pooledConnectionForHost = connectionsPool.get(uri); } boolean added; int size = pooledConnectionForHost.size(); if (config.getMaxConnectionPerHost() == -1 || size < config.getMaxConnectionPerHost()) { added = pooledConnectionForHost.add(channel); if (added) { Timeout t = timer.newTimeout( new IdleRunner(channel, pooledConnectionForHost), config.getIdleConnectionInPoolTimeoutInMs(), TimeUnit.MILLISECONDS); trackedIdleConnections.put(channel, t); log.debug("ConnectionsPool increment totalConnections {}", trackedIdleConnections.size()); } } else { log.debug("Maximum connections per hosts reached {}", config.getMaxConnectionPerHost()); added = false; } return added; }
{ timer.start(); }
@Override public void stop() { if (sharedHttpServers != null) { for (HttpServer server : sharedHttpServers.values()) { server.close(); } sharedHttpServers = null; } if (sharedNetServers != null) { for (NetServer server : sharedNetServers.values()) { server.close(); } sharedNetServers = null; } if (timer != null) { timer.stop(); timer = null; } if (eventBus != null) { eventBus.close(null); } if (backgroundPool != null) { backgroundPool.shutdown(); } if (acceptorPool != null) { acceptorPool.shutdown(); } try { if (backgroundPool != null) { backgroundPool.awaitTermination(20, TimeUnit.SECONDS); backgroundPool = null; } } catch (InterruptedException ex) { // ignore } try { if (acceptorPool != null) { acceptorPool.awaitTermination(20, TimeUnit.SECONDS); acceptorPool = null; } } catch (InterruptedException ex) { // ignore } // log.info("Release external resources from worker pool"); if (corePool != null) { corePool.releaseExternalResources(); corePool = null; } // log.info("Release external resources: done"); setContext(null); }
// @Test public void testTimerStartTiming() throws InterruptedException { HashedWheelTimer timer = new HashedWheelTimer(1000, TimeUnit.MILLISECONDS); timer.start(); timer.stop(); }
private Timer newNettyTimer() { HashedWheelTimer timer = new HashedWheelTimer(); timer.start(); return timer; }