Created
October 22, 2012 12:19
-
-
Save nlinker/3931271 to your computer and use it in GitHub Desktop.
Find the thread by name and change its priority
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... | |
val connection = factory.newConnection() | |
changeInnerPriority(connection, | |
config.get[Int]("tq.connectionPriority").getOrElse(Thread.NORM_PRIORITY)) | |
// ... | |
def changeInnerPriority(connection: com.rabbitmq.client.Connection, | |
priority: Int): Unit = { | |
def getHostAddressAsString: String = { | |
connection.getAddress.getHostAddress | |
} | |
// change priority of the thread to reduce CPU load | |
// caused by the infinite loop inside the connection: | |
// TODO [nick] do this hardcoded address is stable enough? | |
val hostAddress = getHostAddressAsString | |
val threadName = "AMQP Connection " + hostAddress + ":" + connection.getPort | |
getThreadByName(threadName) match { | |
case Some(th) => th.setPriority(priority) | |
case None => () | |
} | |
} | |
def getThreadByName(name: String): Option[Thread] = { | |
getAllThreads.find(th => th.getName == name) | |
} | |
def getAllThreads: Array[Thread] = { | |
val rootGroup = rootThreadGroup | |
var threads: Array[Thread] = new Array[Thread](rootGroup.activeCount) | |
// we call enumerate() repeatedly until the array is large enough to | |
// contain all entries, since activeCount does not guarantee to supply us | |
// the exact number | |
while (rootGroup.enumerate(threads, true) == threads.length) { | |
threads = new Array[Thread](threads.length * 2) | |
} | |
threads | |
} | |
def rootThreadGroup: ThreadGroup = { | |
var rootGroup: ThreadGroup = Thread.currentThread().getThreadGroup | |
// go upstairs until we get the root | |
var parentGroup: ThreadGroup = rootGroup | |
while (parentGroup != null) { | |
rootGroup = parentGroup | |
parentGroup = rootGroup.getParent | |
} | |
rootGroup | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment