use of java.util.concurrent.LinkedBlockingDeque in project spring-data-mongodb by spring-projects.
the class ReactiveMongoRepositoryTests method shouldUseTailableCursor.
// DATAMONGO-1444
@Test
public void shouldUseTailableCursor() throws Exception {
StepVerifier.create(//
template.dropCollection(Capped.class).then(//
template.createCollection(//
Capped.class, //
CollectionOptions.empty().size(1000).maxDocuments(100).capped()))).expectNextCount(//
1).verifyComplete();
StepVerifier.create(template.insert(new Capped("value", Math.random()))).expectNextCount(1).verifyComplete();
BlockingQueue<Capped> documents = new LinkedBlockingDeque<>(100);
Disposable disposable = cappedRepository.findByKey("value").doOnNext(documents::add).subscribe();
assertThat(documents.poll(5, TimeUnit.SECONDS), is(notNullValue()));
StepVerifier.create(template.insert(new Capped("value", Math.random()))).expectNextCount(1).verifyComplete();
assertThat(documents.poll(5, TimeUnit.SECONDS), is(notNullValue()));
assertThat(documents.isEmpty(), is(true));
disposable.dispose();
}
use of java.util.concurrent.LinkedBlockingDeque in project tomee by apache.
the class JNDIContext method newExecutor.
public static ThreadPoolExecutor newExecutor(final int threads, final BlockingQueue<Runnable> blockingQueue) {
/**
* This thread pool starts with 3 core threads and can grow to the limit defined by 'threads'.
* If a pool thread is idle for more than 1 minute it will be discarded, unless the core size is reached.
* It can accept upto the number of processes defined by 'queue'.
* If the queue is full then an attempt is made to add the process to the queue for 10 seconds.
* Failure to add to the queue in this time will either result in a logged rejection, or if 'block'
* is true then a final attempt is made to run the process in the current thread (the service thread).
*/
final ThreadPoolExecutor executorService = new ThreadPoolExecutor(3, (threads < 3 ? 3 : threads), 1, TimeUnit.MINUTES, blockingQueue == null ? new LinkedBlockingDeque<Runnable>(Integer.parseInt(getProperty(null, POOL_QUEUE_SIZE, "2"))) : blockingQueue);
executorService.setThreadFactory(new ThreadFactory() {
private final AtomicInteger i = new AtomicInteger(0);
@Override
public Thread newThread(final Runnable r) {
final Thread t = new Thread(r, "OpenEJB.Client." + i.incrementAndGet());
t.setDaemon(true);
t.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(final Thread t, final Throwable e) {
Logger.getLogger(EJBObjectHandler.class.getName()).log(Level.SEVERE, "Uncaught error in: " + t.getName(), e);
}
});
return t;
}
});
executorService.setRejectedExecutionHandler(new RejectedExecutionHandler() {
@Override
public void rejectedExecution(final Runnable r, final ThreadPoolExecutor tpe) {
if (null == r || null == tpe || tpe.isShutdown() || tpe.isTerminated() || tpe.isTerminating()) {
return;
}
final Logger log = Logger.getLogger(EJBObjectHandler.class.getName());
if (log.isLoggable(Level.WARNING)) {
log.log(Level.WARNING, "EJBObjectHandler ExecutorService at capicity for process: " + r);
}
boolean offer = false;
try {
offer = tpe.getQueue().offer(r, 10, TimeUnit.SECONDS);
} catch (InterruptedException e) {
// Ignore
}
if (!offer) {
log.log(Level.SEVERE, "EJBObjectHandler ExecutorService failed to run asynchronous process: " + r);
}
}
});
return executorService;
}
use of java.util.concurrent.LinkedBlockingDeque in project jetty.project by eclipse.
the class AsyncIOServletTest method testWriteListenerFromOtherThread.
@Test
public void testWriteListenerFromOtherThread() throws Exception {
start(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
AsyncContext asyncContext = request.startAsync();
asyncContext.setTimeout(0);
request.getInputStream().setReadListener(new Listener(asyncContext));
}
});
int cores = 4;
int iterations = 10;
CountDownLatch latch = new CountDownLatch(cores * iterations);
Deque<Throwable> failures = new LinkedBlockingDeque<>();
for (int i = 0; i < cores; ++i) {
client.getExecutor().execute(() -> {
for (int j = 0; j < iterations; ++j) {
try {
ContentResponse response = client.newRequest(newURI()).method(HttpMethod.POST).path(servletPath).content(new InputStreamContentProvider(new ByteArrayInputStream(new byte[16 * 1024]) {
@Override
public int read(byte[] b, int off, int len) {
sleep(5);
return super.read(b, off, Math.min(len, 4242));
}
})).send();
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
latch.countDown();
} catch (Throwable x) {
failures.offer(x);
}
}
});
}
Assert.assertTrue(latch.await(30, TimeUnit.SECONDS));
Assert.assertTrue(failures.isEmpty());
}
use of java.util.concurrent.LinkedBlockingDeque in project elasticsearch by elastic.
the class MockTransportService method addUnresponsiveRule.
/**
* Adds a rule that will cause ignores each send request, simulating an unresponsive node
* and failing to connect once the rule was added.
*
* @param duration the amount of time to delay sending and connecting.
*/
public void addUnresponsiveRule(TransportAddress transportAddress, final TimeValue duration) {
final long startTime = System.currentTimeMillis();
addDelegate(transportAddress, new ClearableTransport(original) {
private final Queue<Runnable> requestsToSendWhenCleared = new LinkedBlockingDeque<Runnable>();
private boolean cleared = false;
TimeValue getDelay() {
return new TimeValue(duration.millis() - (System.currentTimeMillis() - startTime));
}
@Override
public void connectToNode(DiscoveryNode node, ConnectionProfile connectionProfile, CheckedBiConsumer<Connection, ConnectionProfile, IOException> connectionValidator) throws ConnectTransportException {
if (original.nodeConnected(node)) {
// connecting to an already connected node is a no-op
return;
}
TimeValue delay = getDelay();
if (delay.millis() <= 0) {
original.connectToNode(node, connectionProfile, connectionValidator);
return;
}
// TODO: Replace with proper setting
TimeValue connectingTimeout = NetworkService.TcpSettings.TCP_CONNECT_TIMEOUT.getDefault(Settings.EMPTY);
try {
if (delay.millis() < connectingTimeout.millis()) {
Thread.sleep(delay.millis());
original.connectToNode(node, connectionProfile, connectionValidator);
} else {
Thread.sleep(connectingTimeout.millis());
throw new ConnectTransportException(node, "UNRESPONSIVE: simulated");
}
} catch (InterruptedException e) {
throw new ConnectTransportException(node, "UNRESPONSIVE: simulated");
}
}
@Override
protected void sendRequest(Connection connection, long requestId, String action, TransportRequest request, TransportRequestOptions options) throws IOException {
// delayed sending - even if larger then the request timeout to simulated a potential late response from target node
TimeValue delay = getDelay();
if (delay.millis() <= 0) {
connection.sendRequest(requestId, action, request, options);
return;
}
// poor mans request cloning...
RequestHandlerRegistry reg = MockTransportService.this.getRequestHandler(action);
BytesStreamOutput bStream = new BytesStreamOutput();
request.writeTo(bStream);
final TransportRequest clonedRequest = reg.newRequest();
clonedRequest.readFrom(bStream.bytes().streamInput());
Runnable runnable = new AbstractRunnable() {
AtomicBoolean requestSent = new AtomicBoolean();
@Override
public void onFailure(Exception e) {
logger.debug("failed to send delayed request", e);
}
@Override
protected void doRun() throws IOException {
if (requestSent.compareAndSet(false, true)) {
connection.sendRequest(requestId, action, clonedRequest, options);
}
}
};
// store the request to send it once the rule is cleared.
synchronized (this) {
if (cleared) {
runnable.run();
} else {
requestsToSendWhenCleared.add(runnable);
threadPool.schedule(delay, ThreadPool.Names.GENERIC, runnable);
}
}
}
@Override
public void clearRule() {
synchronized (this) {
assert cleared == false;
cleared = true;
requestsToSendWhenCleared.forEach(Runnable::run);
}
}
});
}
use of java.util.concurrent.LinkedBlockingDeque in project pinot by linkedin.
the class ScatterGatherPerfClient method setup.
private void setup() {
MetricsRegistry registry = new MetricsRegistry();
_timedExecutor = new ScheduledThreadPoolExecutor(1);
_service = new ThreadPoolExecutor(10, 10, 10, TimeUnit.DAYS, new LinkedBlockingDeque<Runnable>());
_eventLoopGroup = new NioEventLoopGroup(10);
_timer = new HashedWheelTimer();
NettyClientMetrics clientMetrics = new NettyClientMetrics(registry, "client_");
PooledNettyClientResourceManager rm = new PooledNettyClientResourceManager(_eventLoopGroup, _timer, clientMetrics);
_pool = new KeyedPoolImpl<ServerInstance, NettyClientConnection>(1, _maxActiveConnections, 300000, 10, rm, _timedExecutor, MoreExecutors.sameThreadExecutor(), registry);
rm.setPool(_pool);
_scatterGather = new ScatterGatherImpl(_pool, _service);
for (AsyncReader r : _readerThreads) {
r.start();
}
}
Aggregations