Guest User

Untitled

a guest
Jun 22nd, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.04 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package notificator;
  6.  
  7. import com.mongodb.BasicDBList;
  8. import com.mongodb.BasicDBObject;
  9. import com.mongodb.DBObject;
  10. import com.mongodb.util.JSON;
  11. import java.util.ArrayList;
  12.  
  13. /**
  14.  *
  15.  * @author mm
  16.  */
  17. public class OnlineUsers implements Runnable {
  18.     public static ArrayList queue = new ArrayList();;
  19.     private Boolean required = true;
  20.     private Query query ;
  21.     private static int offset = 0;
  22.     private Boolean inited = false;
  23.     public OnlineUsers() {
  24.        query = new Query();
  25.      
  26.     }
  27.    
  28.     public void run()  {
  29.        
  30.       while(true) {
  31.          try {
  32.             if(!required) {
  33.                   System.err.println("user pool stoped");
  34.                   synchronized(this) {
  35.                       this.wait();
  36.                   }
  37.             }
  38.            
  39.          } catch (InterruptedException ex) {
  40.               ex.printStackTrace();
  41.          }
  42.             getUsers(offset);
  43.             System.out.println(queue.size());
  44.          
  45.        }
  46.     }
  47.    
  48.     public void start() {
  49.        required = true;
  50.        System.err.println("user pool started");
  51.         synchronized(this) {
  52.             this.notify();
  53.        }
  54.     }
  55.    
  56.     public void stop() {
  57.         required = false;
  58.     }
  59.    
  60.    
  61.    
  62.    
  63.     public void getUsers(int offset) {
  64.        getProfiles(query.get(Links.USERS+offset));
  65.     }
  66.    
  67.     private void getProfiles(String uids) {
  68.         System.out.println(uids);
  69.        parseUsers(query.get(Links.PROFILES+uids));
  70.        
  71.     }
  72.    
  73.    
  74.     private void addOnline(int user) {
  75.         queue.add(user);
  76.    }
  77.    
  78.     private void parseUsers(String data) {
  79.        DBObject response = (DBObject) JSON.parse(data);
  80.        BasicDBList list = (BasicDBList) response.get("response");
  81.        BasicDBObject user;
  82.        offset+= 1000;
  83.        for(int i = 0; i < list.size(); i++) {
  84.            user = (BasicDBObject)list.get(i);
  85.            if(user.getInt("online") == 1) {
  86.                addOnline(user.getInt("uid"));
  87.            }
  88.        }
  89.        
  90.     }
  91. }
Add Comment
Please, Sign In to add comment