Changed API for EventHandler and added support for log levels

This commit is contained in:
Jonas Bonér 2011-03-14 10:45:49 +01:00
parent 39caa297ac
commit f8ce3d541e
16 changed files with 69 additions and 35 deletions

View file

@ -97,7 +97,11 @@ class ActorTimeoutException private[akka](message: String) extends AkkaEx
*
* Log an error event:
* <pre>
* EventHandler.notifyListeners(EventHandler.Error(exception, this, message.toString))
* EventHandler.notify(EventHandler.Error(exception, this, message.toString))
* </pre>
* Or use the direct methods (better performance):
* <pre>
* EventHandler.error(exception, this, message.toString)
* </pre>
* @author <a href="http://jonasboner.com">Jonas Bon&#233;r</a>
*/
@ -123,6 +127,35 @@ object EventHandler extends ListenerManagement {
val EventHandlerDispatcher = Dispatchers.newExecutorBasedEventDrivenDispatcher(ID).build
val level: Class[_ <: Event] = config.getString("akka.event-handler-level", "DEBUG") match {
case "ERROR" => classOf[Error]
case "WARNING" => classOf[Warning]
case "INFO" => classOf[Info]
case "DEBUG" => classOf[Debug]
case unknown => throw new ConfigurationException(
"Configuration option 'akka.event-handler-level' is invalid [" + unknown + "]")
}
def notify[T <: Event : ClassManifest](event: => T) {
if (classManifest[T].erasure.asInstanceOf[Class[_ <: Event]] == level) notifyListeners(event)
}
def error(cause: Throwable, instance: AnyRef, message: => String) = {
if (level == classOf[Error]) notifyListeners(Error(cause, instance, message))
}
def warning(instance: AnyRef, message: => String) = {
if (level == classOf[Warning]) notifyListeners(Warning(instance, message))
}
def info(instance: AnyRef, message: => String) = {
if (level == classOf[Info]) notifyListeners(Info(instance, message))
}
def debug(instance: AnyRef, message: => String) = {
if (level == classOf[Debug]) notifyListeners(Debug(instance, message))
}
def formattedTimestamp = DateFormat.getInstance.format(new Date)
def stackTraceFor(e: Throwable) = {

View file

@ -824,7 +824,7 @@ class LocalActorRef private[akka] (
}
} catch {
case e: Throwable =>
EventHandler notifyListeners EventHandler.Error(e, this, messageHandle.message.toString)
EventHandler notify EventHandler.Error(e, this, messageHandle.message.toString)
throw e
}
}
@ -987,7 +987,7 @@ class LocalActorRef private[akka] (
}
private def handleExceptionInDispatch(reason: Throwable, message: Any) = {
EventHandler notifyListeners EventHandler.Error(reason, this, message.toString)
EventHandler notify EventHandler.Error(reason, this, message.toString)
//Prevent any further messages to be processed until the actor has been restarted
dispatcher.suspend(this)

View file

@ -38,7 +38,7 @@ object Scheduler {
initialDelay, delay, timeUnit).asInstanceOf[ScheduledFuture[AnyRef]]
} catch {
case e: Exception =>
EventHandler notifyListeners EventHandler.Error(e, this, receiver + " @ " + message)
EventHandler notify EventHandler.Error(e, this, receiver + " @ " + message)
throw SchedulerException(message + " could not be scheduled on " + receiver, e)
}
}
@ -59,7 +59,7 @@ object Scheduler {
service.scheduleAtFixedRate(runnable, initialDelay, delay, timeUnit).asInstanceOf[ScheduledFuture[AnyRef]]
} catch {
case e: Exception =>
EventHandler notifyListeners EventHandler.Error(e, this)
EventHandler notify EventHandler.Error(e, this)
throw SchedulerException("Failed to schedule a Runnable", e)
}
}
@ -74,7 +74,7 @@ object Scheduler {
delay, timeUnit).asInstanceOf[ScheduledFuture[AnyRef]]
} catch {
case e: Exception =>
EventHandler notifyListeners EventHandler.Error(e, this, receiver + " @ " + message)
EventHandler notify EventHandler.Error(e, this, receiver + " @ " + message)
throw SchedulerException( message + " could not be scheduleOnce'd on " + receiver, e)
}
}
@ -95,7 +95,7 @@ object Scheduler {
service.schedule(runnable,delay, timeUnit).asInstanceOf[ScheduledFuture[AnyRef]]
} catch {
case e: Exception =>
EventHandler notifyListeners EventHandler.Error(e, this)
EventHandler notify EventHandler.Error(e, this)
throw SchedulerException("Failed to scheduleOnce a Runnable", e)
}
}

View file

@ -77,7 +77,7 @@ object Config {
}
} catch {
case e =>
EventHandler notifyListeners EventHandler.Error(e, this)
EventHandler notify EventHandler.Error(e, this)
throw e
}
}

View file

@ -148,7 +148,7 @@ object DataFlow {
(out !! Get).as[T]
} catch {
case e: Exception =>
EventHandler notifyListeners EventHandler.Error(e, this)
EventHandler notify EventHandler.Error(e, this)
out ! Exit
throw e
}

View file

@ -102,7 +102,7 @@ class ExecutorBasedEventDrivenDispatcher(
try executorService.get() execute invocation
catch {
case e: RejectedExecutionException =>
EventHandler notifyListeners EventHandler.Warning(this, e.toString)
EventHandler notify EventHandler.Warning(this, e.toString)
throw e
}
}
@ -141,7 +141,7 @@ class ExecutorBasedEventDrivenDispatcher(
executorService.get() execute mbox
} catch {
case e: RejectedExecutionException =>
EventHandler notifyListeners EventHandler.Warning(this, e.toString)
EventHandler notify EventHandler.Warning(this, e.toString)
mbox.dispatcherLock.unlock()
throw e
}

View file

@ -90,7 +90,7 @@ object Futures {
result completeWithResult r
} catch {
case e: Exception =>
EventHandler notifyListeners EventHandler.Error(e, this)
EventHandler notify EventHandler.Error(e, this)
result completeWithException e
}
}
@ -258,7 +258,7 @@ sealed trait Future[T] {
Right(f(v.right.get))
} catch {
case e: Exception =>
EventHandler notifyListeners EventHandler.Error(e, this)
EventHandler notify EventHandler.Error(e, this)
Left(e)
})
}
@ -286,7 +286,7 @@ sealed trait Future[T] {
f(v.right.get) onComplete (fa.completeWith(_))
} catch {
case e: Exception =>
EventHandler notifyListeners EventHandler.Error(e, this)
EventHandler notify EventHandler.Error(e, this)
fa completeWithException e
}
}
@ -316,7 +316,7 @@ sealed trait Future[T] {
else Left(new MatchError(r))
} catch {
case e: Exception =>
EventHandler notifyListeners EventHandler.Error(e, this)
EventHandler notify EventHandler.Error(e, this)
Left(e)
})
}

View file

@ -36,7 +36,7 @@ final case class FutureInvocation(future: CompletableFuture[Any], function: () =
Right(function.apply)
} catch {
case e: Exception =>
EventHandler notifyListeners EventHandler.Error(e, this)
EventHandler notify EventHandler.Error(e, this)
Left(e)
})
}

View file

@ -208,10 +208,10 @@ class BoundedExecutorDecorator(val executor: ExecutorService, bound: Int) extend
})
} catch {
case e: RejectedExecutionException =>
EventHandler notifyListeners EventHandler.Warning(this, e.toString)
EventHandler notify EventHandler.Warning(this, e.toString)
semaphore.release
case e: Throwable =>
EventHandler notifyListeners EventHandler.Error(e, this)
EventHandler notify EventHandler.Error(e, this)
throw e
}
}

View file

@ -84,7 +84,7 @@ trait DefaultActorPool extends ActorPool
future completeWithResult (delegate !! msg).getOrElse(None)
} catch {
case e =>
EventHandler notifyListeners EventHandler.Error(e, this)
EventHandler notify EventHandler.Error(e, this)
future completeWithException e
}
}

View file

@ -126,7 +126,7 @@ class Switch(startAsOn: Boolean = false) {
action
} catch {
case t: Throwable =>
EventHandler notifyListeners EventHandler.Error(t, this)
EventHandler notify EventHandler.Error(t, this)
switch.compareAndSet(!from, from) //Revert status
throw t
}

View file

@ -389,7 +389,7 @@ trait RequestMethod {
}
} catch {
case io: Exception =>
EventHandler notifyListeners EventHandler.Error(io, this)
EventHandler notify EventHandler.Error(io, this)
false
}
}
@ -408,7 +408,7 @@ trait RequestMethod {
}
} catch {
case io: IOException =>
EventHandler notifyListeners EventHandler.Error(io, this)
EventHandler notify EventHandler.Error(io, this)
}
}

View file

