1 /*
2 * #%L
3 * Bitrepository Monitoring Service
4 * %%
5 * Copyright (C) 2010 - 2012 The State and University Library, The Royal Library and The State Archives, Denmark
6 * %%
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation, either version 2.1 of the
10 * License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Lesser Public License for more details.
16 *
17 * You should have received a copy of the GNU General Lesser Public
18 * License along with this program. If not, see
19 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
20 * #L%
21 */
22 package org.bitrepository.monitoringservice.collector;
23
24 import java.util.Timer;
25 import java.util.TimerTask;
26
27 import access.getstatus.GetStatusClient;
28 import org.bitrepository.client.eventhandler.EventHandler;
29 import org.bitrepository.common.settings.Settings;
30 import org.bitrepository.monitoringservice.alarm.MonitorAlerter;
31 import org.bitrepository.monitoringservice.status.StatusStore;
32
33 /**
34 * The collector of status messages.
35 */
36 public class StatusCollector {
37 /** The getStatusClient */
38 private GetStatusClient getStatusClient;
39 /** The store for the status results.*/
40 private final StatusStore statusStore;
41 /** The EventHandler */
42 private EventHandler eventHandler;
43 /** Defines that the timer is a daemon thread. */
44 private static final Boolean TIMER_IS_DAEMON = true;
45 /** The name of the timer.*/
46 private static final String NAME_OF_TIMER = "GetStatus collection timer";
47 /** Timer for collecting statuses on a regular basis */
48 private static final Timer timer = new Timer(NAME_OF_TIMER, TIMER_IS_DAEMON);
49 /** Time between getStatus collections */
50 private final long collectionInterval;
51
52 /**
53 * Constructor.
54 * @param getStatusClient The status client.
55 * @param settings The settings.
56 * @param statusStore The storage for the status results.
57 * @param alerter The alerter.
58 */
59 public StatusCollector(GetStatusClient getStatusClient, Settings settings, StatusStore statusStore,
60 MonitorAlerter alerter) {
61 this.getStatusClient = getStatusClient;
62 eventHandler = new GetStatusEventHandler(statusStore, alerter);
63 this.statusStore = statusStore;
64 collectionInterval = settings.getReferenceSettings().getMonitoringServiceSettings().getCollectionInterval();
65 }
66
67 /** Start the collection of statuses */
68 public void start() {
69 timer.schedule(new StatusCollectorTimerTask(), 0, collectionInterval);
70 }
71
72 /** Stop the collection of statuses */
73 public void stop() {
74 timer.cancel();
75 }
76
77 /**
78 * The timertask for collecting the status.
79 * Tells the store that a new status request has been issued, and then starts the conversation for retrieving the
80 * status from all the contributors.
81 */
82 private class StatusCollectorTimerTask extends TimerTask {
83 @Override
84 public void run() {
85 statusStore.updateReplyCounts();
86 getStatusClient.getStatus(eventHandler);
87 }
88 }
89 }