This project has retired. For details please refer to its Attic page.
WaitingQueue xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.chukwa.datacollection.agent;
20  
21  
22  import java.util.List;
23  import java.util.concurrent.BlockingQueue;
24  import java.util.concurrent.LinkedBlockingQueue;
25  import org.apache.hadoop.chukwa.Chunk;
26  import org.apache.hadoop.chukwa.datacollection.ChunkQueue;
27  import org.apache.log4j.Logger;
28  
29  public class WaitingQueue implements ChunkQueue {
30  
31    static Logger log = Logger.getLogger(WaitingQueue.class);
32    private BlockingQueue<Chunk> queue = new LinkedBlockingQueue<Chunk>(5);
33  
34    public void add(Chunk event) {
35      try {
36        this.queue.put(event);
37      } catch (InterruptedException e) {
38      }// return upwards
39    }
40  
41    public void add(List<Chunk> events) {
42      this.queue.addAll(events);
43  
44    }
45  
46    public void collect(List<Chunk> events, int maxCount) {
47      // Workaround to block on the queue
48      try {
49        events.add(this.queue.take());
50      } catch (InterruptedException e) {
51      }
52      this.queue.drainTo(events, maxCount - 1);
53  
54      System.out.println("collect [" + Thread.currentThread().getName() + "] ["
55          + events.size() + "]");
56  
57      if (log.isDebugEnabled()) {
58        log.debug("WaitingQueue.inQueueCount:" + queue.size()
59            + "\tWaitingQueue.collectCount:" + events.size());
60      }
61    }
62  
63    public int size() {
64      return queue.size();
65    }
66  
67  }