org.beangle.ems.app.log.AsyncBusinessLogger.scala Maven / Gradle / Ivy
/*
* Copyright (C) 2005, The Beangle Software.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see .
*/
package org.beangle.ems.app.log
import org.beangle.commons.bean.{Disposable, Initializing}
import org.beangle.commons.logging.Logging
import org.beangle.ems.app.log.AsyncBusinessLogger.Worker
import java.util as ju
import java.util.concurrent.{ArrayBlockingQueue, BlockingQueue}
object AsyncBusinessLogger {
class Worker(store: AsyncBusinessLogger) extends Thread {
var stopped: Boolean = false
override def run(): Unit = {
while (store.started && !stopped) {
try {
val elements = new ju.ArrayList[BusinessLogEvent]
val e0 = store.queue.take()
elements.add(e0)
store.queue.drainTo(elements)
val iter = elements.iterator()
while (iter.hasNext) {
val e = iter.next()
store.appenders foreach (ap => ap.append(e))
}
} catch {
case _: InterruptedException => stopped = true
}
}
}
}
}
class AsyncBusinessLogger extends BusinessLogger with Initializing with Disposable {
var appenders: List[Appender] = _
var queueSize: Int = 512
private var queue: BlockingQueue[BusinessLogEvent] = _
private var started: Boolean = _
private var worker: Worker = _
override def publish(entry: BusinessLogEvent): Unit = {
queue.offer(entry)
}
override def init(): Unit = {
queue = new ArrayBlockingQueue[BusinessLogEvent](queueSize)
started = true
worker = new Worker(this)
worker.setDaemon(true)
worker.setName("Beangle async business logstore worker")
worker.start()
}
override def destroy(): Unit = {
worker.interrupt()
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy