Implemented HashedWheelTimer as the default scheduling mechanism in Akka. Fixes #1291
This commit is contained in:
parent
b2d548bd0e
commit
896c906d03
27 changed files with 3320 additions and 134 deletions
2
.history
2
.history
|
|
@ -1,2 +1,4 @@
|
|||
update
|
||||
reload
|
||||
projects
|
||||
exit
|
||||
|
|
|
|||
|
|
@ -4,21 +4,21 @@ import org.scalatest.BeforeAndAfterEach
|
|||
import akka.testkit.TestEvent._
|
||||
import akka.testkit.EventFilter
|
||||
import org.multiverse.api.latches.StandardLatch
|
||||
import java.util.concurrent.{ ScheduledFuture, ConcurrentLinkedQueue, CountDownLatch, TimeUnit }
|
||||
import java.util.concurrent.{ ConcurrentLinkedQueue, CountDownLatch, TimeUnit }
|
||||
import akka.testkit.AkkaSpec
|
||||
import org.jboss.netty.akka.util.{ Timeout ⇒ TimeOut }
|
||||
|
||||
@org.junit.runner.RunWith(classOf[org.scalatest.junit.JUnitRunner])
|
||||
class SchedulerSpec extends AkkaSpec with BeforeAndAfterEach {
|
||||
private val futures = new ConcurrentLinkedQueue[ScheduledFuture[AnyRef]]()
|
||||
private val timeouts = new ConcurrentLinkedQueue[TimeOut]()
|
||||
|
||||
def collectFuture(f: ⇒ ScheduledFuture[AnyRef]): ScheduledFuture[AnyRef] = {
|
||||
val future = f
|
||||
futures.add(future)
|
||||
future
|
||||
def collectTimeout(t: TimeOut): TimeOut = {
|
||||
timeouts.add(t)
|
||||
t
|
||||
}
|
||||
|
||||
override def afterEach {
|
||||
while (futures.peek() ne null) { Option(futures.poll()).foreach(_.cancel(true)) }
|
||||
while (timeouts.peek() ne null) { Option(timeouts.poll()).foreach(_.cancel()) }
|
||||
}
|
||||
|
||||
"A Scheduler" must {
|
||||
|
|
@ -30,14 +30,14 @@ class SchedulerSpec extends AkkaSpec with BeforeAndAfterEach {
|
|||
def receive = { case Tick ⇒ countDownLatch.countDown() }
|
||||
})
|
||||
// run every 50 millisec
|
||||
collectFuture(app.scheduler.schedule(tickActor, Tick, 0, 50, TimeUnit.MILLISECONDS))
|
||||
collectTimeout(app.scheduler.schedule(tickActor, Tick, 0, 50, TimeUnit.MILLISECONDS))
|
||||
|
||||
// after max 1 second it should be executed at least the 3 times already
|
||||
assert(countDownLatch.await(1, TimeUnit.SECONDS))
|
||||
|
||||
val countDownLatch2 = new CountDownLatch(3)
|
||||
|
||||
collectFuture(app.scheduler.schedule(() ⇒ countDownLatch2.countDown(), 0, 50, TimeUnit.MILLISECONDS))
|
||||
collectTimeout(app.scheduler.schedule(() ⇒ countDownLatch2.countDown(), 0, 50, TimeUnit.MILLISECONDS))
|
||||
|
||||
// after max 1 second it should be executed at least the 3 times already
|
||||
assert(countDownLatch2.await(2, TimeUnit.SECONDS))
|
||||
|
|
@ -49,9 +49,10 @@ class SchedulerSpec extends AkkaSpec with BeforeAndAfterEach {
|
|||
val tickActor = actorOf(new Actor {
|
||||
def receive = { case Tick ⇒ countDownLatch.countDown() }
|
||||
})
|
||||
|
||||
// run every 50 millisec
|
||||
collectFuture(app.scheduler.scheduleOnce(tickActor, Tick, 50, TimeUnit.MILLISECONDS))
|
||||
collectFuture(app.scheduler.scheduleOnce(() ⇒ countDownLatch.countDown(), 50, TimeUnit.MILLISECONDS))
|
||||
collectTimeout(app.scheduler.scheduleOnce(tickActor, Tick, 50, TimeUnit.MILLISECONDS))
|
||||
collectTimeout(app.scheduler.scheduleOnce(() ⇒ countDownLatch.countDown(), 50, TimeUnit.MILLISECONDS))
|
||||
|
||||
// after 1 second the wait should fail
|
||||
assert(countDownLatch.await(2, TimeUnit.SECONDS) == false)
|
||||
|
|
@ -87,9 +88,10 @@ class SchedulerSpec extends AkkaSpec with BeforeAndAfterEach {
|
|||
})
|
||||
|
||||
(1 to 10).foreach { i ⇒
|
||||
val future = collectFuture(app.scheduler.scheduleOnce(actor, Ping, 1, TimeUnit.SECONDS))
|
||||
future.cancel(true)
|
||||
val timeout = collectTimeout(app.scheduler.scheduleOnce(actor, Ping, 1, TimeUnit.SECONDS))
|
||||
timeout.cancel()
|
||||
}
|
||||
|
||||
assert(ticks.await(3, TimeUnit.SECONDS) == false) //No counting down should've been made
|
||||
}
|
||||
|
||||
|
|
@ -114,9 +116,9 @@ class SchedulerSpec extends AkkaSpec with BeforeAndAfterEach {
|
|||
})
|
||||
val actor = (supervisor ? props).as[ActorRef].get
|
||||
|
||||
collectFuture(app.scheduler.schedule(actor, Ping, 500, 500, TimeUnit.MILLISECONDS))
|
||||
collectTimeout(app.scheduler.schedule(actor, Ping, 500, 500, TimeUnit.MILLISECONDS))
|
||||
// appx 2 pings before crash
|
||||
collectFuture(app.scheduler.scheduleOnce(actor, Crash, 1000, TimeUnit.MILLISECONDS))
|
||||
collectTimeout(app.scheduler.scheduleOnce(actor, Crash, 1000, TimeUnit.MILLISECONDS))
|
||||
|
||||
assert(restartLatch.tryAwait(2, TimeUnit.SECONDS))
|
||||
// should be enough time for the ping countdown to recover and reach 6 pings
|
||||
|
|
|
|||
|
|
@ -0,0 +1,88 @@
|
|||
/*
|
||||
* Copyright 2009 Red Hat, Inc.
|
||||
*
|
||||
* Red Hat licenses this file to you under the Apache License, version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.jboss.netty.akka.logging;
|
||||
|
||||
/**
|
||||
* A skeletal implementation of {@link InternalLogger}. This class implements
|
||||
* all methods that have a {@link InternalLogLevel} parameter by default to call
|
||||
* specific logger methods such as {@link #info(String)} or {@link #isInfoEnabled()}.
|
||||
*
|
||||
* @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
|
||||
* @author <a href="http://gleamynode.net/">Trustin Lee</a>
|
||||
* @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
|
||||
*/
|
||||
public abstract class AbstractInternalLogger implements InternalLogger {
|
||||
|
||||
/**
|
||||
* Creates a new instance.
|
||||
*/
|
||||
protected AbstractInternalLogger() {
|
||||
super();
|
||||
}
|
||||
|
||||
public boolean isEnabled(InternalLogLevel level) {
|
||||
switch (level) {
|
||||
case DEBUG:
|
||||
return isDebugEnabled();
|
||||
case INFO:
|
||||
return isInfoEnabled();
|
||||
case WARN:
|
||||
return isWarnEnabled();
|
||||
case ERROR:
|
||||
return isErrorEnabled();
|
||||
default:
|
||||
throw new Error();
|
||||
}
|
||||
}
|
||||
|
||||
public void log(InternalLogLevel level, String msg, Throwable cause) {
|
||||
switch (level) {
|
||||
case DEBUG:
|
||||
debug(msg, cause);
|
||||
break;
|
||||
case INFO:
|
||||
info(msg, cause);
|
||||
break;
|
||||
case WARN:
|
||||
warn(msg, cause);
|
||||
break;
|
||||
case ERROR:
|
||||
error(msg, cause);
|
||||
break;
|
||||
default:
|
||||
throw new Error();
|
||||
}
|
||||
}
|
||||
|
||||
public void log(InternalLogLevel level, String msg) {
|
||||
switch (level) {
|
||||
case DEBUG:
|
||||
debug(msg);
|
||||
break;
|
||||
case INFO:
|
||||
info(msg);
|
||||
break;
|
||||
case WARN:
|
||||
warn(msg);
|
||||
break;
|
||||
case ERROR:
|
||||
error(msg);
|
||||
break;
|
||||
default:
|
||||
throw new Error();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
* Copyright 2009 Red Hat, Inc.
|
||||
*
|
||||
* Red Hat licenses this file to you under the Apache License, version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.jboss.netty.akka.logging;
|
||||
|
||||
/**
|
||||
* The log level that {@link InternalLogger} can log at.
|
||||
*
|
||||
* @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
|
||||
* @author <a href="http://gleamynode.net/">Trustin Lee</a>
|
||||
* @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
|
||||
*/
|
||||
public enum InternalLogLevel {
|
||||
/**
|
||||
* 'DEBUG' log level.
|
||||
*/
|
||||
DEBUG,
|
||||
/**
|
||||
* 'INFO' log level.
|
||||
*/
|
||||
INFO,
|
||||
/**
|
||||
* 'WARN' log level.
|
||||
*/
|
||||
WARN,
|
||||
/**
|
||||
* 'ERROR' log level.
|
||||
*/
|
||||
ERROR;
|
||||
}
|
||||
|
|
@ -0,0 +1,102 @@
|
|||
/*
|
||||
* Copyright 2009 Red Hat, Inc.
|
||||
*
|
||||
* Red Hat licenses this file to you under the Apache License, version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.jboss.netty.akka.logging;
|
||||
|
||||
/**
|
||||
* <em>Internal-use-only</em> logger used by Netty. <strong>DO NOT</strong>
|
||||
* access this class outside of Netty.
|
||||
*
|
||||
* @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
|
||||
* @author <a href="http://gleamynode.net/">Trustin Lee</a>
|
||||
*
|
||||
* @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
|
||||
*/
|
||||
public interface InternalLogger {
|
||||
/**
|
||||
* Returns {@code true} if a DEBUG level message is logged.
|
||||
*/
|
||||
boolean isDebugEnabled();
|
||||
|
||||
/**
|
||||
* Returns {@code true} if an INFO level message is logged.
|
||||
*/
|
||||
boolean isInfoEnabled();
|
||||
|
||||
/**
|
||||
* Returns {@code true} if a WARN level message is logged.
|
||||
*/
|
||||
boolean isWarnEnabled();
|
||||
|
||||
/**
|
||||
* Returns {@code true} if an ERROR level message is logged.
|
||||
*/
|
||||
boolean isErrorEnabled();
|
||||
|
||||
/**
|
||||
* Returns {@code true} if the specified log level message is logged.
|
||||
*/
|
||||
boolean isEnabled(InternalLogLevel level);
|
||||
|
||||
/**
|
||||
* Logs a DEBUG level message.
|
||||
*/
|
||||
void debug(String msg);
|
||||
|
||||
/**
|
||||
* Logs a DEBUG level message.
|
||||
*/
|
||||
void debug(String msg, Throwable cause);
|
||||
|
||||
/**
|
||||
* Logs an INFO level message.
|
||||
*/
|
||||
void info(String msg);
|
||||
|
||||
/**
|
||||
* Logs an INFO level message.
|
||||
*/
|
||||
void info(String msg, Throwable cause);
|
||||
|
||||
/**
|
||||
* Logs a WARN level message.
|
||||
*/
|
||||
void warn(String msg);
|
||||
|
||||
/**
|
||||
* Logs a WARN level message.
|
||||
*/
|
||||
void warn(String msg, Throwable cause);
|
||||
|
||||
/**
|
||||
* Logs an ERROR level message.
|
||||
*/
|
||||
void error(String msg);
|
||||
|
||||
/**
|
||||
* Logs an ERROR level message.
|
||||
*/
|
||||
void error(String msg, Throwable cause);
|
||||
|
||||
/**
|
||||
* Logs a message.
|
||||
*/
|
||||
void log(InternalLogLevel level, String msg);
|
||||
|
||||
/**
|
||||
* Logs a message.
|
||||
*/
|
||||
void log(InternalLogLevel level, String msg, Throwable cause);
|
||||
}
|
||||
|
|
@ -0,0 +1,155 @@
|
|||
/*
|
||||
* Copyright 2009 Red Hat, Inc.
|
||||
*
|
||||
* Red Hat licenses this file to you under the Apache License, version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.jboss.netty.akka.logging;
|
||||
|
||||
import org.jboss.netty.akka.util.internal.StackTraceSimplifier;
|
||||
|
||||
/**
|
||||
* Creates an {@link InternalLogger} or changes the default factory
|
||||
* implementation. This factory allows you to choose what logging framework
|
||||
* Netty should use. The default factory is {@link JdkLoggerFactory}.
|
||||
* You can change it to your preferred logging framework before other Netty
|
||||
* classes are loaded:
|
||||
* <pre>
|
||||
* {@link org.jboss.netty.akka.logging.InternalLoggerFactory}.setDefaultFactory(new {@link Log4JLoggerFactory}());
|
||||
* </pre>
|
||||
* Please note that the new default factory is effective only for the classes
|
||||
* which were loaded after the default factory is changed. Therefore,
|
||||
* {@link #setDefaultFactory(org.jboss.netty.akka.logging.InternalLoggerFactory)} should be called as early
|
||||
* as possible and shouldn't be called more than once.
|
||||
*
|
||||
* @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
|
||||
* @author <a href="http://gleamynode.net/">Trustin Lee</a>
|
||||
*
|
||||
* @version $Rev: 2122 $, $Date: 2010-02-02 11:00:04 +0900 (Tue, 02 Feb 2010) $
|
||||
*
|
||||
* @apiviz.landmark
|
||||
* @apiviz.has org.jboss.netty.logging.InternalLogger oneway - - creates
|
||||
*/
|
||||
public abstract class InternalLoggerFactory {
|
||||
private static volatile InternalLoggerFactory defaultFactory = new JdkLoggerFactory();
|
||||
|
||||
static {
|
||||
// Load the dependent classes in advance to avoid the case where
|
||||
// the VM fails to load the required classes because of too many open
|
||||
// files.
|
||||
StackTraceSimplifier.simplify(new Exception());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the default factory. The initial default factory is
|
||||
* {@link JdkLoggerFactory}.
|
||||
*/
|
||||
public static InternalLoggerFactory getDefaultFactory() {
|
||||
return defaultFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the default factory.
|
||||
*/
|
||||
public static void setDefaultFactory(InternalLoggerFactory defaultFactory) {
|
||||
if (defaultFactory == null) {
|
||||
throw new NullPointerException("defaultFactory");
|
||||
}
|
||||
InternalLoggerFactory.defaultFactory = defaultFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new logger instance with the name of the specified class.
|
||||
*/
|
||||
public static InternalLogger getInstance(Class<?> clazz) {
|
||||
return getInstance(clazz.getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new logger instance with the specified name.
|
||||
*/
|
||||
public static InternalLogger getInstance(String name) {
|
||||
final InternalLogger logger = getDefaultFactory().newInstance(name);
|
||||
return new InternalLogger() {
|
||||
|
||||
public void debug(String msg) {
|
||||
logger.debug(msg);
|
||||
}
|
||||
|
||||
public void debug(String msg, Throwable cause) {
|
||||
StackTraceSimplifier.simplify(cause);
|
||||
logger.debug(msg, cause);
|
||||
}
|
||||
|
||||
public void error(String msg) {
|
||||
logger.error(msg);
|
||||
}
|
||||
|
||||
public void error(String msg, Throwable cause) {
|
||||
StackTraceSimplifier.simplify(cause);
|
||||
logger.error(msg, cause);
|
||||
}
|
||||
|
||||
public void info(String msg) {
|
||||
logger.info(msg);
|
||||
}
|
||||
|
||||
public void info(String msg, Throwable cause) {
|
||||
StackTraceSimplifier.simplify(cause);
|
||||
logger.info(msg, cause);
|
||||
}
|
||||
|
||||
public boolean isDebugEnabled() {
|
||||
return logger.isDebugEnabled();
|
||||
}
|
||||
|
||||
public boolean isErrorEnabled() {
|
||||
return logger.isErrorEnabled();
|
||||
}
|
||||
|
||||
public boolean isInfoEnabled() {
|
||||
return logger.isInfoEnabled();
|
||||
}
|
||||
|
||||
public boolean isWarnEnabled() {
|
||||
return logger.isWarnEnabled();
|
||||
}
|
||||
|
||||
public void warn(String msg) {
|
||||
logger.warn(msg);
|
||||
}
|
||||
|
||||
public void warn(String msg, Throwable cause) {
|
||||
StackTraceSimplifier.simplify(cause);
|
||||
logger.warn(msg, cause);
|
||||
}
|
||||
|
||||
public boolean isEnabled(InternalLogLevel level) {
|
||||
return logger.isEnabled(level);
|
||||
}
|
||||
|
||||
public void log(InternalLogLevel level, String msg) {
|
||||
logger.log(level, msg);
|
||||
}
|
||||
|
||||
public void log(InternalLogLevel level, String msg, Throwable cause) {
|
||||
StackTraceSimplifier.simplify(cause);
|
||||
logger.log(level, msg, cause);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new logger instance with the specified name.
|
||||
*/
|
||||
public abstract InternalLogger newInstance(String name);
|
||||
}
|
||||
|
|
@ -0,0 +1,93 @@
|
|||
/*
|
||||
* Copyright 2009 Red Hat, Inc.
|
||||
*
|
||||
* Red Hat licenses this file to you under the Apache License, version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.jboss.netty.akka.logging;
|
||||
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* <a href="http://java.sun.com/javase/6/docs/technotes/guides/logging/index.html">java.util.logging</a>
|
||||
* logger.
|
||||
*
|
||||
* @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
|
||||
* @author <a href="http://gleamynode.net/">Trustin Lee</a>
|
||||
*
|
||||
* @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
|
||||
*
|
||||
*/
|
||||
class JdkLogger extends AbstractInternalLogger {
|
||||
|
||||
private final Logger logger;
|
||||
private final String loggerName;
|
||||
|
||||
JdkLogger(Logger logger, String loggerName) {
|
||||
this.logger = logger;
|
||||
this.loggerName = loggerName;
|
||||
}
|
||||
|
||||
public void debug(String msg) {
|
||||
logger.logp(Level.FINE, loggerName, null, msg);
|
||||
}
|
||||
|
||||
public void debug(String msg, Throwable cause) {
|
||||
logger.logp(Level.FINE, loggerName, null, msg, cause);
|
||||
}
|
||||
|
||||
public void error(String msg) {
|
||||
logger.logp(Level.SEVERE, loggerName, null, msg);
|
||||
}
|
||||
|
||||
public void error(String msg, Throwable cause) {
|
||||
logger.logp(Level.SEVERE, loggerName, null, msg, cause);
|
||||
}
|
||||
|
||||
public void info(String msg) {
|
||||
logger.logp(Level.INFO, loggerName, null, msg);
|
||||
}
|
||||
|
||||
public void info(String msg, Throwable cause) {
|
||||
logger.logp(Level.INFO, loggerName, null, msg, cause);
|
||||
}
|
||||
|
||||
public boolean isDebugEnabled() {
|
||||
return logger.isLoggable(Level.FINE);
|
||||
}
|
||||
|
||||
public boolean isErrorEnabled() {
|
||||
return logger.isLoggable(Level.SEVERE);
|
||||
}
|
||||
|
||||
public boolean isInfoEnabled() {
|
||||
return logger.isLoggable(Level.INFO);
|
||||
}
|
||||
|
||||
public boolean isWarnEnabled() {
|
||||
return logger.isLoggable(Level.WARNING);
|
||||
}
|
||||
|
||||
public void warn(String msg) {
|
||||
logger.logp(Level.WARNING, loggerName, null, msg);
|
||||
}
|
||||
|
||||
public void warn(String msg, Throwable cause) {
|
||||
logger.logp(Level.WARNING, loggerName, null, msg, cause);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return loggerName;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
/*
|
||||
* Copyright 2009 Red Hat, Inc.
|
||||
*
|
||||
* Red Hat licenses this file to you under the Apache License, version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.jboss.netty.akka.logging;
|
||||
|
||||
|
||||
/**
|
||||
* Logger factory which creates a
|
||||
* <a href="http://java.sun.com/javase/6/docs/technotes/guides/logging/index.html">java.util.logging</a>
|
||||
* logger.
|
||||
*
|
||||
* @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
|
||||
* @author <a href="http://gleamynode.net/">Trustin Lee</a>
|
||||
*
|
||||
* @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
|
||||
*
|
||||
*/
|
||||
public class JdkLoggerFactory extends InternalLoggerFactory {
|
||||
|
||||
@Override
|
||||
public InternalLogger newInstance(String name) {
|
||||
final java.util.logging.Logger logger =
|
||||
java.util.logging.Logger.getLogger(name);
|
||||
return new JdkLogger(logger, name);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,64 @@
|
|||
/*
|
||||
* Copyright 2009 Red Hat, Inc.
|
||||
*
|
||||
* Red Hat licenses this file to you under the Apache License, version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.jboss.netty.akka.util;
|
||||
|
||||
import org.jboss.netty.akka.util.internal.SystemPropertyUtil;
|
||||
|
||||
/**
|
||||
* Determines if Netty is running in a debug mode or not. Please note that
|
||||
* this is not a Java debug mode. You can enable Netty debug mode by
|
||||
* specifying the {@code "org.jboss.netty.debug"} system property (e.g.
|
||||
* {@code java -Dorg.jboss.netty.debug ...})
|
||||
* <p>
|
||||
* If debug mode is disabled (default), the stack trace of the exceptions are
|
||||
* compressed to help debugging a user application.
|
||||
* <p>
|
||||
* If debug mode is enabled, the stack trace of the exceptions raised in
|
||||
* {@link ChannelPipeline} or {@link ChannelSink} are retained as it is to help
|
||||
* debugging Netty.
|
||||
*
|
||||
* @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
|
||||
* @author <a href="http://gleamynode.net/">Trustin Lee</a>
|
||||
*
|
||||
* @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
|
||||
*/
|
||||
public class DebugUtil {
|
||||
|
||||
/**
|
||||
* Returns {@code true} if and only if Netty debug mode is enabled.
|
||||
*/
|
||||
public static boolean isDebugEnabled() {
|
||||
String value;
|
||||
try {
|
||||
value = SystemPropertyUtil.get("org.jboss.netty.debug");
|
||||
} catch (Exception e) {
|
||||
value = null;
|
||||
}
|
||||
|
||||
if (value == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
value = value.trim().toUpperCase();
|
||||
return !value.startsWith("N") &&
|
||||
!value.startsWith("F") &&
|
||||
!value.equals("0");
|
||||
}
|
||||
|
||||
private DebugUtil() {
|
||||
// Unused
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,555 @@
|
|||
/*
|
||||
* Copyright 2009 Red Hat, Inc.
|
||||
*
|
||||
* Red Hat licenses this file to you under the Apache License, version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.jboss.netty.akka.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.locks.ReadWriteLock;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
|
||||
import org.jboss.netty.akka.logging.InternalLogger;
|
||||
import org.jboss.netty.akka.logging.InternalLoggerFactory;
|
||||
import org.jboss.netty.akka.util.internal.ConcurrentIdentityHashMap;
|
||||
import org.jboss.netty.akka.util.internal.ReusableIterator;
|
||||
import org.jboss.netty.akka.util.internal.SharedResourceMisuseDetector;
|
||||
|
||||
/**
|
||||
* A {@link Timer} optimized for approximated I/O timeout scheduling.
|
||||
*
|
||||
* <h3>Tick Duration</h3>
|
||||
*
|
||||
* As described with 'approximated', this timer does not execute the scheduled
|
||||
* {@link TimerTask} on time. {@link org.jboss.netty.akka.util.HashedWheelTimer}, on every tick, will
|
||||
* check if there are any {@link TimerTask}s behind the schedule and execute
|
||||
* them.
|
||||
* <p>
|
||||
* You can increase or decrease the accuracy of the execution timing by
|
||||
* specifying smaller or larger tick duration in the constructor. In most
|
||||
* network applications, I/O timeout does not need to be accurate. Therefore,
|
||||
* the default tick duration is 100 milliseconds and you will not need to try
|
||||
* different configurations in most cases.
|
||||
*
|
||||
* <h3>Ticks per Wheel (Wheel Size)</h3>
|
||||
*
|
||||
* {@link org.jboss.netty.akka.util.HashedWheelTimer} maintains a data structure called 'wheel'.
|
||||
* To put simply, a wheel is a hash table of {@link TimerTask}s whose hash
|
||||
* function is 'dead line of the task'. The default number of ticks per wheel
|
||||
* (i.e. the size of the wheel) is 512. You could specify a larger value
|
||||
* if you are going to schedule a lot of timeouts.
|
||||
*
|
||||
* <h3>Do not create many instances.</h3>
|
||||
*
|
||||
* {@link org.jboss.netty.akka.util.HashedWheelTimer} creates a new thread whenever it is instantiated and
|
||||
* started. Therefore, you should make sure to create only one instance and
|
||||
* share it across your application. One of the common mistakes, that makes
|
||||
* your application unresponsive, is to create a new instance in
|
||||
* {@link ChannelPipelineFactory}, which results in the creation of a new thread
|
||||
* for every connection.
|
||||
*
|
||||
* <h3>Implementation Details</h3>
|
||||
*
|
||||
* {@link org.jboss.netty.akka.util.HashedWheelTimer} is based on
|
||||
* <a href="http://cseweb.ucsd.edu/users/varghese/">George Varghese</a> and
|
||||
* Tony Lauck's paper,
|
||||
* <a href="http://cseweb.ucsd.edu/users/varghese/PAPERS/twheel.ps.Z">'Hashed
|
||||
* and Hierarchical Timing Wheels: data structures to efficiently implement a
|
||||
* timer facility'</a>. More comprehensive slides are located
|
||||
* <a href="http://www.cse.wustl.edu/~cdgill/courses/cs6874/TimingWheels.ppt">here</a>.
|
||||
*
|
||||
* @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
|
||||
* @author <a href="http://gleamynode.net/">Trustin Lee</a>
|
||||
* @version $Rev: 2297 $, $Date: 2010-06-07 10:50:02 +0900 (Mon, 07 Jun 2010) $
|
||||
*/
|
||||
public class HashedWheelTimer implements Timer {
|
||||
|
||||
static final InternalLogger logger =
|
||||
InternalLoggerFactory.getInstance(HashedWheelTimer.class);
|
||||
private static final AtomicInteger id = new AtomicInteger();
|
||||
|
||||
private static final SharedResourceMisuseDetector misuseDetector =
|
||||
new SharedResourceMisuseDetector(HashedWheelTimer.class);
|
||||
|
||||
private final Worker worker = new Worker();
|
||||
final Thread workerThread;
|
||||
final AtomicBoolean shutdown = new AtomicBoolean();
|
||||
|
||||
private final long roundDuration;
|
||||
final long tickDuration;
|
||||
final Set<HashedWheelTimeout>[] wheel;
|
||||
final ReusableIterator<HashedWheelTimeout>[] iterators;
|
||||
final int mask;
|
||||
final ReadWriteLock lock = new ReentrantReadWriteLock();
|
||||
volatile int wheelCursor;
|
||||
|
||||
/**
|
||||
* Creates a new timer with the default thread factory
|
||||
* ({@link java.util.concurrent.Executors#defaultThreadFactory()}), default tick duration, and
|
||||
* default number of ticks per wheel.
|
||||
*/
|
||||
public HashedWheelTimer() {
|
||||
this(Executors.defaultThreadFactory());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new timer with the default thread factory
|
||||
* ({@link java.util.concurrent.Executors#defaultThreadFactory()}) and default number of ticks
|
||||
* per wheel.
|
||||
*
|
||||
* @param tickDuration the duration between tick
|
||||
* @param unit the time unit of the {@code tickDuration}
|
||||
*/
|
||||
public HashedWheelTimer(long tickDuration, TimeUnit unit) {
|
||||
this(Executors.defaultThreadFactory(), tickDuration, unit);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new timer with the default thread factory
|
||||
* ({@link java.util.concurrent.Executors#defaultThreadFactory()}).
|
||||
*
|
||||
* @param tickDuration the duration between tick
|
||||
* @param unit the time unit of the {@code tickDuration}
|
||||
* @param ticksPerWheel the size of the wheel
|
||||
*/
|
||||
public HashedWheelTimer(long tickDuration, TimeUnit unit, int ticksPerWheel) {
|
||||
this(Executors.defaultThreadFactory(), tickDuration, unit, ticksPerWheel);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new timer with the default tick duration and default number of
|
||||
* ticks per wheel.
|
||||
*
|
||||
* @param threadFactory a {@link java.util.concurrent.ThreadFactory} that creates a
|
||||
* background {@link Thread} which is dedicated to
|
||||
* {@link TimerTask} execution.
|
||||
*/
|
||||
public HashedWheelTimer(ThreadFactory threadFactory) {
|
||||
this(threadFactory, 100, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new timer with the default number of ticks per wheel.
|
||||
*
|
||||
* @param threadFactory a {@link java.util.concurrent.ThreadFactory} that creates a
|
||||
* background {@link Thread} which is dedicated to
|
||||
* {@link TimerTask} execution.
|
||||
* @param tickDuration the duration between tick
|
||||
* @param unit the time unit of the {@code tickDuration}
|
||||
*/
|
||||
public HashedWheelTimer(
|
||||
ThreadFactory threadFactory, long tickDuration, TimeUnit unit) {
|
||||
this(threadFactory, tickDuration, unit, 512);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new timer.
|
||||
*
|
||||
* @param threadFactory a {@link java.util.concurrent.ThreadFactory} that creates a
|
||||
* background {@link Thread} which is dedicated to
|
||||
* {@link TimerTask} execution.
|
||||
* @param tickDuration the duration between tick
|
||||
* @param unit the time unit of the {@code tickDuration}
|
||||
* @param ticksPerWheel the size of the wheel
|
||||
*/
|
||||
public HashedWheelTimer(
|
||||
ThreadFactory threadFactory,
|
||||
long tickDuration, TimeUnit unit, int ticksPerWheel) {
|
||||
|
||||
if (threadFactory == null) {
|
||||
throw new NullPointerException("threadFactory");
|
||||
}
|
||||
if (unit == null) {
|
||||
throw new NullPointerException("unit");
|
||||
}
|
||||
if (tickDuration <= 0) {
|
||||
throw new IllegalArgumentException(
|
||||
"tickDuration must be greater than 0: " + tickDuration);
|
||||
}
|
||||
if (ticksPerWheel <= 0) {
|
||||
throw new IllegalArgumentException(
|
||||
"ticksPerWheel must be greater than 0: " + ticksPerWheel);
|
||||
}
|
||||
|
||||
// Normalize ticksPerWheel to power of two and initialize the wheel.
|
||||
wheel = createWheel(ticksPerWheel);
|
||||
iterators = createIterators(wheel);
|
||||
mask = wheel.length - 1;
|
||||
|
||||
// Convert tickDuration to milliseconds.
|
||||
this.tickDuration = tickDuration = unit.toMillis(tickDuration);
|
||||
|
||||
// Prevent overflow.
|
||||
if (tickDuration == Long.MAX_VALUE ||
|
||||
tickDuration >= Long.MAX_VALUE / wheel.length) {
|
||||
throw new IllegalArgumentException(
|
||||
"tickDuration is too long: " +
|
||||
tickDuration + ' ' + unit);
|
||||
}
|
||||
|
||||
roundDuration = tickDuration * wheel.length;
|
||||
|
||||
workerThread = threadFactory.newThread(new ThreadRenamingRunnable(
|
||||
worker, "Hashed wheel timer #" + id.incrementAndGet()));
|
||||
|
||||
// Misuse check
|
||||
misuseDetector.increase();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static Set<HashedWheelTimeout>[] createWheel(int ticksPerWheel) {
|
||||
if (ticksPerWheel <= 0) {
|
||||
throw new IllegalArgumentException(
|
||||
"ticksPerWheel must be greater than 0: " + ticksPerWheel);
|
||||
}
|
||||
if (ticksPerWheel > 1073741824) {
|
||||
throw new IllegalArgumentException(
|
||||
"ticksPerWheel may not be greater than 2^30: " + ticksPerWheel);
|
||||
}
|
||||
|
||||
ticksPerWheel = normalizeTicksPerWheel(ticksPerWheel);
|
||||
Set<HashedWheelTimeout>[] wheel = new Set[ticksPerWheel];
|
||||
for (int i = 0; i < wheel.length; i ++) {
|
||||
wheel[i] = new MapBackedSet<HashedWheelTimeout>(
|
||||
new ConcurrentIdentityHashMap<HashedWheelTimeout, Boolean>(16, 0.95f, 4));
|
||||
}
|
||||
return wheel;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static ReusableIterator<HashedWheelTimeout>[] createIterators(Set<HashedWheelTimeout>[] wheel) {
|
||||
ReusableIterator<HashedWheelTimeout>[] iterators = new ReusableIterator[wheel.length];
|
||||
for (int i = 0; i < wheel.length; i ++) {
|
||||
iterators[i] = (ReusableIterator<HashedWheelTimeout>) wheel[i].iterator();
|
||||
}
|
||||
return iterators;
|
||||
}
|
||||
|
||||
private static int normalizeTicksPerWheel(int ticksPerWheel) {
|
||||
int normalizedTicksPerWheel = 1;
|
||||
while (normalizedTicksPerWheel < ticksPerWheel) {
|
||||
normalizedTicksPerWheel <<= 1;
|
||||
}
|
||||
return normalizedTicksPerWheel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the background thread explicitly. The background thread will
|
||||
* start automatically on demand even if you did not call this method.
|
||||
*
|
||||
* @throws IllegalStateException if this timer has been
|
||||
* {@linkplain #stop() stopped} already
|
||||
*/
|
||||
public synchronized void start() {
|
||||
if (shutdown.get()) {
|
||||
throw new IllegalStateException("cannot be started once stopped");
|
||||
}
|
||||
|
||||
if (!workerThread.isAlive()) {
|
||||
workerThread.start();
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized Set<Timeout> stop() {
|
||||
if (Thread.currentThread() == workerThread) {
|
||||
throw new IllegalStateException(
|
||||
HashedWheelTimer.class.getSimpleName() +
|
||||
".stop() cannot be called from " +
|
||||
TimerTask.class.getSimpleName());
|
||||
}
|
||||
|
||||
if (!shutdown.compareAndSet(false, true)) {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
|
||||
boolean interrupted = false;
|
||||
while (workerThread.isAlive()) {
|
||||
workerThread.interrupt();
|
||||
try {
|
||||
workerThread.join(100);
|
||||
} catch (InterruptedException e) {
|
||||
interrupted = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (interrupted) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
|
||||
misuseDetector.decrease();
|
||||
|
||||
Set<Timeout> unprocessedTimeouts = new HashSet<Timeout>();
|
||||
for (Set<HashedWheelTimeout> bucket: wheel) {
|
||||
unprocessedTimeouts.addAll(bucket);
|
||||
bucket.clear();
|
||||
}
|
||||
|
||||
return Collections.unmodifiableSet(unprocessedTimeouts);
|
||||
}
|
||||
|
||||
public Timeout newTimeout(TimerTask task, long delay, TimeUnit unit) {
|
||||
final long currentTime = System.currentTimeMillis();
|
||||
|
||||
if (task == null) {
|
||||
throw new NullPointerException("task");
|
||||
}
|
||||
if (unit == null) {
|
||||
throw new NullPointerException("unit");
|
||||
}
|
||||
|
||||
if (!workerThread.isAlive()) {
|
||||
start();
|
||||
}
|
||||
|
||||
delay = unit.toMillis(delay);
|
||||
HashedWheelTimeout timeout = new HashedWheelTimeout(task, currentTime + delay);
|
||||
scheduleTimeout(timeout, delay);
|
||||
// TODO : remove
|
||||
return timeout;
|
||||
}
|
||||
|
||||
void scheduleTimeout(HashedWheelTimeout timeout, long delay) {
|
||||
// delay must be equal to or greater than tickDuration so that the
|
||||
// worker thread never misses the timeout.
|
||||
if (delay < tickDuration) {
|
||||
delay = tickDuration;
|
||||
}
|
||||
|
||||
// Prepare the required parameters to schedule the timeout object.
|
||||
final long lastRoundDelay = delay % roundDuration;
|
||||
final long lastTickDelay = delay % tickDuration;
|
||||
final long relativeIndex =
|
||||
lastRoundDelay / tickDuration + (lastTickDelay != 0? 1 : 0);
|
||||
|
||||
final long remainingRounds =
|
||||
delay / roundDuration - (delay % roundDuration == 0? 1 : 0);
|
||||
|
||||
// Add the timeout to the wheel.
|
||||
lock.readLock().lock();
|
||||
try {
|
||||
int stopIndex = (int) (wheelCursor + relativeIndex & mask);
|
||||
timeout.stopIndex = stopIndex;
|
||||
timeout.remainingRounds = remainingRounds;
|
||||
wheel[stopIndex].add(timeout);
|
||||
} finally {
|
||||
lock.readLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
private final class Worker implements Runnable {
|
||||
|
||||
private long startTime;
|
||||
private long tick;
|
||||
|
||||
Worker() {
|
||||
super();
|
||||
}
|
||||
|
||||
public void run() {
|
||||
List<HashedWheelTimeout> expiredTimeouts =
|
||||
new ArrayList<HashedWheelTimeout>();
|
||||
|
||||
startTime = System.currentTimeMillis();
|
||||
tick = 1;
|
||||
|
||||
while (!shutdown.get()) {
|
||||
final long deadline = waitForNextTick();
|
||||
if (deadline > 0) {
|
||||
fetchExpiredTimeouts(expiredTimeouts, deadline);
|
||||
notifyExpiredTimeouts(expiredTimeouts);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void fetchExpiredTimeouts(
|
||||
List<HashedWheelTimeout> expiredTimeouts, long deadline) {
|
||||
|
||||
// Find the expired timeouts and decrease the round counter
|
||||
// if necessary. Note that we don't send the notification
|
||||
// immediately to make sure the listeners are called without
|
||||
// an exclusive lock.
|
||||
lock.writeLock().lock();
|
||||
try {
|
||||
int newWheelCursor = wheelCursor = wheelCursor + 1 & mask;
|
||||
ReusableIterator<HashedWheelTimeout> i = iterators[newWheelCursor];
|
||||
fetchExpiredTimeouts(expiredTimeouts, i, deadline);
|
||||
} finally {
|
||||
lock.writeLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
private void fetchExpiredTimeouts(
|
||||
List<HashedWheelTimeout> expiredTimeouts,
|
||||
ReusableIterator<HashedWheelTimeout> i, long deadline) {
|
||||
|
||||
List<HashedWheelTimeout> slipped = null;
|
||||
i.rewind();
|
||||
while (i.hasNext()) {
|
||||
HashedWheelTimeout timeout = i.next();
|
||||
if (timeout.remainingRounds <= 0) {
|
||||
i.remove();
|
||||
if (timeout.deadline <= deadline) {
|
||||
expiredTimeouts.add(timeout);
|
||||
} else {
|
||||
// Handle the case where the timeout is put into a wrong
|
||||
// place, usually one tick earlier. For now, just add
|
||||
// it to a temporary list - we will reschedule it in a
|
||||
// separate loop.
|
||||
if (slipped == null) {
|
||||
slipped = new ArrayList<HashedWheelTimeout>();
|
||||
}
|
||||
slipped.add(timeout);
|
||||
}
|
||||
} else {
|
||||
timeout.remainingRounds --;
|
||||
}
|
||||
}
|
||||
|
||||
// Reschedule the slipped timeouts.
|
||||
if (slipped != null) {
|
||||
for (HashedWheelTimeout timeout: slipped) {
|
||||
scheduleTimeout(timeout, timeout.deadline - deadline);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void notifyExpiredTimeouts(
|
||||
List<HashedWheelTimeout> expiredTimeouts) {
|
||||
// Notify the expired timeouts.
|
||||
for (int i = expiredTimeouts.size() - 1; i >= 0; i --) {
|
||||
expiredTimeouts.get(i).expire();
|
||||
}
|
||||
|
||||
// Clean up the temporary list.
|
||||
expiredTimeouts.clear();
|
||||
}
|
||||
|
||||
private long waitForNextTick() {
|
||||
long deadline = startTime + tickDuration * tick;
|
||||
|
||||
for (;;) {
|
||||
final long currentTime = System.currentTimeMillis();
|
||||
final long sleepTime = tickDuration * tick - (currentTime - startTime);
|
||||
|
||||
if (sleepTime <= 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
try {
|
||||
Thread.sleep(sleepTime);
|
||||
} catch (InterruptedException e) {
|
||||
if (shutdown.get()) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Increase the tick.
|
||||
tick ++;
|
||||
return deadline;
|
||||
}
|
||||
}
|
||||
|
||||
private final class HashedWheelTimeout implements Timeout {
|
||||
|
||||
private static final int ST_INIT = 0;
|
||||
private static final int ST_CANCELLED = 1;
|
||||
private static final int ST_EXPIRED = 2;
|
||||
|
||||
private final TimerTask task;
|
||||
final long deadline;
|
||||
volatile int stopIndex;
|
||||
volatile long remainingRounds;
|
||||
private final AtomicInteger state = new AtomicInteger(ST_INIT);
|
||||
|
||||
HashedWheelTimeout(TimerTask task, long deadline) {
|
||||
this.task = task;
|
||||
this.deadline = deadline;
|
||||
}
|
||||
|
||||
public Timer getTimer() {
|
||||
return HashedWheelTimer.this;
|
||||
}
|
||||
|
||||
public TimerTask getTask() {
|
||||
return task;
|
||||
}
|
||||
|
||||
public void cancel() {
|
||||
if (!state.compareAndSet(ST_INIT, ST_CANCELLED)) {
|
||||
// TODO return false
|
||||
return;
|
||||
}
|
||||
|
||||
wheel[stopIndex].remove(this);
|
||||
}
|
||||
|
||||
public boolean isCancelled() {
|
||||
return state.get() == ST_CANCELLED;
|
||||
}
|
||||
|
||||
public boolean isExpired() {
|
||||
return state.get() != ST_INIT;
|
||||
}
|
||||
|
||||
public void expire() {
|
||||
if (!state.compareAndSet(ST_INIT, ST_EXPIRED)) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
task.run(this);
|
||||
} catch (Throwable t) {
|
||||
logger.warn(
|
||||
"An exception was thrown by " +
|
||||
TimerTask.class.getSimpleName() + ".", t);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
long currentTime = System.currentTimeMillis();
|
||||
long remaining = deadline - currentTime;
|
||||
|
||||
StringBuilder buf = new StringBuilder(192);
|
||||
buf.append(getClass().getSimpleName());
|
||||
buf.append('(');
|
||||
|
||||
buf.append("deadline: ");
|
||||
if (remaining > 0) {
|
||||
buf.append(remaining);
|
||||
buf.append(" ms later, ");
|
||||
} else if (remaining < 0) {
|
||||
buf.append(-remaining);
|
||||
buf.append(" ms ago, ");
|
||||
} else {
|
||||
buf.append("now, ");
|
||||
}
|
||||
|
||||
if (isCancelled()) {
|
||||
buf.append (", cancelled");
|
||||
}
|
||||
|
||||
return buf.append(')').toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,74 @@
|
|||
/*
|
||||
* Copyright 2009 Red Hat, Inc.
|
||||
*
|
||||
* Red Hat licenses this file to you under the Apache License, version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.jboss.netty.akka.util;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.AbstractSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* A {@link java.util.Map}-backed {@link java.util.Set}.
|
||||
*
|
||||
* @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
|
||||
* @author <a href="http://gleamynode.net/">Trustin Lee</a>
|
||||
*
|
||||
* @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
|
||||
*/
|
||||
final class MapBackedSet<E> extends AbstractSet<E> implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = -6761513279741915432L;
|
||||
|
||||
private final Map<E, Boolean> map;
|
||||
|
||||
/**
|
||||
* Creates a new instance which wraps the specified {@code map}.
|
||||
*/
|
||||
MapBackedSet(Map<E, Boolean> map) {
|
||||
this.map = map;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return map.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Object o) {
|
||||
return map.containsKey(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(E o) {
|
||||
return map.put(o, Boolean.TRUE) == null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(Object o) {
|
||||
return map.remove(o) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
map.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<E> iterator() {
|
||||
return map.keySet().iterator();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
/*
|
||||
* Copyright 2009 Red Hat, Inc.
|
||||
*
|
||||
* Red Hat licenses this file to you under the Apache License, version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.jboss.netty.akka.util;
|
||||
|
||||
/**
|
||||
* Overrides the thread name proposed by {@link ThreadRenamingRunnable}.
|
||||
*
|
||||
* @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
|
||||
* @author <a href="http://gleamynode.net/">Trustin Lee</a>
|
||||
* @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
|
||||
*/
|
||||
public interface ThreadNameDeterminer {
|
||||
|
||||
/**
|
||||
* {@link org.jboss.netty.akka.util.ThreadNameDeterminer} that accepts the proposed thread name
|
||||
* as is.
|
||||
*/
|
||||
ThreadNameDeterminer PROPOSED = new ThreadNameDeterminer() {
|
||||
public String determineThreadName(String currentThreadName,
|
||||
String proposedThreadName) throws Exception {
|
||||
return proposedThreadName;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* {@link org.jboss.netty.akka.util.ThreadNameDeterminer} that rejects the proposed thread name and
|
||||
* retains the current one.
|
||||
*/
|
||||
ThreadNameDeterminer CURRENT = new ThreadNameDeterminer() {
|
||||
public String determineThreadName(String currentThreadName,
|
||||
String proposedThreadName) throws Exception {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Overrides the thread name proposed by {@link ThreadRenamingRunnable}.
|
||||
*
|
||||
* @param currentThreadName the current thread name
|
||||
* @param proposedThreadName the proposed new thread name
|
||||
* @return the actual new thread name.
|
||||
* If {@code null} is returned, the proposed thread name is
|
||||
* discarded (i.e. no rename).
|
||||
*/
|
||||
String determineThreadName(String currentThreadName, String proposedThreadName) throws Exception;
|
||||
}
|
||||
|
|
@ -0,0 +1,131 @@
|
|||
/*
|
||||
* Copyright 2009 Red Hat, Inc.
|
||||
*
|
||||
* Red Hat licenses this file to you under the Apache License, version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.jboss.netty.akka.util;
|
||||
|
||||
import org.jboss.netty.akka.logging.InternalLogger;
|
||||
import org.jboss.netty.akka.logging.InternalLoggerFactory;
|
||||
|
||||
|
||||
/**
|
||||
* A {@link Runnable} that changes the current thread name and reverts it back
|
||||
* when its execution ends. To change the default thread names set by Netty,
|
||||
* use {@link #setThreadNameDeterminer(ThreadNameDeterminer)}.
|
||||
*
|
||||
* @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
|
||||
* @author <a href="http://gleamynode.net/">Trustin Lee</a>
|
||||
*
|
||||
* @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
|
||||
*
|
||||
* @apiviz.landmark
|
||||
* @apiviz.has org.jboss.netty.util.ThreadNameDeterminer oneway - -
|
||||
*
|
||||
*/
|
||||
public class ThreadRenamingRunnable implements Runnable {
|
||||
|
||||
private static final InternalLogger logger =
|
||||
InternalLoggerFactory.getInstance(ThreadRenamingRunnable.class);
|
||||
|
||||
private static volatile ThreadNameDeterminer threadNameDeterminer =
|
||||
ThreadNameDeterminer.PROPOSED;
|
||||
|
||||
/**
|
||||
* Returns the {@link ThreadNameDeterminer} which overrides the proposed
|
||||
* new thread name.
|
||||
*/
|
||||
public static ThreadNameDeterminer getThreadNameDeterminer() {
|
||||
return threadNameDeterminer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the {@link ThreadNameDeterminer} which overrides the proposed new
|
||||
* thread name. Please note that the specified {@link ThreadNameDeterminer}
|
||||
* affects only new {@link org.jboss.netty.akka.util.ThreadRenamingRunnable}s; the existing instances
|
||||
* are not affected at all. Therefore, you should make sure to call this
|
||||
* method at the earliest possible point (i.e. before any Netty worker
|
||||
* thread starts) for consistent thread naming. Otherwise, you might see
|
||||
* the default thread names and the new names appear at the same time in
|
||||
* the full thread dump.
|
||||
*/
|
||||
public static void setThreadNameDeterminer(ThreadNameDeterminer threadNameDeterminer) {
|
||||
if (threadNameDeterminer == null) {
|
||||
throw new NullPointerException("threadNameDeterminer");
|
||||
}
|
||||
ThreadRenamingRunnable.threadNameDeterminer = threadNameDeterminer;
|
||||
}
|
||||
|
||||
private final Runnable runnable;
|
||||
private final String proposedThreadName;
|
||||
|
||||
/**
|
||||
* Creates a new instance which wraps the specified {@code runnable}
|
||||
* and changes the thread name to the specified thread name when the
|
||||
* specified {@code runnable} is running.
|
||||
*/
|
||||
public ThreadRenamingRunnable(Runnable runnable, String proposedThreadName) {
|
||||
if (runnable == null) {
|
||||
throw new NullPointerException("runnable");
|
||||
}
|
||||
if (proposedThreadName == null) {
|
||||
throw new NullPointerException("proposedThreadName");
|
||||
}
|
||||
this.runnable = runnable;
|
||||
this.proposedThreadName = proposedThreadName;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
final Thread currentThread = Thread.currentThread();
|
||||
final String oldThreadName = currentThread.getName();
|
||||
final String newThreadName = getNewThreadName(oldThreadName);
|
||||
|
||||
// Change the thread name before starting the actual runnable.
|
||||
boolean renamed = false;
|
||||
if (!oldThreadName.equals(newThreadName)) {
|
||||
try {
|
||||
currentThread.setName(newThreadName);
|
||||
renamed = true;
|
||||
} catch (SecurityException e) {
|
||||
logger.debug(
|
||||
"Failed to rename a thread " +
|
||||
"due to security restriction.", e);
|
||||
}
|
||||
}
|
||||
|
||||
// Run the actual runnable and revert the name back when it ends.
|
||||
try {
|
||||
runnable.run();
|
||||
} finally {
|
||||
if (renamed) {
|
||||
// Revert the name back if the current thread was renamed.
|
||||
// We do not check the exception here because we know it works.
|
||||
currentThread.setName(oldThreadName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String getNewThreadName(String currentThreadName) {
|
||||
String newThreadName = null;
|
||||
|
||||
try {
|
||||
newThreadName =
|
||||
getThreadNameDeterminer().determineThreadName(
|
||||
currentThreadName, proposedThreadName);
|
||||
} catch (Throwable t) {
|
||||
logger.warn("Failed to determine the thread name", t);
|
||||
}
|
||||
|
||||
return newThreadName == null? currentThreadName : newThreadName;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
* Copyright 2009 Red Hat, Inc.
|
||||
*
|
||||
* Red Hat licenses this file to you under the Apache License, version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.jboss.netty.akka.util;
|
||||
|
||||
/**
|
||||
* A handle associated with a {@link TimerTask} that is returned by a
|
||||
* {@link Timer}.
|
||||
*
|
||||
* @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
|
||||
* @author <a href="http://gleamynode.net/">Trustin Lee</a>
|
||||
* @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
|
||||
*/
|
||||
public interface Timeout {
|
||||
|
||||
/**
|
||||
* Returns the {@link Timer} that created this handle.
|
||||
*/
|
||||
Timer getTimer();
|
||||
|
||||
/**
|
||||
* Returns the {@link TimerTask} which is associated with this handle.
|
||||
*/
|
||||
TimerTask getTask();
|
||||
|
||||
/**
|
||||
* Returns {@code true} if and only if the {@link TimerTask} associated
|
||||
* with this handle has been expired.
|
||||
*/
|
||||
boolean isExpired();
|
||||
|
||||
/**
|
||||
* Returns {@code true} if and only if the {@link TimerTask} associated
|
||||
* with this handle has been cancelled.
|
||||
*/
|
||||
boolean isCancelled();
|
||||
|
||||
/**
|
||||
* Cancels the {@link TimerTask} associated with this handle. It the
|
||||
* task has been executed or cancelled already, it will return with no
|
||||
* side effect.
|
||||
*/
|
||||
void cancel();
|
||||
}
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
/*
|
||||
* Copyright 2009 Red Hat, Inc.
|
||||
*
|
||||
* Red Hat licenses this file to you under the Apache License, version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.jboss.netty.akka.util;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* Schedules {@link TimerTask}s for one-time future execution in a background
|
||||
* thread.
|
||||
*
|
||||
* @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
|
||||
* @author <a href="http://gleamynode.net/">Trustin Lee</a>
|
||||
* @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
|
||||
*
|
||||
* @apiviz.landmark
|
||||
* @apiviz.has org.jboss.netty.util.TimerTask oneway - - executes
|
||||
* @apiviz.has org.jboss.netty.util.Timeout oneway - - creates
|
||||
*/
|
||||
public interface Timer {
|
||||
|
||||
/**
|
||||
* Schedules the specified {@link TimerTask} for one-time execution after
|
||||
* the specified delay.
|
||||
*
|
||||
* @return a handle which is associated with the specified task
|
||||
*
|
||||
* @throws IllegalStateException if this timer has been
|
||||
* {@linkplain #stop() stopped} already
|
||||
*/
|
||||
Timeout newTimeout(TimerTask task, long delay, TimeUnit unit);
|
||||
|
||||
/**
|
||||
* Releases all resources acquired by this {@link org.jboss.netty.akka.util.Timer} and cancels all
|
||||
* tasks which were scheduled but not executed yet.
|
||||
*
|
||||
* @return the handles associated with the tasks which were canceled by
|
||||
* this method
|
||||
*/
|
||||
Set<Timeout> stop();
|
||||
}
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
* Copyright 2009 Red Hat, Inc.
|
||||
*
|
||||
* Red Hat licenses this file to you under the Apache License, version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.jboss.netty.akka.util;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* A task which is executed after the delay specified with
|
||||
* {@link Timer#newTimeout(org.jboss.netty.akka.util.TimerTask, long, java.util.concurrent.TimeUnit)}.
|
||||
*
|
||||
* @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
|
||||
* @author <a href="http://gleamynode.net/">Trustin Lee</a>
|
||||
* @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
|
||||
*/
|
||||
public interface TimerTask {
|
||||
|
||||
/**
|
||||
* Executed after the delay specified with
|
||||
* {@link Timer#newTimeout(org.jboss.netty.akka.util.TimerTask, long, java.util.concurrent.TimeUnit)}.
|
||||
*
|
||||
* @param timeout a handle which is associated with this task
|
||||
*/
|
||||
void run(Timeout timeout) throws Exception;
|
||||
}
|
||||
File diff suppressed because it is too large
Load diff
|
|
@ -0,0 +1,27 @@
|
|||
/*
|
||||
* Copyright 2009 Red Hat, Inc.
|
||||
*
|
||||
* Red Hat licenses this file to you under the Apache License, version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.jboss.netty.akka.util.internal;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
|
||||
* @author <a href="http://gleamynode.net/">Trustin Lee</a>
|
||||
* @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
|
||||
*/
|
||||
public interface ReusableIterator<E> extends Iterator<E> {
|
||||
void rewind();
|
||||
}
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
/*
|
||||
* Copyright 2009 Red Hat, Inc.
|
||||
*
|
||||
* Red Hat licenses this file to you under the Apache License, version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.jboss.netty.akka.util.internal;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import org.jboss.netty.akka.logging.InternalLogger;
|
||||
import org.jboss.netty.akka.logging.InternalLoggerFactory;
|
||||
|
||||
/**
|
||||
* Warn when user creates too many instances to avoid {@link OutOfMemoryError}.
|
||||
*
|
||||
* @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
|
||||
* @author <a href="http://gleamynode.net/">Trustin Lee</a>
|
||||
* @version $Rev: 2234 $, $Date: 2010-04-06 18:23:25 +0900 (Tue, 06 Apr 2010) $
|
||||
*/
|
||||
public class SharedResourceMisuseDetector {
|
||||
|
||||
private static final int MAX_ACTIVE_INSTANCES = 256;
|
||||
private static final InternalLogger logger =
|
||||
InternalLoggerFactory.getInstance(SharedResourceMisuseDetector.class);
|
||||
|
||||
private final Class<?> type;
|
||||
private final AtomicLong activeInstances = new AtomicLong();
|
||||
private final AtomicBoolean logged = new AtomicBoolean();
|
||||
|
||||
public SharedResourceMisuseDetector(Class<?> type) {
|
||||
if (type == null) {
|
||||
throw new NullPointerException("type");
|
||||
}
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public void increase() {
|
||||
if (activeInstances.incrementAndGet() > MAX_ACTIVE_INSTANCES) {
|
||||
if (logged.compareAndSet(false, true)) {
|
||||
logger.warn(
|
||||
"You are creating too many " + type.getSimpleName() +
|
||||
" instances. " + type.getSimpleName() +
|
||||
" is a shared resource that must be reused across the" +
|
||||
" application, so that only a few instances are created.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void decrease() {
|
||||
activeInstances.decrementAndGet();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,85 @@
|
|||
/*
|
||||
* Copyright 2009 Red Hat, Inc.
|
||||
*
|
||||
* Red Hat licenses this file to you under the Apache License, version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.jboss.netty.akka.util.internal;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.jboss.netty.akka.util.DebugUtil;
|
||||
|
||||
/**
|
||||
* Simplifies an exception stack trace by removing unnecessary
|
||||
* {@link StackTraceElement}s. Please note that the stack trace simplification
|
||||
* is disabled if {@linkplain DebugUtil debug mode} is turned on.
|
||||
*
|
||||
* @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
|
||||
* @author <a href="http://gleamynode.net/">Trustin Lee</a>
|
||||
*
|
||||
* @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
|
||||
*
|
||||
*/
|
||||
public class StackTraceSimplifier {
|
||||
|
||||
private static final boolean SIMPLIFY_STACK_TRACE = !DebugUtil.isDebugEnabled();
|
||||
private static final Pattern EXCLUDED_STACK_TRACE =
|
||||
Pattern.compile(
|
||||
"^org\\.jboss\\.netty\\." +
|
||||
"(util\\.(ThreadRenamingRunnable|internal\\.DeadLockProofWorker)" +
|
||||
"|channel\\.(SimpleChannel(Upstream|Downstream)?Handler|(Default|Static)ChannelPipeline.*))(\\$.*)?$");
|
||||
|
||||
/**
|
||||
* Removes unnecessary {@link StackTraceElement}s from the specified
|
||||
* exception. {@link ThreadRenamingRunnable}, {@link SimpleChannelHandler},
|
||||
* {@link DefaultChannelPipeline}, and {@link StaticChannelPipeline}
|
||||
* will be dropped from the trace.
|
||||
*/
|
||||
public static void simplify(Throwable e) {
|
||||
if (!SIMPLIFY_STACK_TRACE) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (e.getCause() != null) {
|
||||
simplify(e.getCause());
|
||||
}
|
||||
|
||||
StackTraceElement[] trace = e.getStackTrace();
|
||||
if (trace == null || trace.length == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Perhaps Netty bug. Let us not strip things out.
|
||||
if (EXCLUDED_STACK_TRACE.matcher(trace[0].getClassName()).matches()) {
|
||||
return;
|
||||
}
|
||||
|
||||
List<StackTraceElement> simpleTrace =
|
||||
new ArrayList<StackTraceElement>(trace.length);
|
||||
|
||||
simpleTrace.add(trace[0]);
|
||||
|
||||
// Remove unnecessary stack trace elements.
|
||||
for (int i = 1; i < trace.length; i ++) {
|
||||
if (EXCLUDED_STACK_TRACE.matcher(trace[i].getClassName()).matches()) {
|
||||
continue;
|
||||
}
|
||||
simpleTrace.add(trace[i]);
|
||||
}
|
||||
|
||||
e.setStackTrace(
|
||||
simpleTrace.toArray(new StackTraceElement[simpleTrace.size()]));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,89 @@
|
|||
/*
|
||||
* Copyright 2009 Red Hat, Inc.
|
||||
*
|
||||
* Red Hat licenses this file to you under the Apache License, version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.jboss.netty.akka.util.internal;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Accesses the system property swallowing a {@link SecurityException}.
|
||||
*
|
||||
* @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
|
||||
* @author <a href="http://gleamynode.net/">Trustin Lee</a>
|
||||
*
|
||||
* @version $Rev: 2161 $, $Date: 2010-02-18 11:12:15 +0900 (Thu, 18 Feb 2010) $
|
||||
*
|
||||
*/
|
||||
public class SystemPropertyUtil {
|
||||
|
||||
/**
|
||||
* Returns the value of the Java system property with the specified
|
||||
* {@code key}.
|
||||
*
|
||||
* @return the property value.
|
||||
* {@code null} if there's no such property or if an access to the
|
||||
* specified property is not allowed.
|
||||
*/
|
||||
public static String get(String key) {
|
||||
try {
|
||||
return System.getProperty(key);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value of the Java system property with the specified
|
||||
* {@code key}, while falling back to the specified default value if
|
||||
* the property access fails.
|
||||
*
|
||||
* @return the property value.
|
||||
* {@code def} if there's no such property or if an access to the
|
||||
* specified property is not allowed.
|
||||
*/
|
||||
public static String get(String key, String def) {
|
||||
String value = get(key);
|
||||
if (value == null) {
|
||||
value = def;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value of the Java system property with the specified
|
||||
* {@code key}, while falling back to the specified default value if
|
||||
* the property access fails.
|
||||
*
|
||||
* @return the property value.
|
||||
* {@code def} if there's no such property or if an access to the
|
||||
* specified property is not allowed.
|
||||
*/
|
||||
public static int get(String key, int def) {
|
||||
String value = get(key);
|
||||
if (value == null) {
|
||||
return def;
|
||||
}
|
||||
|
||||
if (Pattern.matches("-?[0-9]+", value)) {
|
||||
return Integer.parseInt(value);
|
||||
} else {
|
||||
return def;
|
||||
}
|
||||
}
|
||||
|
||||
private SystemPropertyUtil() {
|
||||
// Unused
|
||||
}
|
||||
}
|
||||
|
|
@ -17,6 +17,7 @@ import akka.routing.Routing
|
|||
import akka.remote.RemoteSupport
|
||||
import akka.serialization.Serialization
|
||||
import java.net.InetSocketAddress
|
||||
import org.jboss.netty.akka.util.HashedWheelTimer
|
||||
|
||||
object AkkaApplication {
|
||||
|
||||
|
|
@ -174,8 +175,11 @@ class AkkaApplication(val name: String, val config: Configuration) extends Actor
|
|||
|
||||
// TODO correctly pull its config from the config
|
||||
val dispatcherFactory = new Dispatchers(this)
|
||||
|
||||
implicit val dispatcher = dispatcherFactory.defaultGlobalDispatcher
|
||||
def terminationFuture: Future[ExitStatus] = provider.terminationFuture
|
||||
|
||||
// Start the scheduler before the provider (to prevent null pointers from happening in e.g. Gossiper)
|
||||
val scheduler = new DefaultScheduler(new HashedWheelTimer)
|
||||
|
||||
// TODO think about memory consistency effects when doing funky stuff inside constructor
|
||||
val reflective = new ReflectiveAccess(this)
|
||||
|
|
@ -183,6 +187,10 @@ class AkkaApplication(val name: String, val config: Configuration) extends Actor
|
|||
// TODO think about memory consistency effects when doing funky stuff inside constructor
|
||||
val provider: ActorRefProvider = reflective.createProvider
|
||||
|
||||
def terminationFuture: Future[ExitStatus] = provider.terminationFuture
|
||||
|
||||
terminationFuture.onComplete(_ ⇒ scheduler.stop())
|
||||
|
||||
private class Guardian extends Actor {
|
||||
def receive = {
|
||||
case Terminated(_) ⇒ context.self.stop()
|
||||
|
|
@ -236,9 +244,6 @@ class AkkaApplication(val name: String, val config: Configuration) extends Actor
|
|||
// TODO think about memory consistency effects when doing funky stuff inside constructor
|
||||
val serialization = new Serialization(this)
|
||||
|
||||
val scheduler = new DefaultScheduler
|
||||
terminationFuture.onComplete(_ ⇒ scheduler.shutdown())
|
||||
|
||||
// TODO shutdown all that other stuff, whatever that may be
|
||||
def stop(): Unit = {
|
||||
guardian.stop()
|
||||
|
|
|
|||
|
|
@ -5,11 +5,9 @@
|
|||
package akka.actor
|
||||
|
||||
import akka.dispatch._
|
||||
import akka.util._
|
||||
import scala.annotation.tailrec
|
||||
import scala.collection.immutable.{ Stack, TreeMap }
|
||||
import scala.collection.JavaConverters
|
||||
import java.util.concurrent.{ ScheduledFuture, TimeUnit }
|
||||
import java.util.concurrent.TimeUnit
|
||||
import akka.AkkaApplication
|
||||
import akka.event.Logging.{ Debug, Warning, Error }
|
||||
|
||||
|
|
@ -77,7 +75,7 @@ private[akka] class ActorCell(
|
|||
|
||||
final def provider = app.provider
|
||||
|
||||
var futureTimeout: Option[ScheduledFuture[AnyRef]] = None
|
||||
var futureTimeout: Option[org.jboss.netty.akka.util.Timeout] = None
|
||||
|
||||
var _children = emptyChildren //Reuse same empty instance to avoid allocating new instance of the Ordering and the actual empty instance for every actor
|
||||
|
||||
|
|
@ -335,7 +333,7 @@ private[akka] class ActorCell(
|
|||
|
||||
final def cancelReceiveTimeout() {
|
||||
if (futureTimeout.isDefined) {
|
||||
futureTimeout.get.cancel(true)
|
||||
futureTimeout.get.cancel()
|
||||
futureTimeout = None
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,11 +4,10 @@
|
|||
package akka.actor
|
||||
|
||||
import akka.util._
|
||||
import akka.event.Logging
|
||||
|
||||
import scala.collection.mutable
|
||||
import java.util.concurrent.ScheduledFuture
|
||||
import akka.AkkaApplication
|
||||
import akka.event.Logging
|
||||
|
||||
object FSM {
|
||||
|
||||
|
|
@ -31,7 +30,7 @@ object FSM {
|
|||
case class TimeoutMarker(generation: Long)
|
||||
|
||||
case class Timer(name: String, msg: Any, repeat: Boolean, generation: Int)(implicit app: AkkaApplication) {
|
||||
private var ref: Option[ScheduledFuture[AnyRef]] = _
|
||||
private var ref: Option[org.jboss.netty.akka.util.Timeout] = _
|
||||
|
||||
def schedule(actor: ActorRef, timeout: Duration) {
|
||||
if (repeat) {
|
||||
|
|
@ -43,7 +42,7 @@ object FSM {
|
|||
|
||||
def cancel {
|
||||
if (ref.isDefined) {
|
||||
ref.get.cancel(true)
|
||||
ref.get.cancel()
|
||||
ref = None
|
||||
}
|
||||
}
|
||||
|
|
@ -393,7 +392,7 @@ trait FSM[S, D] extends ListenerManagement {
|
|||
* FSM State data and current timeout handling
|
||||
*/
|
||||
private var currentState: State = _
|
||||
private var timeoutFuture: Option[ScheduledFuture[AnyRef]] = None
|
||||
private var timeoutFuture: Option[org.jboss.netty.akka.util.Timeout] = None
|
||||
private var generation: Long = 0L
|
||||
|
||||
/*
|
||||
|
|
@ -458,7 +457,7 @@ trait FSM[S, D] extends ListenerManagement {
|
|||
case t @ Timer(name, msg, repeat, gen) ⇒
|
||||
if ((timers contains name) && (timers(name).generation == gen)) {
|
||||
if (timeoutFuture.isDefined) {
|
||||
timeoutFuture.get.cancel(true)
|
||||
timeoutFuture.get.cancel()
|
||||
timeoutFuture = None
|
||||
}
|
||||
generation += 1
|
||||
|
|
@ -476,7 +475,7 @@ trait FSM[S, D] extends ListenerManagement {
|
|||
removeListener(actorRef)
|
||||
case value ⇒ {
|
||||
if (timeoutFuture.isDefined) {
|
||||
timeoutFuture.get.cancel(true)
|
||||
timeoutFuture.get.cancel()
|
||||
timeoutFuture = None
|
||||
}
|
||||
generation += 1
|
||||
|
|
|
|||
|
|
@ -15,132 +15,82 @@
|
|||
*/
|
||||
package akka.actor
|
||||
|
||||
import akka.AkkaException
|
||||
import java.util.concurrent.atomic.AtomicLong
|
||||
import java.util.concurrent._
|
||||
import akka.util.Duration
|
||||
import org.jboss.netty.akka.util.{ HashedWheelTimer, TimerTask }
|
||||
import akka.AkkaException
|
||||
import org.jboss.netty.akka.util.{ Timeout ⇒ TimeOut }
|
||||
|
||||
case class SchedulerException(msg: String, e: Throwable) extends AkkaException(msg, e) {
|
||||
def this(msg: String) = this(msg, null)
|
||||
}
|
||||
|
||||
trait JScheduler {
|
||||
def schedule(receiver: ActorRef, message: Any, initialDelay: Long, delay: Long, timeUnit: TimeUnit): ScheduledFuture[AnyRef]
|
||||
def scheduleOnce(runnable: Runnable, delay: Long, timeUnit: TimeUnit): ScheduledFuture[AnyRef]
|
||||
def scheduleOnce(receiver: ActorRef, message: Any, delay: Long, timeUnit: TimeUnit): ScheduledFuture[AnyRef]
|
||||
def schedule(receiver: ActorRef, message: Any, initialDelay: Long, delay: Long, timeUnit: TimeUnit): TimeOut
|
||||
def scheduleOnce(runnable: Runnable, delay: Long, timeUnit: TimeUnit): TimeOut
|
||||
def scheduleOnce(receiver: ActorRef, message: Any, delay: Long, timeUnit: TimeUnit): TimeOut
|
||||
}
|
||||
|
||||
abstract class Scheduler extends JScheduler {
|
||||
def schedule(f: () ⇒ Unit, initialDelay: Long, delay: Long, timeUnit: TimeUnit): TimeOut
|
||||
|
||||
def schedule(f: () ⇒ Unit, initialDelay: Long, delay: Long, timeUnit: TimeUnit): ScheduledFuture[AnyRef]
|
||||
def scheduleOnce(f: () ⇒ Unit, delay: Long, timeUnit: TimeUnit): ScheduledFuture[AnyRef]
|
||||
def scheduleOnce(f: () ⇒ Unit, delay: Long, timeUnit: TimeUnit): TimeOut
|
||||
|
||||
def schedule(receiver: ActorRef, message: Any, initialDelay: Duration, delay: Duration): ScheduledFuture[AnyRef] =
|
||||
def schedule(receiver: ActorRef, message: Any, initialDelay: Duration, delay: Duration): TimeOut =
|
||||
schedule(receiver, message, initialDelay.toNanos, delay.toNanos, TimeUnit.NANOSECONDS)
|
||||
|
||||
def schedule(f: () ⇒ Unit, initialDelay: Duration, delay: Duration): ScheduledFuture[AnyRef] =
|
||||
def schedule(f: () ⇒ Unit, initialDelay: Duration, delay: Duration): TimeOut =
|
||||
schedule(f, initialDelay.toNanos, delay.toNanos, TimeUnit.NANOSECONDS)
|
||||
|
||||
def scheduleOnce(receiver: ActorRef, message: Any, delay: Duration): ScheduledFuture[AnyRef] =
|
||||
def scheduleOnce(receiver: ActorRef, message: Any, delay: Duration): TimeOut =
|
||||
scheduleOnce(receiver, message, delay.length, delay.unit)
|
||||
|
||||
def scheduleOnce(f: () ⇒ Unit, delay: Duration): ScheduledFuture[AnyRef] =
|
||||
def scheduleOnce(f: () ⇒ Unit, delay: Duration): TimeOut =
|
||||
scheduleOnce(f, delay.length, delay.unit)
|
||||
}
|
||||
|
||||
class DefaultScheduler extends Scheduler {
|
||||
private def createSendRunnable(receiver: ActorRef, message: Any, throwWhenReceiverExpired: Boolean): Runnable = new Runnable {
|
||||
def run = {
|
||||
receiver ! message
|
||||
if (throwWhenReceiverExpired && receiver.isShutdown) throw new ActorKilledException("Receiver was terminated")
|
||||
class DefaultScheduler(hashedWheelTimer: HashedWheelTimer) extends Scheduler {
|
||||
def schedule(receiver: ActorRef, message: Any, initialDelay: Long, delay: Long, timeUnit: TimeUnit): TimeOut =
|
||||
hashedWheelTimer.newTimeout(createContinuousTask(receiver, message, delay, timeUnit), initialDelay, timeUnit)
|
||||
|
||||
def scheduleOnce(runnable: Runnable, delay: Long, timeUnit: TimeUnit): TimeOut =
|
||||
hashedWheelTimer.newTimeout(createSingleTask(runnable), delay, timeUnit)
|
||||
|
||||
def scheduleOnce(receiver: ActorRef, message: Any, delay: Long, timeUnit: TimeUnit): TimeOut =
|
||||
hashedWheelTimer.newTimeout(createSingleTask(receiver, message), delay, timeUnit)
|
||||
|
||||
def schedule(f: () ⇒ Unit, initialDelay: Long, delay: Long, timeUnit: TimeUnit): TimeOut =
|
||||
hashedWheelTimer.newTimeout(createContinuousTask(f, delay, timeUnit), initialDelay, timeUnit)
|
||||
|
||||
def scheduleOnce(f: () ⇒ Unit, delay: Long, timeUnit: TimeUnit): TimeOut =
|
||||
hashedWheelTimer.newTimeout(createSingleTask(f), delay, timeUnit)
|
||||
|
||||
private def createSingleTask(runnable: Runnable): TimerTask =
|
||||
new TimerTask() { def run(timeout: org.jboss.netty.akka.util.Timeout) { runnable.run() } }
|
||||
|
||||
private def createSingleTask(receiver: ActorRef, message: Any) =
|
||||
new TimerTask { def run(timeout: org.jboss.netty.akka.util.Timeout) { receiver ! message } }
|
||||
|
||||
private def createContinuousTask(receiver: ActorRef, message: Any, delay: Long, timeUnit: TimeUnit) = {
|
||||
new TimerTask {
|
||||
def run(timeout: org.jboss.netty.akka.util.Timeout) {
|
||||
receiver ! message
|
||||
timeout.getTimer.newTimeout(this, delay, timeUnit)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private[akka] val service = Executors.newSingleThreadScheduledExecutor(SchedulerThreadFactory)
|
||||
private def createSingleTask(f: () ⇒ Unit): TimerTask =
|
||||
new TimerTask { def run(timeout: org.jboss.netty.akka.util.Timeout) { f() } }
|
||||
|
||||
/**
|
||||
* Schedules to send the specified message to the receiver after initialDelay and then repeated after delay.
|
||||
* The returned java.util.concurrent.ScheduledFuture can be used to cancel the
|
||||
* send of the message.
|
||||
*/
|
||||
def schedule(receiver: ActorRef, message: Any, initialDelay: Long, delay: Long, timeUnit: TimeUnit): ScheduledFuture[AnyRef] = {
|
||||
try {
|
||||
service.scheduleAtFixedRate(createSendRunnable(receiver, message, true), initialDelay, delay, timeUnit).asInstanceOf[ScheduledFuture[AnyRef]]
|
||||
} catch {
|
||||
case e: Exception ⇒ throw SchedulerException(message + " could not be scheduled on " + receiver, e)
|
||||
private def createContinuousTask(f: () ⇒ Unit, delay: Long, timeUnit: TimeUnit): TimerTask = {
|
||||
new TimerTask {
|
||||
def run(timeout: org.jboss.netty.akka.util.Timeout) {
|
||||
f()
|
||||
timeout.getTimer.newTimeout(this, delay, timeUnit)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedules to run specified function to the receiver after initialDelay and then repeated after delay,
|
||||
* avoid blocking operations since this is executed in the schedulers thread.
|
||||
* The returned java.util.concurrent.ScheduledFuture can be used to cancel the
|
||||
* execution of the function.
|
||||
*/
|
||||
def schedule(f: () ⇒ Unit, initialDelay: Long, delay: Long, timeUnit: TimeUnit): ScheduledFuture[AnyRef] =
|
||||
schedule(new Runnable { def run = f() }, initialDelay, delay, timeUnit)
|
||||
|
||||
/**
|
||||
* Schedules to run specified runnable to the receiver after initialDelay and then repeated after delay,
|
||||
* avoid blocking operations since this is executed in the schedulers thread.
|
||||
* The returned java.util.concurrent.ScheduledFuture can be used to cancel the
|
||||
* execution of the runnable.
|
||||
*/
|
||||
def schedule(runnable: Runnable, initialDelay: Long, delay: Long, timeUnit: TimeUnit): ScheduledFuture[AnyRef] = {
|
||||
try {
|
||||
service.scheduleAtFixedRate(runnable, initialDelay, delay, timeUnit).asInstanceOf[ScheduledFuture[AnyRef]]
|
||||
} catch {
|
||||
case e: Exception ⇒ throw SchedulerException("Failed to schedule a Runnable", e)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedules to send the specified message to the receiver after delay.
|
||||
* The returned java.util.concurrent.ScheduledFuture can be used to cancel the
|
||||
* send of the message.
|
||||
*/
|
||||
def scheduleOnce(receiver: ActorRef, message: Any, delay: Long, timeUnit: TimeUnit): ScheduledFuture[AnyRef] = {
|
||||
try {
|
||||
service.schedule(createSendRunnable(receiver, message, false), delay, timeUnit).asInstanceOf[ScheduledFuture[AnyRef]]
|
||||
} catch {
|
||||
case e: Exception ⇒ throw SchedulerException(message + " could not be scheduleOnce'd on " + receiver, e)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedules a function to be run after delay,
|
||||
* avoid blocking operations since the runnable is executed in the schedulers thread.
|
||||
* The returned java.util.concurrent.ScheduledFuture can be used to cancel the
|
||||
* execution of the function.
|
||||
*/
|
||||
def scheduleOnce(f: () ⇒ Unit, delay: Long, timeUnit: TimeUnit): ScheduledFuture[AnyRef] =
|
||||
scheduleOnce(new Runnable { def run = f() }, delay, timeUnit)
|
||||
|
||||
/**
|
||||
* Schedules a runnable to be run after delay,
|
||||
* avoid blocking operations since the runnable is executed in the schedulers thread.
|
||||
* The returned java.util.concurrent.ScheduledFuture can be used to cancel the
|
||||
* execution of the runnable.
|
||||
*/
|
||||
def scheduleOnce(runnable: Runnable, delay: Long, timeUnit: TimeUnit): ScheduledFuture[AnyRef] = {
|
||||
try {
|
||||
service.schedule(runnable, delay, timeUnit).asInstanceOf[ScheduledFuture[AnyRef]]
|
||||
} catch {
|
||||
case e: Exception ⇒ throw SchedulerException("Failed to scheduleOnce a Runnable", e)
|
||||
}
|
||||
}
|
||||
|
||||
private[akka] def shutdown() { service.shutdownNow() }
|
||||
}
|
||||
|
||||
private object SchedulerThreadFactory extends ThreadFactory {
|
||||
private val count = new AtomicLong(0)
|
||||
val threadFactory = Executors.defaultThreadFactory()
|
||||
|
||||
def newThread(r: Runnable): Thread = {
|
||||
val thread = threadFactory.newThread(r)
|
||||
thread.setName("akka:scheduler-" + count.incrementAndGet())
|
||||
thread.setDaemon(true)
|
||||
thread
|
||||
}
|
||||
}
|
||||
private[akka] def stop() = hashedWheelTimer.stop()
|
||||
}
|
||||
|
|
@ -21,7 +21,7 @@ import java.util.{ LinkedList ⇒ JLinkedList }
|
|||
import scala.annotation.tailrec
|
||||
import scala.collection.mutable.Stack
|
||||
import akka.util.{ Switch, Duration, BoxedType }
|
||||
import java.util.concurrent.atomic.{ AtomicReferenceFieldUpdater, AtomicInteger, AtomicReference, AtomicBoolean }
|
||||
import java.util.concurrent.atomic.{ AtomicReferenceFieldUpdater, AtomicInteger, AtomicBoolean }
|
||||
|
||||
class FutureTimeoutException(message: String, cause: Throwable = null) extends AkkaException(message, cause) {
|
||||
def this(message: String) = this(message, null)
|
||||
|
|
@ -985,7 +985,7 @@ class DefaultPromise[T](val timeout: Timeout)(implicit val dispatcher: MessageDi
|
|||
}
|
||||
}
|
||||
val timeoutFuture = dispatcher.app.scheduler.scheduleOnce(runnable, timeLeft(), NANOS)
|
||||
onComplete(_ ⇒ timeoutFuture.cancel(true))
|
||||
onComplete(_ ⇒ timeoutFuture.cancel())
|
||||
false
|
||||
} else true
|
||||
} else false
|
||||
|
|
|
|||
|
|
@ -109,7 +109,7 @@ class Gossiper(remote: Remote) {
|
|||
private val failureDetector = remote.failureDetector
|
||||
private val connectionManager = new RemoteConnectionManager(app, remote, Map.empty[InetSocketAddress, ActorRef])
|
||||
private val seeds = Set(address) // FIXME read in list of seeds from config
|
||||
private val scheduler = new DefaultScheduler
|
||||
private val scheduler = app.scheduler
|
||||
|
||||
private val address = app.defaultAddress
|
||||
private val nodeFingerprint = address.##
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue