View Javadoc

1   package org.apache.helix.manager.zk;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.concurrent.BlockingQueue;
23  import java.util.concurrent.LinkedBlockingQueue;
24  import java.util.concurrent.atomic.AtomicInteger;
25  
26  import org.I0Itec.zkclient.exception.ZkInterruptedException;
27  import org.apache.log4j.Logger;
28  
29  // copy from ZkEventThread
30  public class ZkCacheEventThread extends Thread
31  {
32  
33    private static final Logger          LOG      =
34                                                      Logger.getLogger(ZkCacheEventThread.class);
35    private final BlockingQueue<ZkCacheEvent> _events  = new LinkedBlockingQueue<ZkCacheEvent>();
36    private static AtomicInteger         _eventId = new AtomicInteger(0);
37  
38    static abstract class ZkCacheEvent
39    {
40  
41      private final String _description;
42  
43      public ZkCacheEvent(String description)
44      {
45        _description = description;
46      }
47  
48      public abstract void run() throws Exception;
49  
50      @Override
51      public String toString()
52      {
53        return "ZkCacheEvent[" + _description + "]";
54      }
55    }
56  
57    ZkCacheEventThread(String name)
58    {
59      setDaemon(true);
60      setName("ZkCache-EventThread-" + getId() + "-" + name);
61    }
62  
63    @Override
64    public void run()
65    {
66      LOG.info("Starting ZkCache event thread.");
67      try
68      {
69        while (!isInterrupted())
70        {
71          ZkCacheEvent zkEvent = _events.take();
72          int eventId = _eventId.incrementAndGet();
73          LOG.debug("Delivering event #" + eventId + " " + zkEvent);
74          try
75          {
76            zkEvent.run();
77          }
78          catch (InterruptedException e)
79          {
80            interrupt();
81          }
82          catch (ZkInterruptedException e)
83          {
84            interrupt();
85          }
86          catch (Throwable e)
87          {
88            LOG.error("Error handling event " + zkEvent, e);
89          }
90          LOG.debug("Delivering event #" + eventId + " done");
91        }
92      }
93      catch (InterruptedException e)
94      {
95        LOG.info("Terminate ZkClient event thread.");
96      }
97    }
98  
99    public void send(ZkCacheEvent event)
100   {
101     if (!isInterrupted())
102     {
103       LOG.debug("New event: " + event);
104       _events.add(event);
105     }
106   }
107 }