This project has retired. For details please refer to its Attic page.
ConsoleOutConnector 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.test;
20  
21  
22  import org.apache.hadoop.chukwa.Chunk;
23  import org.apache.hadoop.chukwa.datacollection.*;
24  import org.apache.hadoop.chukwa.datacollection.agent.*;
25  import org.apache.hadoop.chukwa.datacollection.connector.Connector;
26  
27  import java.nio.charset.Charset;
28  import java.util.*;
29  
30  /**
31   * Output events to stdout. Intended for debugging use.
32   * 
33   */
34  public class ConsoleOutConnector extends Thread implements Connector {
35  
36    final ChukwaAgent agent;
37    volatile boolean shutdown;
38    final boolean silent;
39  
40    public ConsoleOutConnector(ChukwaAgent a) {
41      this(a, false);
42    }
43  
44    public ConsoleOutConnector(ChukwaAgent a, boolean silent) {
45      agent = a;
46      this.silent = silent;
47    }
48  
49    public void run() {
50      try {
51        System.out.println("console connector started");
52        ChunkQueue eventQueue = DataFactory.getInstance().getEventQueue();
53        if (!silent)
54          System.out.println("-------------------");
55  
56        while (!shutdown) {
57          List<Chunk> evts = new ArrayList<Chunk>();
58          eventQueue.collect(evts, 1);
59  
60          for (Chunk e : evts) {
61            if (!silent) {
62              System.out.println("Console out connector got event at offset "
63                  + e.getSeqID());
64              System.out.println("data type was " + e.getDataType());
65              if (e.getData().length > 1000)
66                System.out.println("data length was " + e.getData().length
67                    + ", not printing");
68              else
69                System.out.println(new String(e.getData(), Charset.forName("UTF-8")));
70            }
71  
72            agent.reportCommit(e.getInitiator(), e.getSeqID());
73  
74            if (!silent)
75              System.out.println("-------------------");
76          }
77        }
78      } catch (InterruptedException e) {
79      } // thread is about to exit anyway
80    }
81  
82    public void shutdown() {
83      shutdown = true;
84      this.interrupt();
85    }
86  
87    @Override
88    public void reloadConfiguration() {
89      System.out.println("reloadConfiguration");
90    }
91  
92  }