1
|
package eu.dnetlib.services;
|
2
|
|
3
|
import java.io.File;
|
4
|
import java.lang.management.ManagementFactory;
|
5
|
import java.lang.management.OperatingSystemMXBean;
|
6
|
|
7
|
/**
|
8
|
* This bean packages the minimum information for describing a service running instance.
|
9
|
*/
|
10
|
public class ServiceRunningInstance {
|
11
|
|
12
|
private String serviceId;
|
13
|
private String baseUrl;
|
14
|
private long freeDiskSpace = 0;
|
15
|
private long totalDiskSpace = 0;
|
16
|
private long freeMemory = 0;
|
17
|
private long totalMemory = 0;
|
18
|
private long maxMemory = 0;
|
19
|
private long usedMemory = 0;
|
20
|
private long numberOfProcessors = 0;
|
21
|
private double sysLoadAvg = 0;
|
22
|
|
23
|
public static ServiceRunningInstance newInstance(final String serviceId, final String baseUrl, final String baseDir) {
|
24
|
return newInstance(new ServiceRunningInstance(), serviceId, baseUrl, baseDir);
|
25
|
}
|
26
|
|
27
|
protected static <T extends ServiceRunningInstance> T newInstance(final T instance,
|
28
|
final String serviceId,
|
29
|
final String baseUrl,
|
30
|
final String baseDir) {
|
31
|
|
32
|
instance.setServiceId(serviceId);
|
33
|
instance.setBaseUrl(baseUrl);
|
34
|
|
35
|
final File f = new File(baseDir);
|
36
|
instance.setFreeDiskSpace(bytesToMegaBytes(f.getFreeSpace()));
|
37
|
instance.setTotalDiskSpace(bytesToMegaBytes(f.getTotalSpace()));
|
38
|
|
39
|
final Runtime runtime = Runtime.getRuntime();
|
40
|
instance.setFreeMemory(bytesToMegaBytes(runtime.freeMemory()));
|
41
|
instance.setTotalMemory(bytesToMegaBytes(runtime.totalMemory()));
|
42
|
instance.setMaxMemory(bytesToMegaBytes(runtime.maxMemory()));
|
43
|
instance.setUsedMemory(bytesToMegaBytes(runtime.totalMemory() - runtime.freeMemory()));
|
44
|
|
45
|
final OperatingSystemMXBean osBean = ManagementFactory.getOperatingSystemMXBean();
|
46
|
instance.setNumberOfProcessors(osBean.getAvailableProcessors());
|
47
|
instance.setSysLoadAvg(osBean.getSystemLoadAverage());
|
48
|
|
49
|
return instance;
|
50
|
}
|
51
|
|
52
|
public ServiceRunningInstance() {}
|
53
|
|
54
|
private static final long bytesToMegaBytes(final long n) {
|
55
|
return n / 1_048_576;
|
56
|
}
|
57
|
|
58
|
public String getServiceId() {
|
59
|
return serviceId;
|
60
|
}
|
61
|
|
62
|
public void setServiceId(final String serviceId) {
|
63
|
this.serviceId = serviceId;
|
64
|
}
|
65
|
|
66
|
public String getBaseUrl() {
|
67
|
return baseUrl;
|
68
|
}
|
69
|
|
70
|
public void setBaseUrl(final String baseUrl) {
|
71
|
this.baseUrl = baseUrl;
|
72
|
}
|
73
|
|
74
|
public long getFreeDiskSpace() {
|
75
|
return freeDiskSpace;
|
76
|
}
|
77
|
|
78
|
public void setFreeDiskSpace(final long freeDiskSpace) {
|
79
|
this.freeDiskSpace = freeDiskSpace;
|
80
|
}
|
81
|
|
82
|
public long getTotalDiskSpace() {
|
83
|
return totalDiskSpace;
|
84
|
}
|
85
|
|
86
|
public void setTotalDiskSpace(final long totalDiskSpace) {
|
87
|
this.totalDiskSpace = totalDiskSpace;
|
88
|
}
|
89
|
|
90
|
public long getFreeMemory() {
|
91
|
return freeMemory;
|
92
|
}
|
93
|
|
94
|
public void setFreeMemory(final long freeMemory) {
|
95
|
this.freeMemory = freeMemory;
|
96
|
}
|
97
|
|
98
|
public long getTotalMemory() {
|
99
|
return totalMemory;
|
100
|
}
|
101
|
|
102
|
public void setTotalMemory(final long totalMemory) {
|
103
|
this.totalMemory = totalMemory;
|
104
|
}
|
105
|
|
106
|
public long getMaxMemory() {
|
107
|
return maxMemory;
|
108
|
}
|
109
|
|
110
|
public void setMaxMemory(final long maxMemory) {
|
111
|
this.maxMemory = maxMemory;
|
112
|
}
|
113
|
|
114
|
public long getUsedMemory() {
|
115
|
return usedMemory;
|
116
|
}
|
117
|
|
118
|
public void setUsedMemory(final long usedMemory) {
|
119
|
this.usedMemory = usedMemory;
|
120
|
}
|
121
|
|
122
|
public long getNumberOfProcessors() {
|
123
|
return numberOfProcessors;
|
124
|
}
|
125
|
|
126
|
public void setNumberOfProcessors(final long numberOfProcessors) {
|
127
|
this.numberOfProcessors = numberOfProcessors;
|
128
|
}
|
129
|
|
130
|
public double getSysLoadAvg() {
|
131
|
return sysLoadAvg;
|
132
|
}
|
133
|
|
134
|
public void setSysLoadAvg(final double sysLoadAvg) {
|
135
|
this.sysLoadAvg = sysLoadAvg;
|
136
|
}
|
137
|
|
138
|
}
|