Search in sources :

Example 16 with ScheduledFuture

use of java.util.concurrent.ScheduledFuture in project spring-framework by spring-projects.

the class WebSocketStompClientTests method cancelInactivityTasks.

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void cancelInactivityTasks() throws Exception {
    TcpConnection<byte[]> tcpConnection = getTcpConnection();
    ScheduledFuture future = mock(ScheduledFuture.class);
    when(this.taskScheduler.scheduleWithFixedDelay(any(), eq(1L))).thenReturn(future);
    tcpConnection.onReadInactivity(mock(Runnable.class), 2L);
    tcpConnection.onWriteInactivity(mock(Runnable.class), 2L);
    this.webSocketHandlerCaptor.getValue().afterConnectionClosed(this.webSocketSession, CloseStatus.NORMAL);
    verify(future, times(2)).cancel(true);
    verifyNoMoreInteractions(future);
}
Also used : ScheduledFuture(java.util.concurrent.ScheduledFuture) Test(org.junit.Test)

Example 17 with ScheduledFuture

use of java.util.concurrent.ScheduledFuture in project otter by alibaba.

the class MonitorScheduler method register.

/**
     * 注册对应的Monitor对象
     * 
     * @param monitor
     * @param period 调度周期,单位ms
     */
public static void register(final Monitor monitor, Long delay, Long period) {
    ScheduledFuture future = scheduler.scheduleAtFixedRate(new Runnable() {

        public void run() {
            monitor.reload();
        }
    }, delay, period, TimeUnit.MILLISECONDS);
    register.put(monitor, future);
}
Also used : ScheduledFuture(java.util.concurrent.ScheduledFuture)

Example 18 with ScheduledFuture

use of java.util.concurrent.ScheduledFuture in project cassandra by apache.

the class StreamTransferTask method complete.

/**
     * Received ACK for file at {@code sequenceNumber}.
     *
     * @param sequenceNumber sequence number of file
     */
public void complete(int sequenceNumber) {
    boolean signalComplete;
    synchronized (this) {
        ScheduledFuture timeout = timeoutTasks.remove(sequenceNumber);
        if (timeout != null)
            timeout.cancel(false);
        OutgoingFileMessage file = files.remove(sequenceNumber);
        if (file != null)
            file.complete();
        signalComplete = files.isEmpty();
    }
    // all file sent, notify session this task is complete.
    if (signalComplete)
        session.taskCompleted(this);
}
Also used : OutgoingFileMessage(org.apache.cassandra.streaming.messages.OutgoingFileMessage) ScheduledFuture(java.util.concurrent.ScheduledFuture)

Example 19 with ScheduledFuture

use of java.util.concurrent.ScheduledFuture in project commons by twitter.

the class LowResClockTest method testLowResClock.

@Test
public void testLowResClock() {
    final WaitingFakeClock clock = new WaitingFakeClock();
    final long start = clock.nowMillis();
    ScheduledExecutorService mockExecutor = createMock(ScheduledExecutorService.class);
    final Capture<Runnable> runnable = new Capture<Runnable>();
    final Capture<Long> period = new Capture<Long>();
    mockExecutor.scheduleAtFixedRate(capture(runnable), anyLong(), captureLong(period), eq(TimeUnit.MILLISECONDS));
    expectLastCall().andAnswer(new IAnswer<ScheduledFuture<?>>() {

        public ScheduledFuture<?> answer() {
            final Thread ticker = new Thread() {

                @Override
                public void run() {
                    Tick tick = new Tick(clock, start, period.getValue(), runnable.getValue());
                    try {
                        while (true) {
                            clock.doOnAdvance(tick);
                        }
                    } catch (InterruptedException e) {
                    /* terminate */
                    }
                }
            };
            ticker.setDaemon(true);
            ticker.start();
            final ScheduledFuture<?> future = createMock(ScheduledFuture.class);
            final AtomicBoolean stopped = new AtomicBoolean(false);
            expect(future.isCancelled()).andAnswer(new IAnswer<Boolean>() {

                @Override
                public Boolean answer() throws Throwable {
                    return stopped.get();
                }
            }).anyTimes();
            expect(future.cancel(anyBoolean())).andAnswer(new IAnswer<Boolean>() {

                @Override
                public Boolean answer() throws Throwable {
                    ticker.interrupt();
                    stopped.set(true);
                    return true;
                }
            });
            replay(future);
            return future;
        }
    });
    replay(mockExecutor);
    LowResClock lowRes = new LowResClock(Amount.of(1L, Time.SECONDS), mockExecutor, clock);
    long t = lowRes.nowMillis();
    clock.advance(Amount.of(100L, Time.MILLISECONDS));
    assertEquals(t, lowRes.nowMillis());
    clock.advance(Amount.of(900L, Time.MILLISECONDS));
    assertEquals(t + 1000, lowRes.nowMillis());
    clock.advance(Amount.of(100L, Time.MILLISECONDS));
    assertEquals(t + 1000, lowRes.nowMillis());
    lowRes.close();
    try {
        lowRes.nowMillis();
        fail("Closed clock should throw exception!");
    } catch (IllegalStateException e) {
    /* expected */
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Capture(org.easymock.Capture) ScheduledFuture(java.util.concurrent.ScheduledFuture) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IAnswer(org.easymock.IAnswer) EasyMock.captureLong(org.easymock.EasyMock.captureLong) EasyMock.anyLong(org.easymock.EasyMock.anyLong) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) EasyMock.anyBoolean(org.easymock.EasyMock.anyBoolean) Test(org.junit.Test)

Example 20 with ScheduledFuture

use of java.util.concurrent.ScheduledFuture in project hazelcast by hazelcast.

the class DefaultNearCacheManager method destroyAllNearCaches.

@Override
public void destroyAllNearCaches() {
    for (NearCache nearCache : new HashSet<NearCache>(nearCacheMap.values())) {
        nearCacheMap.remove(nearCache.getName());
        nearCache.destroy();
    }
    for (ScheduledFuture preloadTaskFuture : preloadTaskFutures) {
        preloadTaskFuture.cancel(true);
    }
    if (storageTaskFuture != null) {
        storageTaskFuture.cancel(true);
    }
}
Also used : NearCache(com.hazelcast.internal.nearcache.NearCache) ScheduledFuture(java.util.concurrent.ScheduledFuture) HashSet(java.util.HashSet)

Aggregations

ScheduledFuture (java.util.concurrent.ScheduledFuture)83 Test (org.junit.Test)27 DelegatingScheduledFutureStripper (com.hazelcast.scheduledexecutor.impl.DelegatingScheduledFutureStripper)9 ParallelTest (com.hazelcast.test.annotation.ParallelTest)9 QuickTest (com.hazelcast.test.annotation.QuickTest)9 Date (java.util.Date)9 IOException (java.io.IOException)7 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)7 Map (java.util.Map)6 TimeValue (org.elasticsearch.common.unit.TimeValue)6 None (com.linkedin.common.util.None)5 D2Client (com.linkedin.d2.balancer.D2Client)5 D2ClientBuilder (com.linkedin.d2.balancer.D2ClientBuilder)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 TestThreadPool (org.elasticsearch.threadpool.TestThreadPool)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 JSONObject (org.json.simple.JSONObject)5 IdleReaderException (com.twitter.distributedlog.exceptions.IdleReaderException)4 ExecutionException (java.util.concurrent.ExecutionException)4 Future (java.util.concurrent.Future)4