View Javadoc
1   package org.apache.commons.jcs3.auxiliary.remote.server;
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.rmi.Remote;
23  import java.rmi.RemoteException;
24  import java.rmi.registry.Registry;
25  
26  import org.apache.commons.jcs3.auxiliary.remote.RemoteUtils;
27  import org.apache.commons.jcs3.engine.logging.behavior.ICacheEventLogger;
28  import org.apache.commons.jcs3.log.Log;
29  import org.apache.commons.jcs3.log.LogManager;
30  
31  /**
32   * This class tries to keep the registry alive. If if is able to create a registry, it will also
33   * rebind the remote cache server.
34   * @deprecated Functionality moved to RemoteCacheServerFactory
35   */
36  @Deprecated
37  public class RegistryKeepAliveRunner
38      implements Runnable
39  {
40      /** The logger */
41      private static final Log log = LogManager.getLog( RegistryKeepAliveRunner.class );
42  
43      /** the host on which to start the registry */
44      private final String registryHost;
45  
46      /** the port on which to start the registry */
47      private final int registryPort;
48  
49      /** An optional event logger */
50      private ICacheEventLogger cacheEventLogger;
51  
52      /** the registry */
53      private Registry registry;
54  
55      /**
56       * @param registryHost - Hostname of the registry
57       * @param registryPort - the port on which to start the registry
58       * @param serviceName
59       */
60      public RegistryKeepAliveRunner( final String registryHost, final int registryPort, final String serviceName )
61      {
62          this.registryHost = registryHost;
63          this.registryPort = registryPort;
64      }
65  
66      /**
67       * Tries to lookup the server. If unsuccessful it will rebind the server using the factory
68       * rebind method.
69       * <p>
70       */
71      @Override
72      public void run()
73      {
74          checkAndRestoreIfNeeded();
75      }
76  
77      /**
78       * Tries to lookup the server. If unsuccessful it will rebind the server using the factory
79       * rebind method.
80       */
81      protected void checkAndRestoreIfNeeded()
82      {
83          RemoteCacheServerFactory.keepAlive(registryHost, registryPort, cacheEventLogger);
84      }
85  
86      /**
87       * Creates the registry and registers the server.
88       * <p>
89       * @param serviceName the service name
90       */
91      protected void createAndRegister( final String serviceName )
92      {
93          createReqistry( serviceName );
94          registerServer( serviceName );
95      }
96  
97      /**
98       * Try to create the registry. Log errors
99       * <p>
100      * @param serviceName the service name
101      */
102     protected void createReqistry( final String serviceName )
103     {
104         // TODO: Refactor method signature. This is ugly but required to keep the binary API compatibility
105         this.registry = RemoteUtils.createRegistry(registryPort);
106 
107         if ( cacheEventLogger != null )
108         {
109             if (this.registry != null)
110             {
111                 cacheEventLogger.logApplicationEvent( "RegistryKeepAliveRunner", "createRegistry",
112                         "Successfully created registry [" + serviceName + "]." );
113             }
114             else
115             {
116                 cacheEventLogger.logError( "RegistryKeepAliveRunner", "createRegistry",
117                         "Could not start registry [" + serviceName + "]." );
118             }
119         }
120     }
121 
122     /**
123      * Try to rebind the server.
124      * <p>
125      * @param serviceName the service name
126      */
127     protected void registerServer( final String serviceName )
128     {
129         try
130         {
131             // try to rebind anyway
132             final Remote server = RemoteCacheServerFactory.getRemoteCacheServer();
133             RemoteCacheServerFactory.registerServer(serviceName, server);
134 
135             final String message = "Successfully rebound server to registry [" + serviceName + "].";
136             if ( cacheEventLogger != null )
137             {
138                 cacheEventLogger.logApplicationEvent( "RegistryKeepAliveRunner", "registerServer", message );
139             }
140             log.info( message );
141         }
142         catch ( final RemoteException e )
143         {
144             final String message = "Could not rebind server to registry [" + serviceName + "].";
145             log.error( message, e );
146             if ( cacheEventLogger != null )
147             {
148                 cacheEventLogger.logError( "RegistryKeepAliveRunner", "registerServer", message + ":"
149                     + e.getMessage() );
150             }
151         }
152     }
153 
154     /**
155      * Allows it to be injected.
156      * <p>
157      * @param cacheEventLogger
158      */
159     public void setCacheEventLogger( final ICacheEventLogger cacheEventLogger )
160     {
161         this.cacheEventLogger = cacheEventLogger;
162     }
163 }