/push/src/main/scala/com/msg/push/util/RegionInfoMongoHelper.scala

https://github.com/goodrain/realtime-message-system · Scala · 153 lines · 144 code · 8 blank · 1 comment · 29 complexity · 697f16d07421a06cb260918a81b5518a MD5 · raw file

  1. package com.msg.push.util
  2. import com.mongodb.MongoException
  3. import com.typesafe.config.ConfigFactory
  4. import com.mongodb.MongoOptions
  5. import com.mongodb.ServerAddress
  6. import com.mongodb.casbah.MongoConnection
  7. import com.mongodb.casbah.MongoDB
  8. import scala.util.Try
  9. import com.mongodb.casbah.commons.MongoDBObject
  10. import java.util.ArrayList
  11. import com.mongodb.DBObject
  12. import org.bson.BSON
  13. import com.mongodb.BasicDBList
  14. import com.msg.common.model.RegAddrEntity
  15. import com.msg.common.model.TopicInfo
  16. import com.msg.common.model.QueueInfo
  17. import com.msg.common.model.QueueEntity
  18. import com.msg.common.model.TemplateEntity
  19. import com.msg.common.model.TemplateInfo
  20. import com.msg.common.model.TopicEntity
  21. import scala.collection.JavaConversions._
  22. object RegionInfoMongoHelper {
  23. private val conf = ConfigFactory.load("mongdb-region.conf")
  24. val opt = new MongoOptions()
  25. val hosts = conf.getString("region_mongo.host")
  26. val ports = conf.getString("region_mongo.port")
  27. val hostArr = hosts.split(",")
  28. val portArr = ports.split(",")
  29. var addArr = Set.empty[ServerAddress]
  30. for (index <- 0 to hostArr.length - 1) {
  31. addArr += new ServerAddress(hostArr(index), portArr(index).toInt)
  32. }
  33. private val mongoConn = MongoConnection(addArr.toList, opt)
  34. private val dbname = conf.getString("region_mongo.db")
  35. private def mongoDb(dbname: String): MongoDB = {
  36. val db = mongoConn.getDB(dbname)
  37. db
  38. }
  39. private def getAllTopic(collectionName: String): ArrayList[DBObject] = {
  40. var result: ArrayList[DBObject] = new ArrayList[DBObject]
  41. try {
  42. val collection = mongoDb(dbname)(collectionName)
  43. var cursor = collection.find()
  44. while (cursor.hasNext) {
  45. result.add(cursor.next)
  46. }
  47. } catch {
  48. case e: Throwable => e.printStackTrace()
  49. }
  50. result
  51. }
  52. def initTopicInfo(collectionName: String) = {
  53. try {
  54. var dbObjectList = getAllTopic(collectionName)
  55. if (dbObjectList != null && dbObjectList.size() > 0) {
  56. val validTopicMap = new ArrayList[String]
  57. for (index <- 0 to dbObjectList.size() - 1) {
  58. val dbobject = dbObjectList.get(index)
  59. val code = dbobject.get("code").asInstanceOf[String]
  60. val checkStatus = dbobject.get("check_status").asInstanceOf[Int]
  61. if (checkStatus == 1) {
  62. val entityObj = dbobject.get("entity").asInstanceOf[DBObject]
  63. if (entityObj != null && entityObj.keySet().size() > 0) {
  64. val iterator = entityObj.keySet().iterator()
  65. while (iterator.hasNext()) {
  66. val curtopic = iterator.next()
  67. val topic = code + "_" + curtopic
  68. val obj = entityObj.get(curtopic).asInstanceOf[DBObject]
  69. val status = obj.get("status").asInstanceOf[Int]
  70. if (status == 1) {
  71. val status = obj.get("status").asInstanceOf[Int]
  72. val isStore = obj.get("is_store").asInstanceOf[Int]
  73. val key = obj.get("key").asInstanceOf[String]
  74. val storeMethod = obj.get("store_method").asInstanceOf[Int]
  75. val storeNum = obj.get("store_num").asInstanceOf[Int]
  76. val sendNum = obj.get("send_num").asInstanceOf[Int]
  77. val isRegx = obj.get("is_regx").asInstanceOf[Int]
  78. val regxParam = obj.get("regx_param").asInstanceOf[String]
  79. val isTemplate = obj.get("is_replace").asInstanceOf[Int]
  80. val isNotify = obj.get("is_notify").asInstanceOf[Int]
  81. val defaultTemp = obj.get("template_regx").asInstanceOf[String]
  82. val defaultNotify = obj.get("notify_regx").asInstanceOf[String]
  83. val broadStatus = obj.get("broad_status").asInstanceOf[Int]
  84. val topicInfo = TopicInfo(topic, code, key, isStore, storeMethod, sendNum, storeNum, isRegx, status, isTemplate, isNotify, regxParam, defaultTemp, defaultNotify,broadStatus)
  85. TopicEntity.addTopicInfo(topic, topicInfo)
  86. validTopicMap.add(topic)
  87. } else {
  88. TopicEntity.removeTopicInfo(topic)
  89. }
  90. }
  91. }
  92. }
  93. }
  94. if (validTopicMap != null && validTopicMap.size() != TopicEntity.getAllTopics.size()) {
  95. val oldMap = TopicEntity.getAllTopics
  96. oldMap.keySet().foreach(key => {
  97. if (!validTopicMap.contains(key)) {
  98. TopicEntity.removeTopicInfo(key)
  99. }
  100. })
  101. }
  102. } else {
  103. TopicEntity.removeAll
  104. }
  105. } catch {
  106. case e: Throwable => e.printStackTrace()
  107. }
  108. }
  109. private def getAllRegion(collectionName: String): ArrayList[DBObject] = {
  110. var result: ArrayList[DBObject] = new ArrayList[DBObject]
  111. try {
  112. val collection = mongoDb(dbname)(collectionName)
  113. var cursor = collection.find()
  114. while (cursor.hasNext) {
  115. result.add(cursor.next)
  116. }
  117. } catch {
  118. case e: Throwable => e.printStackTrace()
  119. }
  120. result
  121. }
  122. def initAllRegionInfo(collectionName: String) = {
  123. try {
  124. var dbObjectList = getAllRegion(collectionName)
  125. val addressList = new ArrayList[String]()
  126. if (dbObjectList != null && dbObjectList.size() > 0) {
  127. for (index <- 0 to dbObjectList.size() - 1) {
  128. val dbobject = dbObjectList.get(index)
  129. val name = dbobject.get("name").asInstanceOf[String]
  130. val status = dbobject.get("status").asInstanceOf[Int]
  131. if (status == 1) {
  132. RegAddrEntity.addAddress(name)
  133. addressList.add(name)
  134. } else {
  135. RegAddrEntity.removeAddress(name)
  136. }
  137. }
  138. }
  139. RegAddrEntity.mergeAddress(addressList)
  140. //println("valid region address ="+RegAddrEntity.getAllRegionAddress().toString())
  141. } catch {
  142. case e: Throwable => e.printStackTrace()
  143. }
  144. }
  145. }