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.List;
23  
24  import org.apache.commons.jcs.auxiliary.remote.behavior.IRemoteCacheAttributes;
25  import org.apache.commons.jcs.auxiliary.remote.server.behavior.RemoteType;
26  import org.apache.commons.jcs.engine.CacheStatus;
27  import org.apache.commons.jcs.engine.behavior.ICache;
28  import org.apache.commons.jcs.engine.behavior.ICompositeCacheManager;
29  import org.apache.commons.jcs.engine.behavior.IElementSerializer;
30  import org.apache.commons.jcs.engine.logging.behavior.ICacheEventLogger;
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  
34  /**
35   * Used to provide access to multiple services under nowait protection. Factory should construct
36   * NoWaitFacade to give to the composite cache out of caches it constructs from the varies manager
37   * to lateral services.
38   * <p>
39   * Typically, we only connect to one remote server per facade. We use a list of one
40   * RemoteCacheNoWait.
41   */
42  public class RemoteCacheNoWaitFacade<K, V>
43      extends AbstractRemoteCacheNoWaitFacade<K, V>
44  {
45      /** log instance */
46      private static final Log log = LogFactory.getLog( RemoteCacheNoWaitFacade.class );
47  
48      /** Provide factory instance to RemoteCacheFailoverRunner */
49      private final RemoteCacheFactory cacheFactory;
50  
51      /**
52       * Constructs with the given remote cache, and fires events to any listeners.
53       * <p>
54       * @param noWaits
55       * @param rca
56       * @param cacheEventLogger
57       * @param elementSerializer
58       * @param cacheFactory
59       */
60      public RemoteCacheNoWaitFacade( List<RemoteCacheNoWait<K,V>> noWaits,
61                                      IRemoteCacheAttributes rca,
62                                      ICacheEventLogger cacheEventLogger,
63                                      IElementSerializer elementSerializer,
64                                      RemoteCacheFactory cacheFactory)
65      {
66          super( noWaits, rca, cacheEventLogger, elementSerializer );
67          this.cacheFactory = cacheFactory;
68      }
69  
70      /**
71       * Constructs with the given remote cache, and fires events to any listeners.
72       * <p>
73       * @param noWaits
74       * @param rca
75       * @param cacheMgr
76       * @param cacheEventLogger
77       * @param elementSerializer
78       * @param cacheFactory
79       * @deprecated Unused parameter cacheMgr scheduled for removal
80       */
81      @Deprecated
82      public RemoteCacheNoWaitFacade( List<ICache<K, V>> noWaits,
83                                      RemoteCacheAttributes rca,
84                                      ICompositeCacheManager cacheMgr,
85                                      ICacheEventLogger cacheEventLogger,
86                                      IElementSerializer elementSerializer,
87                                      RemoteCacheFactory cacheFactory)
88      {
89          super( noWaits, rca, cacheMgr, cacheEventLogger, elementSerializer );
90          this.cacheFactory = cacheFactory;
91      }
92  
93      /**
94       * Begin the failover process if this is a local cache. Clustered remote caches do not failover.
95       * <p>
96       * @param rcnw The no wait in error.
97       */
98      @Override
99      protected void failover( RemoteCacheNoWait<K, V> rcnw )
100     {
101         if ( log.isDebugEnabled() )
102         {
103             log.debug( "in failover for " + rcnw );
104         }
105 
106         if ( getAuxiliaryCacheAttributes().getRemoteType() == RemoteType.LOCAL )
107         {
108             if ( rcnw.getStatus() == CacheStatus.ERROR )
109             {
110                 // start failover, primary recovery process
111                 RemoteCacheFailoverRunner<K, V> runner = new RemoteCacheFailoverRunner<K, V>( this, this.cacheFactory );
112                 runner.setDaemon( true );
113                 runner.start();
114                 runner.notifyError();
115 
116                 if ( getCacheEventLogger() != null )
117                 {
118                     getCacheEventLogger().logApplicationEvent( "RemoteCacheNoWaitFacade", "InitiatedFailover",
119                                                                rcnw + " was in error." );
120                 }
121             }
122             else
123             {
124                 if ( log.isInfoEnabled() )
125                 {
126                     log.info( "The noWait is not in error" );
127                 }
128             }
129         }
130     }
131 
132 }