Permalink
Browse files

Migrated from JSON/JSON-Simple to scala-json (patched for Scala 2.8).

  • Loading branch information...
1 parent af4a8fb commit 83a926ec2f3e8751957c4b84c696ab0508e0576c @cscotta committed Oct 3, 2010
View
Binary file not shown.
@@ -11,11 +11,10 @@ class OctobotProject(info: ProjectInfo) extends DefaultWebProject(info) {
// dependencies
val specs = "org.scala-tools.testing" % "specs_2.8.0" % "1.6.5"
val log4j = "log4j" % "log4j" % "1.2.16"
- val jsonSimple = "com.googlecode.json-simple" % "json-simple" % "1.1"
- val json = "org.json" % "json" % "20090211"
val amqpClient = "com.rabbitmq" % "amqp-client" % "2.1.0"
val mail = "javax.mail" % "mail" % "1.4.3"
val jvyaml = "net.java.dev" % "jvyaml" % "0.2.1"
// beanstalkclient in lib/
+ // scala-json in lib/
// jedis in lib/
}
@@ -1,10 +1,11 @@
package com.urbanairship.octobot
-import java.util.{HashMap, LinkedList, ArrayList}
+import java.util.{ArrayList, LinkedList}
+import scala.collection.mutable.{HashMap}
import java.io.{OutputStream, IOException}
import java.net.{Socket, ServerSocket}
import java.lang.management.{RuntimeMXBean, ManagementFactory}
-import org.json.{JSONObject, JSONTokener}
+import com.twitter.json._
import org.apache.log4j.Logger
import scala.collection.JavaConversions._
@@ -65,28 +66,28 @@ class Introspector() extends Runnable {
var taskRetries: HashMap[String, Int] = null
Metrics.metricsLock.synchronized {
- executionTimes = new HashMap[String, LinkedList[Long]](Metrics.executionTimes)
- taskSuccesses = new HashMap[String, Int](Metrics.taskSuccesses)
- taskFailures = new HashMap[String, Int](Metrics.taskFailures)
- taskRetries = new HashMap[String, Int](Metrics.taskRetries)
+ executionTimes = Metrics.executionTimes.clone
+ taskSuccesses = Metrics.taskSuccesses.clone
+ taskFailures = Metrics.taskFailures.clone
+ taskRetries = Metrics.taskRetries.clone
instrumentedTasks = new ArrayList[String](Metrics.instrumentedTasks)
}
// Build a JSON object for each task we've instrumented.
instrumentedTasks.foreach { taskName =>
- val task = new JSONObject()
+ val task = new HashMap[String, Any]
task.put("successes", taskSuccesses.get(taskName))
task.put("failures", taskFailures.get(taskName))
task.put("retries", taskRetries.get(taskName))
- task.put("average_time", average(executionTimes.get(taskName)))
+ task.put("average_time", average(executionTimes.getOrElse(taskName, null)))
metrics.put("task_" + taskName, task)
}
metrics.put("mail_queue_size", MailQueue.size().asInstanceOf[AnyRef])
metrics.put("tasks_instrumented", instrumentedTasks.size().asInstanceOf[AnyRef])
metrics.put("alive_since", (mx.getUptime() / 1000).asInstanceOf[AnyRef])
- new JSONObject(metrics).toString
+ Json.build(metrics).body
}
@@ -1,11 +1,10 @@
package com.urbanairship.octobot
import java.io.{PrintWriter, StringWriter}
-import org.json.{JSONObject, JSONTokener}
+import com.twitter.json._
import org.apache.log4j.Logger
import com.urbanairship.octobot.consumers._
-
// This thread opens a streaming connection to a queue, which continually
// pushes messages to Octobot queue workers. The tasks contained within these
// messages are invoked, then acknowledged and removed from the queue.
@@ -34,7 +33,7 @@ object QueueConsumer {
// reflection, accounting for non-existent tasks and errors while running.
def invokeTask(rawMessage: String) : Boolean = {
var taskName = ""
- var message : JSONObject = null
+ var message: Map[String, AnyVal] = null
var retryCount = 0
var retryTimes = 0
@@ -49,11 +48,11 @@ object QueueConsumer {
logger.info("Retrying task. Attempt " + retryCount + " of " + retryTimes)
try {
- message = new JSONObject(new JSONTokener(rawMessage))
- taskName = message.get("task").asInstanceOf[String]
+ message = Json.parse(rawMessage).asInstanceOf[Map[String, AnyVal]]
+ taskName = message.get("task").get.toString
- if (message.has("retries"))
- retryTimes = message.get("retries").asInstanceOf[Int]
+ if (message.contains("retries"))
+ retryTimes = message.get("retries").get.asInstanceOf[Int]
} catch {
case ex: Exception => {
@@ -1,20 +1,20 @@
package com.urbanairship.octobot
-import scala.collection.mutable
+import com.twitter.json._
import java.lang.reflect.Method
-import org.json.JSONObject
+import scala.collection.mutable.HashMap
// This class is responsible for the actual invocation of a task.
// Given a task name and a JSON object to be passed onto it, the "execute"
// method looks up the task and method to invoke based on the name and calls it,
// then caches the method lookup.
object TaskExecutor {
- val taskCache = new mutable.HashMap[String, Method]
- val argClass = new JSONObject().getClass
+ val taskCache = new HashMap[String, Method]
+ val argClass = new HashMap[String, AnyVal].getClass
// Invokes a task identified by class name with a message.
- def execute(taskName: String, message: JSONObject) {
+ def execute(taskName: String, message: Map[String, AnyVal]) {
val method: Method = {
taskCache.getOrElse(taskName, {
@@ -1,7 +1,6 @@
package com.urbanairship.octobot.consumers
import java.io.IOException
-import org.json.JSONObject
import org.apache.log4j.Logger
import com.urbanairship.octobot.{Queue, QueueConsumer}
import com.rabbitmq.client.{Channel, Connection, ConnectionFactory, QueueingConsumer}

0 comments on commit 83a926e

Please sign in to comment.