Пример #1
0
 Worker(ConcurrentSkipListSet<String> queue, Set<String> crawledChars, Realm realm) {
   this.queue = queue;
   this.crawledChars = crawledChars;
   this.charDAO = new PlayerCharacterDAO(realm);
   this.jwow = JWoW.getInstance();
 }
Пример #2
0
    @Override
    public void run() {
      try {
        String chrName = null;

        while (!queue.isEmpty() && jwow.getApiAccessesToday() < jwow.getRequestLimit() - 500) {

          // Odd behavior because by SynchronizedSkipList because
          // pollFirst seems not to be atomic
          // and returns the first element in multiple Threads. the
          // synchronized block fixes this problem.
          // synchronized(queue)
          // {
          // System.out.println(this.getId()+" - size queue: "+queue.size());
          chrName = queue.pollFirst();
          // System.out.println(this.getId()+" - size queue: "+queue.size());
          // }

          if (crawledChars.size() % 20 == 0) {
            System.out.println(
                "Chars to crawl: "
                    + queue.size()
                    + " / crawled: "
                    + crawledChars.size()
                    + " Thread: "
                    + this.getId());
            System.out.println("Playercache size: " + jwow.getPlayerCache().size());
            System.out.println(this.getId() + ": " + chrName);
          }

          // System.out.println(this.getId() + ": " + chrName);
          // check if character was already crawled
          if (crawledChars.contains(chrName)) {
            System.out.println("found already crawled char");
            continue;
          }
          crawledChars.add(chrName);

          // Try to grab character from JWoW Api
          // long time = System.currentTimeMillis();
          PlayerCharacter chr = charDAO.getCharacterByName(chrName);

          // System.out.println("getChar took: "
          // + (System.currentTimeMillis() - time) + "ms");

          if (chr.isShadow()) {
            continue;
          }

          // if the char has a guild than read the name of the members
          Guild guild;
          try {
            guild = chr.getGuild();
          } catch (OutOfApiCallsException e) {
            System.err.println("Limit for daily non-auth api access reached.");
            return;
          }
          if (guild != null) {
            List<String> members = guild.getMemberNames();
            for (String member : members) {
              if (!crawledChars.contains(member) && !queue.contains(member)) {
                queue.add(member);
              }
            }
          }

          // System.out.println("name: " + chrName + "-"
          // + chr.getGuildname()+ "Thread: "+ this.getId());
          // System.out.println(chr);
        }

      } catch (OutOfApiCallsException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      } catch (ApiOfflineException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      } catch (Api404Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }