public static Fiber threadFiber() { Fiber fiber = new ThreadFiber(); fiber.start(); return fiber; }
/** * Creates and starts a fiber and returns the created instance. * @return The created fiber. */ public static Fiber pooledFiber(Lane<String,ExecutorService> lane) { if(null == lanePoolFactoryMap.get(lane)) { lanePoolFactoryMap.putIfAbsent(lane, new PoolFiberFactory(lane.getUnderlyingLane())); } Fiber fiber = lanePoolFactoryMap.get(lane).create(); fiber.start(); return fiber; }
@Override public synchronized void close() { if (!isCloseCalled) { fiber.dispose(); eventQueue.clearSubscribers(); // Iterate through the list of disposables and dispose each one. Collection<Disposable> disposables = disposableHandlerMap.values(); for (Disposable disposable : disposables) { disposable.dispose(); } handlersByEventType.clear(); handlersByEventType = null; anyHandler.clear(); anyHandler = null; isCloseCalled = true; } }
@Override public void close(final SelectableChannel channel) { if (onSelectorThread()) { controls.close(channel); } else { execute(new Callback<NioControls>() { @Override public void onMessage(NioControls message) { controls.close(channel); } }); } }
public NioFiberImpl(final NioBatchExecutor executor, Collection<NioChannelHandler> handlers) { this(executor, handlers, "nioFiber", true, new NoOpWriteFailure(), new NoOpBuffer()); }
/** * Create a new Fiber from this pool that uses the supplied {@link Executor} to execute commands * * @param batchExecutor Executor to use for command executor. Required. * @return Fiber instance */ public Fiber create(BatchExecutor batchExecutor) { return new PoolFiber(this.executor, batchExecutor, _scheduler); }
public void onSelectorEnd() { for (NioChannelHandler handler : handlers) { handler.onSelectorEnd(); } }
private NioChannelHandler.Result execEvent(SelectionKey key, NioState attachment) { try { return attachment.onSelect(executor, this, controls, key); } catch (CancelledKeyException invalid) { //key is cancelled, so remove all handlers. //this won't close the socket, but it is assumed that //the code that initiated the cancel knows whether the socket //should be closed return NioChannelHandler.Result.RemoveHandler; } }
private Disposable register(final Disposable stopper) { Disposable wrapper = new Disposable() { public void dispose() { stopper.dispose(); remove(this); } }; //scheduler is shared so tasks removed individually add(wrapper); return wrapper; } }
@Override public void onMessage(NioControls message) { controls.close(channel); } });
public void onEnd() { for (NioChannelHandler handler : handlers) { handler.onEnd(); } }
public Downloader() { PoolFiberFactory f = new PoolFiberFactory(pool); //subscribe multiple fibers for parallel execution for (int i = 0, numThreads = 10; i < numThreads; i++) { Fiber fiber = f.create(); fiber.start(); fibers.add(fiber); channel.subscribe(fiber, new DownloadCallback()); } }
@Override public void dispose() { for (DownloadJob j : jobs) { switch (j.getState()) { case NEW: case PREPARING: case WORKING: j.setState(State.ABORTED); } } for (Fiber f : fibers) { f.dispose(); } pool.shutdown(); }
@Override public void close(final SelectableChannel channel) { if (onSelectorThread()) { controls.close(channel); } else { execute(new Callback<NioControls>() { @Override public void onMessage(NioControls message) { controls.close(channel); } }); } }
public NioFiberImpl(final NioBatchExecutor executor, Collection<NioChannelHandler> handlers) { this(executor, handlers, "nioFiber", true, new NoOpWriteFailure(), new NoOpBuffer()); }
/** * Create a new Fiber from this pool that uses the supplied {@link Executor} to execute commands * * @param batchExecutor Executor to use for command executor. Required. * @return Fiber instance */ public Fiber create(BatchExecutor batchExecutor) { return new PoolFiber(this.executor, batchExecutor, _scheduler); }
public void onSelectorEnd() { for (NioChannelHandler handler : handlers) { handler.onSelectorEnd(); } }
private NioChannelHandler.Result execEvent(SelectionKey key, NioState attachment) { try { return attachment.onSelect(executor, this, controls, key); } catch (CancelledKeyException invalid) { //key is cancelled, so remove all handlers. //this won't close the socket, but it is assumed that //the code that initiated the cancel knows whether the socket //should be closed return NioChannelHandler.Result.RemoveHandler; } }