本文整理汇总了Java中io.netty.util.TimerTask类的典型用法代码示例。如果您正苦于以下问题:Java TimerTask类的具体用法?Java TimerTask怎么用?Java TimerTask使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
TimerTask类属于io.netty.util包,在下文中一共展示了TimerTask类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: awaitResultAsync
import io.netty.util.TimerTask; //导入依赖的package包/类
@Override
protected void awaitResultAsync(final RemoteInvocationOptions optionsCopy, final RemotePromise<Object> result,
final RemoteServiceRequest request, final String responseName) {
if (!optionsCopy.isResultExpected()) {
return;
}
Long startTime = 0L;
if (request != null && request.getArgs() != null && request.getArgs().length > 3) {
startTime = (Long)request.getArgs()[3];
}
long delay = startTime - System.currentTimeMillis();
if (delay > 0) {
commandExecutor.getConnectionManager().newTimeout(new TimerTask() {
@Override
public void run(Timeout timeout) throws Exception {
ScheduledTasksService.super.awaitResultAsync(optionsCopy, result, request, responseName);
}
}, delay, TimeUnit.MILLISECONDS);
} else {
super.awaitResultAsync(optionsCopy, result, request, responseName);
}
}
示例2: scheduleReconnect
import io.netty.util.TimerTask; //导入依赖的package包/类
private synchronized void scheduleReconnect()
{
if (_stop)
return;
if (_timeout != null)
return;
Constants.ahessianLogger.warn("channel closed wait to reconnect ...");
_retryCounter++;
long retryIntervall = Math.min(RECONNECT_DELAY * _retryCounter,
MAX_RECONNECT_DELAY);
_timeout = _timer.newTimeout(new TimerTask()
{
public void run(Timeout timeout) throws Exception
{
_timeout = null;
connect(_bootstrap.getBootstrap());
}
}, retryIntervall, TimeUnit.MILLISECONDS);
}
示例3: HeartbeatHandlerInbound
import io.netty.util.TimerTask; //导入依赖的package包/类
public HeartbeatHandlerInbound(final String name, final Timer timer,
final long timeout)
{
_name = name;
final TimerTask task = new TimerTask()
{
public void run(Timeout nTimeout) throws Exception
{
if (((getLastCalled() + timeout) <= System.currentTimeMillis())
&& isConnected())
try
{
_action.timedOut(_ctx);
}
catch (Exception e)
{
Constants.ahessianLogger.warn("", e);
}
}
};
_intervalTimer = new IntervalTimer(timer, task, timeout);
}
示例4: start
import io.netty.util.TimerTask; //导入依赖的package包/类
public void start(PulsarClientImpl client, ConsumerBase consumerBase, long ackTimeoutMillis) {
this.stop();
timeout = client.timer().newTimeout(new TimerTask() {
@Override
public void run(Timeout t) throws Exception {
if (isAckTimeout()) {
log.warn("[{}] {} messages have timed-out", consumerBase, oldOpenSet.size());
Set<MessageIdImpl> messageIds = new HashSet<>();
oldOpenSet.forEach(messageIds::add);
oldOpenSet.clear();
consumerBase.redeliverUnacknowledgedMessages(messageIds);
}
toggle();
timeout = client.timer().newTimeout(this, ackTimeoutMillis, TimeUnit.MILLISECONDS);
}
}, ackTimeoutMillis, TimeUnit.MILLISECONDS);
}
示例5: newTimeout
import io.netty.util.TimerTask; //导入依赖的package包/类
/**
* {@inheritDoc}
* @see io.netty.util.Timer#newTimeout(io.netty.util.TimerTask, long, java.util.concurrent.TimeUnit)
*/
@Override
public Timeout newTimeout(final TimerTask task, final long delay, final TimeUnit unit) {
final WrappedTimeout[] t = new WrappedTimeout[1];
t[0] = new WrappedTimeout(timer.newTimeout(new TimerTask(){
@Override
public void run(final Timeout timeout) throws Exception {
try {
task.run(t[0]);
} finally {
pendingTimeouts.decrementAndGet();
timeouts.increment();
}
}
}, delay, unit));
return t[0];
}
示例6: TimedDeferredRequest
import io.netty.util.TimerTask; //导入依赖的package包/类
/**
* Intentional private local constructor
* @param key the request key
* @param request the request object
* @param window the window
* @param timeoutMillis the time after which this future will be cancelled
* @param timer the timer used to implement the timeout functionality
*/
private TimedDeferredRequest(final K key,
final R request,
final Window<K, R, D> window,
final Timer timer,
final long timeoutMillis) {
super(key, request, window);
this.timeout = checkNotNull(timer).newTimeout(new TimerTask() {
@Override
public void run(Timeout timerTask) throws Exception {
window.fail(checkNotNull(key),
new TimeoutException("The operation timed out (Window full)"));
}
},
timeoutMillis,
TimeUnit.MILLISECONDS);
}
示例7: afterTimeoutFutureIsCancelled
import io.netty.util.TimerTask; //导入依赖的package包/类
@Test
public void afterTimeoutFutureIsCancelled() throws Exception {
when(timer.newTimeout(any(TimerTask.class), anyInt(), eq(TimeUnit.MILLISECONDS)))
.thenAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
invocationOnMock.getArgumentAt(0, TimerTask.class).run(null);
return null;
}
});
DeferredRequest<Integer, String, Boolean> deferredRequest = TimedDeferredRequest.create(5, "request", window, timer, 10000);
verify(window).fail(eq(5), any(TimeoutException.class));
}
示例8: newRefreshTask
import io.netty.util.TimerTask; //导入依赖的package包/类
private void newRefreshTask() {
if (refreshTaskMap.containsKey(getName())) {
return;
}
Timeout task = commandExecutor.getConnectionManager().newTimeout(new TimerTask() {
@Override
public void run(Timeout timeout) throws Exception {
expire(internalLockLeaseTime, TimeUnit.MILLISECONDS);
refreshTaskMap.remove(getName());
newRefreshTask(); // reschedule itself
}
}, internalLockLeaseTime / 3, TimeUnit.MILLISECONDS);
if (refreshTaskMap.putIfAbsent(getName(), task) != null) {
task.cancel();
}
}
示例9: fireNext
import io.netty.util.TimerTask; //导入依赖的package包/类
public void fireNext(final ChannelHandlerContext ctx, final long delay) {
final Event readyForNext = new Event(currentConversationId);
if(delay > timerTickSize) {
timer.newTimeout(new TimerTask() {
@Override public void run(Timeout timeout) {
if(logger.isDebugEnabled()) {
logger.debug("running after delay: {}", delay);
}
if(readyForNext.conversationId != currentConversationId) {
logger.debug("pending 'next' event found obsolete, aborting");
return;
}
ctx.pipeline().fireChannelRead(readyForNext);
}
}, delay, TimeUnit.MILLISECONDS);
} else {
ctx.pipeline().fireChannelRead(readyForNext);
}
}
示例10: newTimeout
import io.netty.util.TimerTask; //导入依赖的package包/类
@Override
public Timeout newTimeout(TimerTask task, long delay, TimeUnit unit) {
try {
return timer.newTimeout(task, delay, unit);
} catch (IllegalStateException e) {
// timer is shutdown
return dummyTimeout;
}
}
示例11: setUp
import io.netty.util.TimerTask; //导入依赖的package包/类
@Before
public void setUp() throws Exception {
/*
* This needs to be called explicitly to ensure the featuresReply is not null.
* Otherwise, there is no guarantee @Before will for setUpFeaturesReply() will
* call that function before our @Before setUp() here.
*/
setUpFeaturesReply();
switchManager = createMock(IOFSwitchManager.class);
roleManager = createMock(RoleManager.class);
sw = createMock(IOFSwitchBackend.class);
timer = createMock(Timer.class);
expect(timer.newTimeout(anyObject(TimerTask.class), anyLong(), anyObject(TimeUnit.class))).andReturn(EasyMock.createNiceMock(Timeout.class));
replay(timer);
seenXids = null;
// TODO: should mock IDebugCounterService and make sure
// the expected counters are updated.
debugCounterService = new DebugCounterServiceImpl();
SwitchManagerCounters counters =
new SwitchManagerCounters(debugCounterService);
expect(switchManager.getCounters()).andReturn(counters).anyTimes();
replay(switchManager);
connection = new MockOFConnection(featuresReply.getDatapathId(), OFAuxId.MAIN);
switchHandler = new OFSwitchHandshakeHandler(connection, featuresReply, switchManager, roleManager, timer);
// replay sw. Reset it if you need more specific behavior
replay(sw);
}
示例12: getSaslHandler
import io.netty.util.TimerTask; //导入依赖的package包/类
/**
* Get SASL handler
* @param bootstrap to reconnect to
* @return new SASL handler
* @throws java.io.IOException if handler failed to create
*/
private SaslClientHandler getSaslHandler(final UserGroupInformation realTicket,
final Bootstrap bootstrap) throws IOException {
return new SaslClientHandler(realTicket, authMethod, token, serverPrincipal,
client.fallbackAllowed, client.conf.get("hbase.rpc.protection",
SaslUtil.QualityOfProtection.AUTHENTICATION.name().toLowerCase()),
new SaslClientHandler.SaslExceptionHandler() {
@Override
public void handle(int retryCount, Random random, Throwable cause) {
try {
// Handle Sasl failure. Try to potentially get new credentials
handleSaslConnectionFailure(retryCount, cause, realTicket);
// Try to reconnect
client.newTimeout(new TimerTask() {
@Override
public void run(Timeout timeout) throws Exception {
connect(bootstrap);
}
}, random.nextInt(reloginMaxBackoff) + 1, TimeUnit.MILLISECONDS);
} catch (IOException | InterruptedException e) {
close(e);
}
}
}, new SaslClientHandler.SaslSuccessfulConnectHandler() {
@Override
public void onSuccess(Channel channel) {
startHBaseConnection(channel);
}
});
}
示例13: retryOrClose
import io.netty.util.TimerTask; //导入依赖的package包/类
/**
* Retry to connect or close
*
* @param bootstrap to connect with
* @param connectCounter amount of tries
* @param e exception of fail
*/
private void retryOrClose(final Bootstrap bootstrap, int connectCounter, Throwable e) {
if (connectCounter < client.maxRetries) {
client.newTimeout(new TimerTask() {
@Override public void run(Timeout timeout) throws Exception {
connect(bootstrap);
}
}, client.failureSleep, TimeUnit.MILLISECONDS);
} else {
client.failedServers.addToFailedServers(address);
close(e);
}
}
示例14: waitForCallbackResult
import io.netty.util.TimerTask; //导入依赖的package包/类
private Object waitForCallbackResult(final Long id,
final HessianProxyFuture future)
{
long timeout = 10000;
if (timeout > 0)
{
TimerTask task = new TimerTask()
{
public void run(Timeout arg0) throws Exception
{
_openCallbackCalls.remove(id);
future.timedOut();
}
};
future.setTimeout(_timer.newTimeout(task, timeout,
TimeUnit.MILLISECONDS));
}
try
{
return future.getCallbackResult();
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
示例15: channelInactive
import io.netty.util.TimerTask; //导入依赖的package包/类
@Override
public void channelInactive(final ChannelHandlerContext ctx)
throws Exception
{
_hasSession = false;
ctx.channel().attr(SESSION).get().close();
final String sessionId = ctx.channel().attr(SESSION).get().getId();
Constants.ahessianLogger.info("Session disconnected: " + sessionId);
_sessionId = "";
_channel = null;
// remove the session if the client does not reconnect within timeout
if (_sessionTimeout > 0)
{
Timeout timeOut = _timer.newTimeout(new TimerTask()
{
public void run(Timeout arg0) throws Exception
{
ctx.channel().attr(SESSION).get().invalidate();
_factory.removeSession(sessionId);
_sessionPipelines.remove(sessionId);
_valid = false;
Constants.ahessianLogger.warn(ctx.channel()
+ " session timed out: " + sessionId);
}
}, _sessionTimeout, TimeUnit.MILLISECONDS);
ctx.channel().attr(SESSION).get().setTimeOut(timeOut);
}
ctx.fireChannelInactive();
}