@ -36,7 +36,7 @@ trait Servlet30Context extends AsyncListener {
}
catch {
case ex: IllegalStateException =>
EventHandler notifyListeners EventHandler.Error(ex, this)
EventHandler notify EventHandler.Error(ex, this)
false
}
}

View file

@ -369,7 +369,7 @@ trait SpnegoAuthenticationActor extends AuthenticationActor[SpnegoCredentials] {
Some(UserInfo(user, null, rolesFor(user)))
} catch {
case e: PrivilegedActionException => {
EventHandler notifyListeners EventHandler.Error(e, this)
EventHandler notify EventHandler.Error(e, this)
None
}
}

View file

@ -445,7 +445,7 @@ class ActiveRemoteClientHandler(
}
} catch {
case e: Throwable =>
EventHandler notifyListeners EventHandler.Error(e, this)
EventHandler notify EventHandler.Error(e, this)
client.notifyListeners(RemoteClientError(e, client.module, client.remoteAddress))
throw e
}
@ -502,7 +502,7 @@ class ActiveRemoteClientHandler(
.newInstance(exception.getMessage).asInstanceOf[Throwable]
} catch {
case problem: Throwable =>
EventHandler notifyListeners EventHandler.Error(problem, this)
EventHandler notify EventHandler.Error(problem, this)
UnparsableException(classname, exception.getMessage)
}
}
@ -582,7 +582,7 @@ class NettyRemoteServer(serverModule: NettyRemoteServerModule, val host: String,
serverModule.notifyListeners(RemoteServerShutdown(serverModule))
} catch {
case e: Exception =>
EventHandler notifyListeners EventHandler.Error(e, this)
EventHandler notify EventHandler.Error(e, this)
}
}
}
@ -614,7 +614,7 @@ trait NettyRemoteServerModule extends RemoteServerModule { self: RemoteModule =>
}
} catch {
case e: Exception =>
EventHandler notifyListeners EventHandler.Error(e, this)
EventHandler notify EventHandler.Error(e, this)
notifyListeners(RemoteServerError(e, this))
}
this
@ -923,7 +923,7 @@ class RemoteServerHandler(
val actorRef =
try { createActor(actorInfo, channel).start } catch {
case e: SecurityException =>
EventHandler notifyListeners EventHandler.Error(e, this)
EventHandler notify EventHandler.Error(e, this)
write(channel, createErrorReplyMessage(e, request, AkkaActorType.ScalaActor))
server.notifyListeners(RemoteServerError(e, server))
return
@ -1011,7 +1011,7 @@ class RemoteServerHandler(
write(channel, RemoteEncoder.encode(messageBuilder.build))
} catch {
case e: Exception =>
EventHandler notifyListeners EventHandler.Error(e, this)
EventHandler notify EventHandler.Error(e, this)
server.notifyListeners(RemoteServerError(e, server))
}
@ -1027,11 +1027,11 @@ class RemoteServerHandler(
}
} catch {
case e: InvocationTargetException =>
EventHandler notifyListeners EventHandler.Error(e, this)
EventHandler notify EventHandler.Error(e, this)
write(channel, createErrorReplyMessage(e.getCause, request, AkkaActorType.TypedActor))
server.notifyListeners(RemoteServerError(e, server))
case e: Exception =>
EventHandler notifyListeners EventHandler.Error(e, this)
EventHandler notify EventHandler.Error(e, this)
write(channel, createErrorReplyMessage(e, request, AkkaActorType.TypedActor))
server.notifyListeners(RemoteServerError(e, server))
}
@ -1091,7 +1091,7 @@ class RemoteServerHandler(
actorRef
} catch {
case e: Throwable =>
EventHandler notifyListeners EventHandler.Error(e, this)
EventHandler notify EventHandler.Error(e, this)
server.notifyListeners(RemoteServerError(e, server))
throw e
}
@ -1158,7 +1158,7 @@ class RemoteServerHandler(
newInstance
} catch {
case e: Throwable =>
EventHandler notifyListeners EventHandler.Error(e, this)
EventHandler notify EventHandler.Error(e, this)
server.notifyListeners(RemoteServerError(e, server))
throw e
}

View file

@ -13,6 +13,7 @@ akka {
time-unit = "seconds" # Time unit for all timeout properties throughout the config
event-handlers = ["akka.actor.EventHandler$DefaultListener"] # event handlers to register at boot time (EventHandler$DefaultListener logs to STDOUT)
event-handler-level = "DEBUG" # Options: ERROR, WARING, INFO, DEBUG
# These boot classes are loaded (and created) automatically when the Akka Microkernel boots up
# Can be used to bootstrap your application(s)