1 /*
2 * Copyright (c) 1998-2002 The Jgroup Team.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16 *
17 */
18
19 package jgroup.relacs.gm;
20
21 import java.rmi.RemoteException;
22
23 import jgroup.core.JgroupException;
24 import jgroup.core.registry.RegistryService;
25 import jgroup.relacs.config.AppConfig;
26
27
28 /**
29 * The <code>LeaseLayer</code> implements a lease based refresh mechanism
30 * towards the dependable registry. This layer must be configured within
31 * a server group, allowing each server to perform refresh calls towards
32 * the dependable registry.
33 *
34 * @author Henning Hommeland
35 * @author Jo Andreas Lind
36 * @author Hein Meling
37 * @since Jgroup 2.1
38 */
39 public class LeaseLayer
40 implements LeaseService, Runnable
41 {
42
43 //////////////////////////////////////////////////////////////////////////////////////////
44 // Private fields
45 //////////////////////////////////////////////////////////////////////////////////////////
46
47 /* The RegistryService of the local group manager */
48 private RegistryService registryService;
49
50 /* The refresh rate used by this layer to update the dependable registry */
51 private long refreshRate = DEFAULT_REFRESH_RATE;
52
53
54 //////////////////////////////////////////////////////////////////////////////////////////
55 // Constructors
56 //////////////////////////////////////////////////////////////////////////////////////////
57
58 /**
59 * Initializes a LeaseLayer.
60 */
61 private LeaseLayer(RegistryService regs)
62 {
63 /* Keep the RegistryService for later use. */
64 registryService = regs;
65 }
66
67
68 //////////////////////////////////////////////////////////////////////////////////////////
69 // Static Factory
70 //////////////////////////////////////////////////////////////////////////////////////////
71
72 public static LeaseLayer getLayer(RegistryService regs)
73 throws RemoteException
74 {
75 return new LeaseLayer(regs);
76 }
77
78
79 //////////////////////////////////////////////////////////////////////////////////////////
80 // Methods from LeaseService
81 //////////////////////////////////////////////////////////////////////////////////////////
82
83 /**
84 * We obtain the refresh rate for the given server and start
85 * the lease refresh thread.
86 */
87 public void addListener(Object listener)
88 {
89 AppConfig app = AppConfig.getApplication(listener);
90 refreshRate = app.getIntParam("LeaseRefresh.refreshRate", DEFAULT_REFRESH_RATE);
91
92 /*
93 * Create a new thread and start it once the iid in
94 * the RegistryLayer is ready.
95 */
96 Thread thrd = new Thread(this, "LeaseThread");
97 thrd.setDaemon(true);
98 thrd.start();
99 }
100
101
102 //////////////////////////////////////////////////////////////////////////////////////////
103 // Methods from Runnable
104 //////////////////////////////////////////////////////////////////////////////////////////
105
106 /**
107 * This method will execute in a new thread,
108 * started from the constructor. It will preform
109 * a refresh every <i>refreshrate</i> seconds.
110 */
111 public void run()
112 {
113 while (true) {
114 /* Run a refresh on the registry */
115 try {
116 registryService.refreshLease();
117 } catch (RemoteException re) {
118 System.out.println(re.getMessage());
119 } catch (JgroupException je) {
120 System.out.println(je.getMessage());
121 }
122
123 /* Sleep for refresh rate milliseconds */
124 try {
125 Thread.sleep(refreshRate);
126 } catch (InterruptedException e) { /* ignore it */ }
127 }
128 }
129
130 } // End LeaseLayer