1 package org.apache.commons.jcs3.auxiliary.remote.http.client;
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.io.IOException;
23
24 import org.apache.commons.jcs3.auxiliary.remote.AbstractRemoteAuxiliaryCache;
25 import org.apache.commons.jcs3.auxiliary.remote.behavior.IRemoteCacheListener;
26 import org.apache.commons.jcs3.engine.ZombieCacheServiceNonLocal;
27 import org.apache.commons.jcs3.engine.behavior.ICacheServiceNonLocal;
28 import org.apache.commons.jcs3.log.Log;
29 import org.apache.commons.jcs3.log.LogManager;
30
31 /**
32 * This uses an http client as the service.
33 */
34 public class RemoteHttpCache<K, V>
35 extends AbstractRemoteAuxiliaryCache<K, V>
36 {
37 /** The logger. */
38 private static final Log log = LogManager.getLog( RemoteHttpCache.class );
39
40 /** for error notifications */
41 private final RemoteHttpCacheMonitor monitor;
42
43 /** Keep the child copy here for the restore process. */
44 private final RemoteHttpCacheAttributes remoteHttpCacheAttributes;
45
46 /**
47 * Constructor for the RemoteCache object. This object communicates with a remote cache server.
48 * One of these exists for each region. This also holds a reference to a listener. The same
49 * listener is used for all regions for one remote server. Holding a reference to the listener
50 * allows this object to know the listener id assigned by the remote cache.
51 * <p>
52 * @param remoteHttpCacheAttributes
53 * @param remote
54 * @param listener
55 * @param monitor the cache monitor
56 */
57 public RemoteHttpCache( final RemoteHttpCacheAttributes remoteHttpCacheAttributes, final ICacheServiceNonLocal<K, V> remote,
58 final IRemoteCacheListener<K, V> listener, final RemoteHttpCacheMonitor monitor )
59 {
60 super( remoteHttpCacheAttributes, remote, listener );
61
62 this.remoteHttpCacheAttributes = remoteHttpCacheAttributes;
63 this.monitor = monitor;
64 }
65
66 /**
67 * Nothing right now. This should setup a zombie and initiate recovery.
68 * <p>
69 * @param ex
70 * @param msg
71 * @param eventName
72 * @throws IOException
73 */
74 @Override
75 protected void handleException( final Exception ex, final String msg, final String eventName )
76 throws IOException
77 {
78 // we should not switch if the existing is a zombie.
79 if ( !( getRemoteCacheService() instanceof ZombieCacheServiceNonLocal ) )
80 {
81 final String message = "Disabling remote cache due to error: " + msg;
82 logError( cacheName, "", message );
83 log.error( message, ex );
84
85 setRemoteCacheService( new ZombieCacheServiceNonLocal<>( getRemoteCacheAttributes().getZombieQueueMaxSize() ) );
86
87 monitor.notifyError( this );
88 }
89
90 if ( ex instanceof IOException )
91 {
92 throw (IOException) ex;
93 }
94 throw new IOException( ex.getMessage() );
95 }
96
97 /**
98 * @return url of service
99 */
100 @Override
101 public String getEventLoggingExtraInfo()
102 {
103 return null;
104 }
105
106 /**
107 * @return the remoteHttpCacheAttributes
108 */
109 public RemoteHttpCacheAttributes getRemoteHttpCacheAttributes()
110 {
111 return remoteHttpCacheAttributes;
112 }
113 }