View Javadoc
1   package org.apache.commons.jcs.auxiliary.remote;
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.ConcurrentHashMap;
23  
24  import org.apache.commons.jcs.auxiliary.AbstractAuxiliaryCacheMonitor;
25  
26  /**
27   * Used to monitor and repair any failed connection for the remote cache service. By default the
28   * monitor operates in a failure driven mode. That is, it goes into a wait state until there is an
29   * error.
30   *
31   * TODO consider moving this into an active monitoring mode. Upon the notification of a
32   * connection error, the monitor changes to operate in a time driven mode. That is, it attempts to
33   * recover the connections on a periodic basis. When all failed connections are restored, it changes
34   * back to the failure driven mode.
35   */
36  public class RemoteCacheMonitor extends AbstractAuxiliaryCacheMonitor
37  {
38      /**
39       * Map of managers to monitor
40       */
41      private ConcurrentHashMap<RemoteCacheManager, RemoteCacheManager> managers;
42  
43      /** Constructor for the RemoteCacheMonitor object */
44      public RemoteCacheMonitor()
45      {
46          super("JCS-RemoteCacheMonitor");
47          this.managers = new ConcurrentHashMap<RemoteCacheManager, RemoteCacheManager>();
48          setIdlePeriod(30000L);
49      }
50  
51      /**
52       * Add a manager to be monitored
53       *
54       * @param manager the remote cache manager
55       */
56      public void addManager(RemoteCacheManager manager)
57      {
58          this.managers.put(manager, manager);
59  
60          // if not yet started, go ahead
61          if (this.getState() == Thread.State.NEW)
62          {
63              this.start();
64          }
65      }
66  
67      /**
68       * Clean up all resources before shutdown
69       */
70      @Override
71      public void dispose()
72      {
73          this.managers.clear();
74      }
75  
76      // Avoid the use of any synchronization in the process of monitoring for
77      // performance reason.
78      // If exception is thrown owing to synchronization,
79      // just skip the monitoring until the next round.
80      /** Main processing method for the RemoteCacheMonitor object */
81      @Override
82      public void doWork()
83      {
84          // Monitor each RemoteCacheManager instance one after the other.
85          // Each RemoteCacheManager corresponds to one remote connection.
86          for (RemoteCacheManager mgr : managers.values())
87          {
88              // If we can't fix them, just skip and re-try in
89              // the next round.
90              if ( mgr.canFixCaches() )
91              {
92                  mgr.fixCaches();
93              }
94              else
95              {
96                  allright.set(false);
97              }
98          }
99      }
100 }