/** * Creates and starts a fiber and returns the created instance. * @return The created fiber. */ public static Fiber pooledFiber() { Fiber fiber = FACT.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; } }
/** * Receives message and batches as needed. */ @Override protected void onMessageOnProducerThread(T msg) { synchronized (_lock) { if (_pending == null) { _pending = new ArrayList<>(); _queue.schedule(_flushRunnable, _interval, _timeUnit); } _pending.add(msg); } }
private void dispatch(final WsMessage message) { executor.execute(() -> { for (Listener remote : remotes) { send(message, remote); } }); }
void resetTimeout(@Nonnull final RaftStateContext ctx) { if (null != heartbeatTask) { heartbeatTask.dispose(); } heartbeatTask = scheduler.scheduleAtFixedRate(() -> { LOGGER.debug("Sending heartbeat"); sendRequests(ctx); }, timeout, timeout, MILLISECONDS); }
/** * Receives message and batches as needed. */ @Override protected void onMessageOnProducerThread(T msg) { synchronized (_lock) { if (_pending == null) { _pending = new ArrayList<>(); _queue.schedule(_flushRunnable, _interval, _timeUnit); } _pending.add(msg); } }
public static Fiber threadFiber() { Fiber fiber = new ThreadFiber(); fiber.start(); return fiber; }
@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(); }
/** * Message received and batched on producer thread. */ @Override protected void onMessageOnProducerThread(T msg) { synchronized (_batchLock) { K key = _keyResolver.convert(msg); if (_pending == null) { _pending = new HashMap<>(); _context.schedule(_flushRunner, _flushIntervalInMs, _timeUnit); } _pending.put(key, msg); } }
/** * 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; }
public void stop() { executor.dispose(); }
/** * Message received and batched on producer thread. */ @Override protected void onMessageOnProducerThread(T msg) { synchronized (_batchLock) { K key = _keyResolver.convert(msg); if (_pending == null) { _pending = new HashMap<>(); _context.schedule(_flushRunner, _flushIntervalInMs, _timeUnit); } _pending.put(key, msg); } }
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()); } }
public void send(RequestChannel<R, V> channel, R req, Fiber target) { // send may occur from a different thread than the receive thread. // Once the message is sent there is a race. The lock prevents the receiving thread from // attempting to dispose before the variables are properly set. synchronized (lock) { final Disposable requestDispose = channel.publish(target, req, this); if (timeout != null) { final Disposable timer = target.schedule(this, timeout.time, timeout.unit); this.d = new Disposable() { public void dispose() { requestDispose.dispose(); timer.dispose(); } }; } else { d = requestDispose; } } } }
public void start() { executor.start(); }
public void send(RequestChannel<R, V> channel, R req, Fiber target) { // send may occur from a different thread than the receive thread. // Once the message is sent there is a race. The lock prevents the receiving thread from // attempting to dispose before the variables are properly set. synchronized (lock) { final Disposable requestDispose = channel.publish(target, req, this); if (timeout != null) { final Disposable timer = target.schedule(this, timeout.time, timeout.unit); this.d = new Disposable() { public void dispose() { requestDispose.dispose(); timer.dispose(); } }; } else { d = requestDispose; } } } }
@Override protected void configure() { install(new StateModule(timeout)); PoolFiberFactory fiberFactory = new PoolFiberFactory(executor); Fiber raftFiber = fiberFactory.create(new BatchExecutor()); raftFiber.start(); bind(Fiber.class).annotatedWith(RaftExecutor.class).toInstance(raftFiber); Fiber stateMachineFiber = fiberFactory.create(new BatchExecutor()); stateMachineFiber.start(); install(new LogModule(logDir, stateMachine, stateMachineFiber)); bind(ClusterConfig.class).toInstance(config); bind(Client.class).asEagerSingleton(); expose(Raft.class); expose(ClusterConfig.class); }
/** * Receives message and batches as needed. */ @Override protected void onMessageOnProducerThread(T msg) { _lock.lock(); try { if (_pending.isEmpty()) { _queue.schedule(_flushRunnable, _interval, _timeUnit); } _pending.add(msg); } finally { _lock.unlock(); } }