001package org.apache.commons.jcs3.auxiliary.remote.server; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.rmi.Remote; 023import java.rmi.RemoteException; 024import java.rmi.registry.Registry; 025 026import org.apache.commons.jcs3.auxiliary.remote.RemoteUtils; 027import org.apache.commons.jcs3.engine.logging.behavior.ICacheEventLogger; 028import org.apache.commons.jcs3.log.Log; 029import org.apache.commons.jcs3.log.LogManager; 030 031/** 032 * This class tries to keep the registry alive. If if is able to create a registry, it will also 033 * rebind the remote cache server. 034 * @deprecated Functionality moved to RemoteCacheServerFactory 035 */ 036@Deprecated 037public class RegistryKeepAliveRunner 038 implements Runnable 039{ 040 /** The logger */ 041 private static final Log log = LogManager.getLog( RegistryKeepAliveRunner.class ); 042 043 /** the host on which to start the registry */ 044 private final String registryHost; 045 046 /** the port on which to start the registry */ 047 private final int registryPort; 048 049 /** An optional event logger */ 050 private ICacheEventLogger cacheEventLogger; 051 052 /** the registry */ 053 private Registry registry; 054 055 /** 056 * @param registryHost - Hostname of the registry 057 * @param registryPort - the port on which to start the registry 058 * @param serviceName 059 */ 060 public RegistryKeepAliveRunner( final String registryHost, final int registryPort, final String serviceName ) 061 { 062 this.registryHost = registryHost; 063 this.registryPort = registryPort; 064 } 065 066 /** 067 * Tries to lookup the server. If unsuccessful it will rebind the server using the factory 068 * rebind method. 069 * <p> 070 */ 071 @Override 072 public void run() 073 { 074 checkAndRestoreIfNeeded(); 075 } 076 077 /** 078 * Tries to lookup the server. If unsuccessful it will rebind the server using the factory 079 * rebind method. 080 */ 081 protected void checkAndRestoreIfNeeded() 082 { 083 RemoteCacheServerFactory.keepAlive(registryHost, registryPort, cacheEventLogger); 084 } 085 086 /** 087 * Creates the registry and registers the server. 088 * <p> 089 * @param serviceName the service name 090 */ 091 protected void createAndRegister( final String serviceName ) 092 { 093 createReqistry( serviceName ); 094 registerServer( serviceName ); 095 } 096 097 /** 098 * Try to create the registry. Log errors 099 * <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}