View Javadoc

1   /*
2    * $Id: DataServiceStatus.java,v 1.1.1.1 2005/02/17 18:37:35 mch Exp $
3    *
4    * (C) Copyright Astrogrid...
5    */
6   
7   package org.astrogrid.dataservice.service;
8   
9   /***
10   * Status information for a datacenter.  Should be instantiated when the
11   * datacenter starts.
12   *
13   * <p>
14   * @author M Hill
15   */
16  
17  import java.util.Date;
18  import org.astrogrid.dataservice.service.DataServer;
19  import org.astrogrid.status.ServiceStatus;
20  import org.astrogrid.status.ServiceStatusSnapshot;
21  import org.astrogrid.status.TaskStatus;
22  
23  public class DataServiceStatus implements Runnable {
24     
25     Date started = new Date();
26  
27     ServiceStatusSnapshot history = null;
28     
29     /*** Interval between automatic snapshots being taken in seconds. 0 or less
30      * indicates stop taking snapshots */
31     long snappingInterval = -1;
32     
33     public DataServiceStatus() {
34     }
35     
36     /***
37      * Creates the appropriate service status snapshot and adds it to the history
38      */
39     protected  ServiceStatusSnapshot takeSnapshot() {
40        ServiceStatusSnapshot snapshot = new ServiceStatusSnapshot();
41        snapshot.setFreeMemory(Runtime.getRuntime().freeMemory());
42        snapshot.setMaxMemory(Runtime.getRuntime().maxMemory());
43        snapshot.setTotalMemory(Runtime.getRuntime().totalMemory());
44        snapshot.setPrevious(history);
45        
46        long numClosedTasks =0;
47        long numTasks = 0;
48        TaskStatus[] tasks = getTasks();
49        for (int i = 0; i < tasks.length; i++) {
50           if (tasks[i].isFinished()) {
51              numClosedTasks++;
52           }
53           else {
54              numTasks++;
55           }
56        }
57        
58        snapshot.setNumTasks(numTasks);
59        snapshot.setNumClosedTasks(numClosedTasks);
60        
61        snapshot.setPrevious(history);
62        history = snapshot;
63        
64        return snapshot;
65     }
66     
67     /*** Return the status's of the various tasks both running
68      * and complete
69      */
70     public TaskStatus[] getTasks() {
71        return (TaskStatus[]) DataServer.querierManager.getAllStatus();
72     }
73     
74  
75     /*** Constructs a ServiceStatus bean from the this status suitable for
76      * returning across the web, etc.
77      */
78     public ServiceStatus getServiceStatus() {
79        ServiceStatus status = new ServiceStatus();
80        status.setStarted(started);
81        status.setFreeMemory(Runtime.getRuntime().freeMemory());
82        status.setMaxMemory(Runtime.getRuntime().maxMemory());
83        status.setTotalMemory(Runtime.getRuntime().totalMemory());
84        status.setTasks(getTasks());
85        status.setPrevious(history);
86        return status;
87     }
88     
89     /*** Instruction to take regular snapshots at the given interval (in seconds) */
90     public void takeSnaps(long interval) {
91        snappingInterval = interval;
92        new Thread(this).start();
93     }
94  
95     /*** Instruction to stop taking regular snapshots  */
96     public void stopSnaps() {
97        snappingInterval = -1;
98     }
99     
100    /***
101     * Use the takeSnaps() method to create regular snapshots and add them to the
102     * history
103     */
104    public void run()  {
105       while (snappingInterval>0) {
106          try {
107             Thread.sleep(snappingInterval*1000);
108          }
109          catch (InterruptedException e)
110          {
111             //ignore
112          }
113          takeSnapshot();
114       }
115    }
116    
117    /*** Return string - which is a getServiceStatus string */
118    public String toString() {
119       return getServiceStatus().toString();
120    }
121 }
122 
123 
124 
125 
126 
127 
128