PageRenderTime 2846ms CodeModel.GetById 32ms RepoModel.GetById 6ms app.codeStats 0ms

/app/jobs/JobInvoker.java

https://bitbucket.org/iliax/onlinechecker
Java | 145 lines | 100 code | 30 blank | 15 comment | 8 complexity | acee81536d80551e525c4482e1482cfe MD5 | raw file
Possible License(s): LGPL-3.0
  1. package jobs;
  2. import static util.VkMotitorConstants.*;
  3. import static util.StaticDataHolder.*;
  4. import java.net.UnknownHostException;
  5. import java.util.ArrayList;
  6. import java.util.Arrays;
  7. import java.util.Collections;
  8. import java.util.Date;
  9. import java.util.HashMap;
  10. import java.util.LinkedList;
  11. import java.util.List;
  12. import java.util.Map;
  13. import java.util.Vector;
  14. import java.util.concurrent.ExecutorService;
  15. import java.util.concurrent.Executors;
  16. import java.util.concurrent.locks.Lock;
  17. import java.util.concurrent.locks.ReentrantLock;
  18. import org.bson.BSONObject;
  19. import com.google.gson.Gson;
  20. import com.google.gson.JsonArray;
  21. import com.google.gson.JsonElement;
  22. import com.google.gson.JsonObject;
  23. import com.google.gson.JsonParser;
  24. import com.mongodb.BasicDBObject;
  25. import com.mongodb.DB;
  26. import com.mongodb.DBCollection;
  27. import com.mongodb.DBCursor;
  28. import com.mongodb.DBObject;
  29. import com.mongodb.Mongo;
  30. import com.mongodb.MongoException;
  31. import com.mongodb.util.JSON;
  32. import com.sun.java.swing.plaf.windows.WindowsBorders.DashedBorder;
  33. import dbstuff.DbLogger.LEVEL;
  34. import play.Logger;
  35. import play.Play;
  36. import play.cache.Cache;
  37. import play.jobs.Every;
  38. import play.jobs.Job;
  39. import play.jobs.JobsPlugin;
  40. import play.jobs.On;
  41. import play.jobs.OnApplicationStart;
  42. import play.libs.WS.HttpResponse;
  43. import play.libs.WS.WSRequest;
  44. import static util.VkMotitorConstants.*;
  45. public class JobInvoker extends Job {
  46. public static final int INTERVAL=60;
  47. private static List<String> getTargetsList() {
  48. return lsHolder.getTargetUids();
  49. }
  50. public void stop(){
  51. JobsPlugin.executor.remove(this);
  52. }
  53. public void start(){
  54. every(INTERVAL);
  55. }
  56. @Override
  57. public void doJob() throws Exception {
  58. if (JobRunnable.count.get() < THREADS_COUNT) {
  59. dbw.addCheckLogRecord("passed(prev iteration !processed)");
  60. return;
  61. }
  62. //Logger.info("\n -- started at " + new Date().toString());
  63. List<String> targets = getTargetsList();
  64. int part = targets.size() / THREADS_COUNT;
  65. Thread th = new Thread(new JobRunnable(targets.subList(0, part)));
  66. th.setDaemon(true);
  67. th.start();
  68. // th = new Thread(new JobRunnable(targets.subList(part, part * 2)));
  69. // th.setDaemon(true);
  70. // th.start();
  71. //
  72. // th = new Thread(
  73. // new JobRunnable(targets.subList((part * 2), (part * 3))));
  74. // th.setDaemon(true);
  75. // th.start();
  76. //
  77. // th = new Thread(new JobRunnable(targets.subList((part * 3),
  78. // targets.size())));
  79. // th.setDaemon(true);
  80. // th.start();
  81. }
  82. // util
  83. @Deprecated
  84. public static void addOnlineTargetsAfterId(int id, int count) {
  85. for (int j = 0; j < count;) {
  86. StringBuilder sb = new StringBuilder(URL_BEGINING);
  87. for (int ii = 0; ii < 100; ii++) {
  88. sb.append("id" + id++);
  89. sb.append(",");
  90. }
  91. sb.deleteCharAt(sb.length() - 1);
  92. sb.append(URL_ENDING);
  93. WSRequest request = play.libs.WS.url(sb.toString());
  94. HttpResponse resp = request.get();
  95. JsonElement json = new JsonParser().parse(resp.getString());
  96. JsonArray arr = json.getAsJsonObject().get("response")
  97. .getAsJsonArray();
  98. for (JsonElement el : arr) {
  99. String online = el.getAsJsonObject().get("online")
  100. .getAsString();
  101. String uid = el.getAsJsonObject().get("uid").getAsString();
  102. if (online.equals("1")) {
  103. lsHolder.addNew(uid, null);
  104. j += 1;
  105. System.out.println(uid + " added ----------------");
  106. } else if (online.equals("0")) {
  107. System.out.println(uid);
  108. } else {
  109. System.out.println(online + "-fuck");
  110. return;
  111. }
  112. }
  113. }
  114. }
  115. }