Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package notificator;
- import com.mongodb.BasicDBList;
- import com.mongodb.BasicDBObject;
- import com.mongodb.DBObject;
- import com.mongodb.util.JSON;
- import java.util.ArrayList;
- /**
- *
- * @author mm
- */
- public class OnlineUsers implements Runnable {
- public static ArrayList queue = new ArrayList();;
- private Boolean required = true;
- private Query query ;
- private static int offset = 0;
- private Boolean inited = false;
- public OnlineUsers() {
- query = new Query();
- }
- public void run() {
- while(true) {
- try {
- if(!required) {
- System.err.println("user pool stoped");
- synchronized(this) {
- this.wait();
- }
- }
- } catch (InterruptedException ex) {
- ex.printStackTrace();
- }
- getUsers(offset);
- System.out.println(queue.size());
- }
- }
- public void start() {
- required = true;
- System.err.println("user pool started");
- synchronized(this) {
- this.notify();
- }
- }
- public void stop() {
- required = false;
- }
- public void getUsers(int offset) {
- getProfiles(query.get(Links.USERS+offset));
- }
- private void getProfiles(String uids) {
- System.out.println(uids);
- parseUsers(query.get(Links.PROFILES+uids));
- }
- private void addOnline(int user) {
- queue.add(user);
- }
- private void parseUsers(String data) {
- DBObject response = (DBObject) JSON.parse(data);
- BasicDBList list = (BasicDBList) response.get("response");
- BasicDBObject user;
- offset+= 1000;
- for(int i = 0; i < list.size(); i++) {
- user = (BasicDBObject)list.get(i);
- if(user.getInt("online") == 1) {
- addOnline(user.getInt("uid"));
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment