java.util.concurrent.BlockingQueue Java Examples
The following examples show how to use
java.util.concurrent.BlockingQueue.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EndpointConnectionPool.java From localization_nifi with Apache License 2.0 | 6 votes |
private void cleanupExpiredSockets() { for (final BlockingQueue<EndpointConnection> connectionQueue : connectionQueueMap.values()) { final List<EndpointConnection> connections = new ArrayList<>(); EndpointConnection connection; while ((connection = connectionQueue.poll()) != null) { // If the socket has not been used in 10 seconds, shut it down. final long lastUsed = connection.getLastTimeUsed(); if (lastUsed < System.currentTimeMillis() - idleExpirationMillis) { try { connection.getSocketClientProtocol().shutdown(connection.getPeer()); } catch (final Exception e) { logger.debug("Failed to shut down {} using {} due to {}", connection.getSocketClientProtocol(), connection.getPeer(), e); } terminate(connection); } else { connections.add(connection); } } connectionQueue.addAll(connections); } }
Example #2
Source File: OffsetMapQueueSinkTest.java From beast with Apache License 2.0 | 6 votes |
@Test public void shouldPushMessageToQueue() throws InterruptedException { BlockingQueue<Map<TopicPartition, OffsetAndMetadata>> queue = new LinkedBlockingQueue<>(); queueSink = new OffsetMapQueueSink(queue, queueConfig); Records messages = new Records(Collections.singletonList(new Record(offsetInfo, new HashMap<>()))); Status status = queueSink.push(messages); assertTrue(status.isSuccess()); assertEquals(1, queue.size()); Map<TopicPartition, OffsetAndMetadata> partitionsCommitOffset = queue.take(); assertEquals(1, partitionsCommitOffset.size()); Map.Entry<TopicPartition, OffsetAndMetadata> offset = partitionsCommitOffset.entrySet().iterator().next(); assertEquals(offset.getKey().topic(), "default-topic"); assertEquals(offset.getKey().partition(), 0); assertEquals(offset.getValue().offset(), 1); }
Example #3
Source File: SocketChannelDispatcher.java From nifi with Apache License 2.0 | 6 votes |
public SocketChannelDispatcher(final EventFactory<E> eventFactory, final ChannelHandlerFactory<E, AsyncChannelDispatcher> handlerFactory, final BlockingQueue<ByteBuffer> bufferPool, final BlockingQueue<E> events, final ComponentLog logger, final int maxConnections, final SSLContext sslContext, final SslContextFactory.ClientAuth clientAuth, final Charset charset) { this.eventFactory = eventFactory; this.handlerFactory = handlerFactory; this.bufferPool = bufferPool; this.events = events; this.logger = logger; this.maxConnections = maxConnections; this.keyQueue = new LinkedBlockingQueue<>(maxConnections); this.sslContext = sslContext; this.clientAuth = clientAuth; this.charset = charset; if (bufferPool == null || bufferPool.size() == 0 || bufferPool.size() != maxConnections) { throw new IllegalArgumentException( "A pool of available ByteBuffers equal to the maximum number of connections is required"); } }
Example #4
Source File: ConnectionTest.java From actioncable-client-java with MIT License | 6 votes |
@Test(timeout = TIMEOUT) public void shouldFireOnFailureWhenInternalServerErrorReceived() throws InterruptedException, IOException { final MockWebServer mockWebServer = new MockWebServer(); final MockResponse response = new MockResponse(); response.setResponseCode(500); response.setStatus("HTTP/1.1 500 Internal Server Error"); mockWebServer.enqueue(response); mockWebServer.start(); final URI uri = mockWebServer.url("/").uri(); final Connection connection = new Connection(uri, new Consumer.Options()); final BlockingQueue<String> events = new LinkedBlockingQueue<String>(); connection.setListener(new DefaultConnectionListener() { @Override public void onFailure(Exception e) { events.offer("onFailed"); } }); connection.open(); assertThat(events.take(), is("onFailed")); mockWebServer.shutdown(); }
Example #5
Source File: PushPullBlockingQueueTest.java From disruptor with Apache License 2.0 | 6 votes |
@Test public void testPeek() { final int cap = 10; BlockingQueue<Integer> dbq = new PushPullBlockingQueue<Integer>(cap); try { Assert.assertNull(dbq.peek()); } catch(NoSuchElementException nsex) { Assert.fail(); } for(int i=0; i<cap; i++) { dbq.offer(Integer.valueOf(i)); Assert.assertEquals(Integer.valueOf(0), dbq.peek()); } for(int i=0; i<cap; i++) { Assert.assertEquals(Integer.valueOf(i), dbq.peek()); dbq.poll(); // count up values checking peeks } }
Example #6
Source File: CouchbaseWriterTest.java From incubator-gobblin with Apache License 2.0 | 6 votes |
private void drainQueue(BlockingQueue<Pair<AbstractDocument, Future>> queue, int threshold, long sleepTime, TimeUnit sleepUnit, List<Pair<AbstractDocument, Future>> failedFutures) { while (queue.remainingCapacity() < threshold) { if (sleepTime > 0) { Pair<AbstractDocument, Future> topElement = queue.peek(); if (topElement != null) { try { topElement.getSecond().get(sleepTime, sleepUnit); } catch (Exception te) { failedFutures.add(topElement); } queue.poll(); } } } }
Example #7
Source File: PushPullBlockingQueueTest.java From disruptor with Apache License 2.0 | 6 votes |
@Test public void testOffer() { final int cap = 16; BlockingQueue<Integer> dbq = new PushPullBlockingQueue<Integer>(cap); for(int i=0; i<cap; i++) { dbq.offer(Integer.valueOf(i)); } Assert.assertFalse(dbq.offer(Integer.valueOf(cap))); for(int i=0; i<cap; i++) { Assert.assertEquals(Integer.valueOf(i), dbq.poll()); } }
Example #8
Source File: IpRules.java From xio with Apache License 2.0 | 6 votes |
public Result remove(IpRule ipRule, BlockingQueue<UpdateMessage> workLoad) { try { InetAddress address = InetAddress.getByAddress(ipRule.getIpAddress()); log.debug("address {}", address.getHostAddress()); if (!rules.containsKey(address)) { return new Result(false, "nothing to remove for address " + address.getHostAddress()); } else { workLoad.put(UpdateMessage.removeIpRule(address)); rules.remove(address); } } catch (UnknownHostException | InterruptedException e) { log.error("addIpRule couldn't add {}", ipRule, e); return new Result(false, e.getMessage()); } return new Result(true, ""); }
Example #9
Source File: AccountResource.java From nuls with MIT License | 6 votes |
@POST @Path("/lock/{address}") @Produces(MediaType.APPLICATION_JSON) @ApiOperation(value = "[锁账户] 清除缓存的锁定账户", notes = "Clear the cache unlock account.") public RpcClientResult lock(@ApiParam(name = "address", value = "账户地址", required = true) @PathParam("address") String address) { Account account = accountService.getAccount(address).getData(); if (null == account) { return Result.getFailed(AccountErrorCode.ACCOUNT_NOT_EXIST).toRpcClientResult(); } accountCacheService.removeAccount(account.getAddress()); BlockingQueue<Runnable> queue = scheduler.getQueue(); String addr = account.getAddress().toString(); Runnable scheduledFuture = (Runnable) accountUnlockSchedulerMap.get(addr); if (queue.contains(scheduledFuture)) { scheduler.remove(scheduledFuture); accountUnlockSchedulerMap.remove(addr); } Map<String, Boolean> map = new HashMap<>(); map.put("value", true); return Result.getSuccess().setData(map).toRpcClientResult(); }
Example #10
Source File: ListenSyslog.java From localization_nifi with Apache License 2.0 | 6 votes |
protected ChannelDispatcher createChannelReader(final String protocol, final BlockingQueue<ByteBuffer> bufferPool, final BlockingQueue<RawSyslogEvent> events, final int maxConnections, final SSLContextService sslContextService, final Charset charset) throws IOException { final EventFactory<RawSyslogEvent> eventFactory = new RawSyslogEventFactory(); if (UDP_VALUE.getValue().equals(protocol)) { return new DatagramChannelDispatcher(eventFactory, bufferPool, events, getLogger()); } else { // if an SSLContextService was provided then create an SSLContext to pass down to the dispatcher SSLContext sslContext = null; if (sslContextService != null) { sslContext = sslContextService.createSSLContext(SSLContextService.ClientAuth.REQUIRED); } final ChannelHandlerFactory<RawSyslogEvent<SocketChannel>, AsyncChannelDispatcher> handlerFactory = new SocketChannelHandlerFactory<>(); return new SocketChannelDispatcher(eventFactory, handlerFactory, bufferPool, events, getLogger(), maxConnections, sslContext, charset); } }
Example #11
Source File: BinlogWorker.java From binlake with Apache License 2.0 | 6 votes |
/** * 打印binlog 位置日志 * * @param logPositions */ private void debugLogPosition(ConcurrentLinkedQueue<LogPosition> logPositions) { if (LogUtils.debug.isDebugEnabled()) { Iterator<LogPosition> liter = logPositions.iterator(); boolean isHead = true; int count = 0; while (liter.hasNext()) { LogPosition lp = liter.next(); if (isHead) { LogUtils.debug.debug(host + " truncLogPosQueue logPositions head is " + lp); isHead = false; } count++; } LogUtils.debug.debug(host + " truncLogPosQueue logPositions queue size " + count); BlockingQueue<Object> queue = this.throttler; if (queue != null) { LogUtils.debug.debug(host + " throttler queue size " + queue.size()); } } }
Example #12
Source File: StreamingAllSharingExecutor2.java From twister2 with Apache License 2.0 | 6 votes |
private CommunicationWorker[] scheduleWaitFor(Map<Integer, INodeInstance> nodes) { BlockingQueue<INodeInstance> tasks; tasks = new ArrayBlockingQueue<>(nodes.size() * 2); tasks.addAll(nodes.values()); CommunicationWorker[] workers = new CommunicationWorker[numThreads]; workers[0] = new CommunicationWorker(tasks); doneSignal = new CountDownLatch(numThreads - 1); for (int i = 1; i < numThreads; i++) { workers[i] = new CommunicationWorker(tasks); threads.submit(workers[i]); } return workers; }
Example #13
Source File: BaseConfiguration.java From simple-robot-core with Apache License 2.0 | 6 votes |
/** * 获取线程池的阻塞队列 */ public BlockingQueue<Runnable> getWorkQueue() { if (this.workQueue != null) { return this.workQueue; } else { if (this.workQueueFrom == null) { return null; } else { try { Class<?> clz = Class.forName(workQueueFrom); Object instance = clz.newInstance(); this.workQueue = (BlockingQueue<Runnable>) instance; return this.workQueue; } catch (Exception e) { throw new ConfigurationException("无法读取包路径'" + workQueueFrom + "'来作为'" + BlockingQueue.class + "'实例。", e); } } } }
Example #14
Source File: EndpointConnectionPool.java From localization_nifi with Apache License 2.0 | 6 votes |
public void shutdown() { shutdown = true; taskExecutor.shutdown(); peerSelector.clear(); for (final EndpointConnection conn : activeConnections) { conn.getPeer().getCommunicationsSession().interrupt(); } for (final BlockingQueue<EndpointConnection> connectionQueue : connectionQueueMap.values()) { EndpointConnection state; while ((state = connectionQueue.poll()) != null) { terminate(state); } } }
Example #15
Source File: DaemonSchedulerTest.java From lutece-core with BSD 3-Clause "New" or "Revised" License | 6 votes |
private void testScheduleDelay( boolean shouldThrow ) throws ClassNotFoundException, InstantiationException, IllegalAccessException, InterruptedException, BrokenBarrierException, TimeoutException { BlockingQueue<DaemonEntry> queue = new LinkedBlockingQueue<>( ); ExecutorService executor = Executors.newSingleThreadExecutor( ); DaemonScheduler scheduler = new DaemonScheduler( queue, executor ); try { DaemonEntry entry = getDaemonEntry( "JUNIT" ); TestDaemon testDaemon = (TestDaemon) entry.getDaemon( ); testDaemon.setRunThrows( shouldThrow ); Instant start = Instant.now( ); scheduler.schedule( entry, 500L, TimeUnit.MILLISECONDS ); assertFalse( testDaemon.hasRun( ) ); testDaemon.go( ); assertTrue( 500L <= Duration.between( start, Instant.now( ) ).toMillis( ) ); testDaemon.waitForCompletion( ); assertTrue( testDaemon.hasRun( ) ); } finally { scheduler.shutdown( ); } }
Example #16
Source File: MPMCBlockingQueueTest.java From disruptor with Apache License 2.0 | 6 votes |
@Test public void testPeek() { final int cap = 10; BlockingQueue<Integer> dbq = new MPMCBlockingQueue<>(cap); try { Assert.assertNull(dbq.peek()); } catch(NoSuchElementException nsex) { Assert.fail(); } for(int i=0; i<cap; i++) { dbq.offer(Integer.valueOf(i)); Assert.assertEquals(Integer.valueOf(0), dbq.peek()); } for(int i=0; i<cap; i++) { Assert.assertEquals(Integer.valueOf(i), dbq.peek()); dbq.poll(); // count up values checking peeks } }
Example #17
Source File: EventIndexTask.java From localization_nifi with Apache License 2.0 | 5 votes |
public EventIndexTask(final BlockingQueue<StoredDocument> documentQueue, final RepositoryConfiguration repoConfig, final IndexManager indexManager, final IndexDirectoryManager directoryManager, final int maxEventsPerCommit, final EventReporter eventReporter) { this.documentQueue = documentQueue; this.indexManager = indexManager; this.directoryManager = directoryManager; this.commitThreshold = maxEventsPerCommit; this.eventReporter = eventReporter; }
Example #18
Source File: BrokerFastFailureTest.java From rocketmq-read with Apache License 2.0 | 5 votes |
@Test public void testCleanExpiredRequestInQueue() throws Exception { BrokerFastFailure brokerFastFailure = new BrokerFastFailure(null); BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>(); brokerFastFailure.cleanExpiredRequestInQueue(queue, 1); assertThat(queue.size()).isZero(); //Normal Runnable Runnable runnable = new Runnable() { @Override public void run() { } }; queue.add(runnable); assertThat(queue.size()).isEqualTo(1); brokerFastFailure.cleanExpiredRequestInQueue(queue, 1); assertThat(queue.size()).isEqualTo(1); queue.clear(); //With expired request RequestTask expiredRequest = new RequestTask(runnable, null, null); queue.add(new FutureTaskExt<>(expiredRequest, null)); TimeUnit.MILLISECONDS.sleep(100); RequestTask requestTask = new RequestTask(runnable, null, null); queue.add(new FutureTaskExt<>(requestTask, null)); assertThat(queue.size()).isEqualTo(2); brokerFastFailure.cleanExpiredRequestInQueue(queue, 100); assertThat(queue.size()).isEqualTo(1); assertThat(((FutureTaskExt) queue.peek()).getRunnable()).isEqualTo(requestTask); }
Example #19
Source File: EsThreadPoolExecutor.java From crate with Apache License 2.0 | 5 votes |
@SuppressForbidden(reason = "properly rethrowing errors, see EsExecutors.rethrowErrors") EsThreadPoolExecutor(String name, int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, XRejectedExecutionHandler handler) { super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler); this.name = name; }
Example #20
Source File: DaemonScheduler.java From lutece-core with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Constructor * * @param queue * the queue where daemon execution requests are stored * @param executor * the executor service handling the execution of daemons */ public DaemonScheduler( BlockingQueue<DaemonEntry> queue, ExecutorService executor ) { _queue = queue; _executor = executor; _scheduledDaemonsTimer = new Timer( "Lutece-Daemons-Scheduled-Timer-Thread", true ); _executingDaemons = new HashMap<>( ); _scheduledDaemons = new HashMap<>( ); _coordinatorThread = new Thread( this, "Lutece-Daemons-Coordinator" ); _coordinatorThread.setDaemon( true ); _coordinatorThread.start( ); _bShuttingDown = false; }
Example #21
Source File: PooledExecutorWithDMStats.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * Create a new pool **/ public PooledExecutorWithDMStats(BlockingQueue<Runnable> q, int maxPoolSize, PoolStatHelper stats, ThreadFactory tf, int msTimeout, RejectedExecutionHandler reh) { super(getCorePoolSize(maxPoolSize), maxPoolSize, msTimeout, TimeUnit.MILLISECONDS, q, tf, reh); // if (getCorePoolSize() != 0 && getCorePoolSize() == getMaximumPoolSize()) { // allowCoreThreadTimeOut(true); // deadcoded for 1.5 // } this.stats = stats; }
Example #22
Source File: HDF5File.java From dawnsci with Eclipse Public License 1.0 | 5 votes |
/** * Finish all writes (block until it is done) */ public synchronized void flushWrites() { if (service != null) { BlockingQueue<Runnable> queue = service.getQueue(); final long milli = 10; // period to sleep between checking for empty queue while (!service.isTerminated() && queue.peek() != null) { try { Thread.sleep(milli); } catch (InterruptedException e) { } } } flushDatasets(); }
Example #23
Source File: SessionThreadPoolExecutor.java From sofa-registry with Apache License 2.0 | 5 votes |
public SessionThreadPoolExecutor(String executorName, int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) { this(executorName, corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, (r, executor) -> { String msg = String.format("Task(%s) %s rejected from %s, throw RejectedExecutionException.", r.getClass(), r, executor); LOGGER.error(msg); throw new RejectedExecutionException(msg); }); }
Example #24
Source File: SimulationWorkerTest.java From tlaplus with MIT License | 5 votes |
@Test public void testInvariantBadEval() throws Exception { ITool tool = new FastTool("", "BasicMultiTrace", "MCBadInvNonInitState", new SimpleFilenameToStream()); StateVec initStates = tool.getInitStates(); ILiveCheck liveCheck = new NoOpLiveCheck(tool, "BasicMultiTrace"); BlockingQueue<SimulationWorkerResult> resultQueue = new LinkedBlockingQueue<>(); SimulationWorker worker = new SimulationWorker(0, tool, initStates, resultQueue, 0, 100, 100, false, null, liveCheck, new LongAdder(), new LongAdder()); worker.start(); SimulationWorkerResult res = resultQueue.take(); assertTrue(res.isError()); SimulationWorkerError err = res.error(); assertEquals(EC.TLC_INVARIANT_EVALUATION_FAILED, err.errorCode); assertEquals(1, err.stateTrace.size()); // Check the generated trace. assertEquals("0", getStateVal(err.stateTrace.elementAt(0), "depth")); assertEquals("0", getStateVal(err.stateTrace.elementAt(0), "branch")); assertEquals("0", getStateVal(err.state, "depth")); assertEquals("1", getStateVal(err.state, "branch")); worker.join(); assertFalse(worker.isAlive()); }
Example #25
Source File: EJBHomeProxyHandle.java From tomee with Apache License 2.0 | 5 votes |
@Override public void writeExternal(final ObjectOutput out) throws IOException { // write out the version of the serialized data for future use out.writeByte(2); final boolean hasExec = handler.executor != null && handler.executor != JNDIContext.globalExecutor(); out.writeBoolean(hasExec); if (hasExec) { out.writeInt(handler.executor.getMaximumPoolSize()); final BlockingQueue<Runnable> queue = handler.executor.getQueue(); out.writeInt(queue.size() + queue.remainingCapacity()); } handler.client.setMetaData(metaData); handler.client.writeExternal(out); final EJBMetaDataImpl ejb = handler.ejb; out.writeObject(ejb.homeClass); out.writeObject(ejb.remoteClass); out.writeObject(ejb.keyClass); out.writeByte(ejb.type); out.writeUTF(ejb.deploymentID); out.writeShort(ejb.deploymentCode); handler.server.setMetaData(metaData); handler.server.writeExternal(out); /// out.writeObject( handler.primaryKey ); }
Example #26
Source File: MemoryQueueEventChannel.java From jstarcraft-core with Apache License 2.0 | 5 votes |
@Override public void registerMonitor(Set<Class> types, EventMonitor monitor) { for (Class type : types) { EventManager manager = managers.get(type); if (manager == null) { manager = new EventManager(); managers.put(type, manager); BlockingQueue<Object> events = getEvents(type); EventThread thread = new EventThread(manager, events); thread.start(); threads.put(type, thread); } manager.attachMonitor(monitor); } }
Example #27
Source File: DiscardedPolicy.java From Lottor with MIT License | 5 votes |
@Override public void rejectedExecution(Runnable runnable, ThreadPoolExecutor executor) { if (threadName != null) { LOG.error("txTransaction Thread pool [{}] is exhausted, executor={}", threadName, executor.toString()); } if (!executor.isShutdown()) { BlockingQueue<Runnable> queue = executor.getQueue(); int discardSize = queue.size() >> 1; for (int i = 0; i < discardSize; i++) { queue.poll(); } queue.offer(runnable); } }
Example #28
Source File: WatchManagerTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Test public void testWatchManagerResetFile() throws Exception { Assume.assumeTrue(!IS_WINDOWS || Boolean.getBoolean(FORCE_RUN_KEY)); final ConfigurationScheduler scheduler = new ConfigurationScheduler(); scheduler.incrementScheduledItems(); final WatchManager watchManager = new WatchManager(scheduler); watchManager.setIntervalSeconds(1); scheduler.start(); watchManager.start(); try { final File sourceFile = new File(originalFile); Path source = Paths.get(sourceFile.toURI()); try (final FileOutputStream targetStream = new FileOutputStream(testFile)) { Files.copy(source, targetStream); } final File updateFile = new File(newFile); final File targetFile = new File(testFile); final BlockingQueue<File> queue = new LinkedBlockingQueue<>(); watchManager.watchFile(targetFile, new TestWatcher(queue)); watchManager.stop(); Thread.sleep(1000); source = Paths.get(updateFile.toURI()); Files.copy(source, Paths.get(targetFile.toURI()), StandardCopyOption.REPLACE_EXISTING); watchManager.reset(targetFile); watchManager.start(); Thread.sleep(1000); final File f = queue.poll(1, TimeUnit.SECONDS); assertNull("File change detected", f); } finally { watchManager.stop(); scheduler.stop(); } }
Example #29
Source File: IdentityFactoryRaceConditionTest.java From openpojo with Apache License 2.0 | 5 votes |
ReportingThreadExecutionPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) { super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, new RejectedExecutionHandler() { public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { throw new RuntimeException("Rejected Task - " + r); } }); }
Example #30
Source File: AuditLogBootingSyslogTest.java From wildfly-core with GNU Lesser General Public License v2.1 | 5 votes |
private void waitForExpectedOperations(int expectedOperations, BlockingQueue<SyslogServerEventIF> queue) throws InterruptedException { int operations = 0; int openClose = 0; long endTime = System.currentTimeMillis() + TimeoutUtil.adjust(5000); do { if (queue.isEmpty()) { Thread.sleep(100); } while (!queue.isEmpty()) { SyslogServerEventIF event = queue.take(); char[] messageChars = event.getMessage().toCharArray(); for (char character : messageChars) { if (character == '{' || character == '}') { if (character == '{') { openClose++; } else { openClose--; } Assert.assertTrue(openClose >= 0); if (openClose == 0) operations++; } } } if (operations >= expectedOperations) { break; } } while (System.currentTimeMillis() < endTime); Assert.assertEquals(expectedOperations, operations); }