1 package org.apache.commons.jcs3.auxiliary.remote.server;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
33
34
35
36 @Deprecated
37 public class RegistryKeepAliveRunner
38 implements Runnable
39 {
40
41 private static final Log log = LogManager.getLog( RegistryKeepAliveRunner.class );
42
43
44 private final String registryHost;
45
46
47 private final int registryPort;
48
49
50 private ICacheEventLogger cacheEventLogger;
51
52
53 private Registry registry;
54
55
56
57
58
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
68
69
70
71 @Override
72 public void run()
73 {
74 checkAndRestoreIfNeeded();
75 }
76
77
78
79
80
81 protected void checkAndRestoreIfNeeded()
82 {
83 RemoteCacheServerFactory.keepAlive(registryHost, registryPort, cacheEventLogger);
84 }
85
86
87
88
89
90
91 protected void createAndRegister( final String serviceName )
92 {
93 createReqistry( serviceName );
94 registerServer( serviceName );
95 }
96
97
98
99
100
101
102 protected void createReqistry( final String serviceName )
103 {
104
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
124
125
126
127 protected void registerServer( final String serviceName )
128 {
129 try
130 {
131
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
156
157
158
159 public void setCacheEventLogger( final ICacheEventLogger cacheEventLogger )
160 {
161 this.cacheEventLogger = cacheEventLogger;
162 }
163 }