use of net.yacy.grid.http.ServiceResponse in project yacy_grid_mcp by yacy.
the class StatusService method serviceImpl.
@Override
public ServiceResponse serviceImpl(Query call, HttpServletResponse response) {
// generate json
Runtime runtime = Runtime.getRuntime();
JSONObject json = new JSONObject(true);
JSONObject system = new JSONObject(true);
system.put("service", Service.type.name());
system.put("assigned_memory", runtime.maxMemory());
system.put("used_memory", runtime.totalMemory() - runtime.freeMemory());
system.put("available_memory", runtime.maxMemory() - runtime.totalMemory() + runtime.freeMemory());
system.put("cores", runtime.availableProcessors());
system.put("threads", Thread.activeCount());
system.put("load_system_average", OS.getSystemLoadAverage());
// system.put("load_system_cpu", OS.getSystemCpuLoad());
system.put("load_process_cpu", OS.getProcessCpuLoad());
system.put("server_threads", APIServer.getServerThreads());
JSONObject client_info = new JSONObject(true);
JSONObject request_header = new JSONObject(true);
client_info.put("request_header", request_header);
json.put("system", system);
json.put("client_info", client_info);
return new ServiceResponse(json);
}
use of net.yacy.grid.http.ServiceResponse in project yacy_grid_mcp by yacy.
the class ThreaddumpService method serviceImpl.
@Override
public ServiceResponse serviceImpl(Query post, HttpServletResponse response) {
int multi = post.get("multi", post.get("count", 0));
final StringBuilder buffer = new StringBuilder(1000);
// Thread dump
final Date dt = new Date();
int keylen = 30;
bufferappend(buffer, "************* Start Thread Dump " + dt + " *******************");
bufferappend(buffer, "");
bufferappend(buffer, keylen, "Assigned Memory", Memory.assigned());
bufferappend(buffer, keylen, "Used Memory", Memory.used());
bufferappend(buffer, keylen, "Available Memory", Memory.available());
bufferappend(buffer, keylen, "Short Status", Memory.shortStatus() ? "true" : "false");
bufferappend(buffer, keylen, "Short Threshold", Float.toString(Memory.shortmemthreshold));
bufferappend(buffer, keylen, "Cores", Memory.cores());
bufferappend(buffer, keylen, "Active Thread Count", Thread.activeCount());
bufferappend(buffer, keylen, "Total Started Thread Count", Memory.threadBean.getTotalStartedThreadCount());
bufferappend(buffer, keylen, "Peak Thread Count", Memory.threadBean.getPeakThreadCount());
bufferappend(buffer, keylen, "System Load Average", Memory.osBean.getSystemLoadAverage());
long runtimeseconds = (System.currentTimeMillis() - startupTime) / 1000;
int runtimeminutes = (int) (runtimeseconds / 60);
runtimeseconds = runtimeseconds % 60;
int runtimehours = runtimeminutes / 60;
runtimeminutes = runtimeminutes % 60;
bufferappend(buffer, keylen, "Runtime", runtimehours + "h " + runtimeminutes + "m " + runtimeseconds + "s");
// print system beans
for (Method method : Memory.osBean.getClass().getDeclaredMethods()) try {
method.setAccessible(true);
if (method.getName().startsWith("get") && Modifier.isPublic(method.getModifiers())) {
bufferappend(buffer, keylen, method.getName(), method.invoke(Memory.osBean));
}
} catch (Throwable e) {
}
bufferappend(buffer, "");
bufferappend(buffer, "");
if (multi > 0) {
// generate multiple dumps
final Map<String, Integer> dumps = new HashMap<String, Integer>();
for (int i = 0; i < multi; i++) {
try {
ThreadDump dump = new ThreadDump(ThreadDump.getAllStackTraces(), Thread.State.RUNNABLE);
for (final Map.Entry<StackTrace, SortedSet<String>> e : dump.entrySet()) {
if (multiDumpFilterPattern.matcher(e.getKey().text).matches())
continue;
Integer c = dumps.get(e.getKey().text);
if (c == null)
dumps.put(e.getKey().text, Integer.valueOf(e.getValue().size()));
else {
c = Integer.valueOf(c.intValue() + e.getValue().size());
dumps.put(e.getKey().text, c);
}
}
} catch (final OutOfMemoryError e) {
break;
}
}
// write dumps
while (!dumps.isEmpty()) {
final Map.Entry<String, Integer> e = removeMax(dumps);
bufferappend(buffer, "Occurrences: " + e.getValue());
bufferappend(buffer, e.getKey());
bufferappend(buffer, "");
}
bufferappend(buffer, "");
} else {
// generate a single thread dump
final Map<Thread, StackTraceElement[]> stackTraces = ThreadDump.getAllStackTraces();
// write those ordered into the stackTrace list
for (Thread.State state : ORDERED_STATES) new ThreadDump(stackTraces, state).appendStackTraces(buffer, state);
}
ThreadMXBean threadbean = ManagementFactory.getThreadMXBean();
bufferappend(buffer, "");
bufferappend(buffer, "THREAD LIST FROM ThreadMXBean, " + threadbean.getThreadCount() + " threads:");
bufferappend(buffer, "");
ThreadInfo[] threadinfo = threadbean.dumpAllThreads(true, true);
for (ThreadInfo ti : threadinfo) {
bufferappend(buffer, ti.getThreadName());
}
return new ServiceResponse(buffer.toString());
}
use of net.yacy.grid.http.ServiceResponse in project yacy_grid_mcp by yacy.
the class AvailableService method serviceImpl.
@Override
public ServiceResponse serviceImpl(Query call, HttpServletResponse response) {
String serviceName = call.get("serviceName", "");
String queueName = call.get("queueName", "");
JSONObject json = new JSONObject(true);
if (serviceName.length() > 0 && queueName.length() > 0) {
try {
AvailableContainer available = Data.gridBroker.available(YaCyServices.valueOf(serviceName), new GridQueue(queueName));
String url = available.getFactory().getConnectionURL();
json.put(ObjectAPIHandler.AVAILABLE_KEY, available.getAvailable());
json.put(ObjectAPIHandler.SUCCESS_KEY, true);
if (url != null)
json.put(ObjectAPIHandler.SERVICE_KEY, url);
} catch (IOException e) {
json.put(ObjectAPIHandler.SUCCESS_KEY, false);
json.put(ObjectAPIHandler.COMMENT_KEY, e.getMessage());
}
} else {
json.put(ObjectAPIHandler.SUCCESS_KEY, false);
json.put(ObjectAPIHandler.COMMENT_KEY, "the request must contain a serviceName and a queueName");
}
return new ServiceResponse(json);
}
use of net.yacy.grid.http.ServiceResponse in project yacy_grid_mcp by yacy.
the class ClearService method serviceImpl.
@Override
public ServiceResponse serviceImpl(Query call, HttpServletResponse response) {
String serviceName = call.get("serviceName", "");
String queueName = call.get("queueName", "");
JSONObject json = new JSONObject(true);
if (serviceName.length() > 0 && queueName.length() > 0) {
try {
QueueFactory<byte[]> qf = Data.gridBroker.clear(YaCyServices.valueOf(serviceName), new GridQueue(queueName));
String url = qf.getConnectionURL();
json.put(ObjectAPIHandler.SUCCESS_KEY, true);
if (url != null)
json.put(ObjectAPIHandler.SERVICE_KEY, url);
} catch (IOException e) {
json.put(ObjectAPIHandler.SUCCESS_KEY, false);
json.put(ObjectAPIHandler.COMMENT_KEY, e.getMessage());
}
} else {
json.put(ObjectAPIHandler.SUCCESS_KEY, false);
json.put(ObjectAPIHandler.COMMENT_KEY, "the request must contain a serviceName and a queueName");
}
return new ServiceResponse(json);
}
use of net.yacy.grid.http.ServiceResponse in project yacy_grid_mcp by yacy.
the class ReceiveService method serviceImpl.
@Override
public ServiceResponse serviceImpl(Query call, HttpServletResponse response) {
String serviceName = call.get("serviceName", "");
String queueName = call.get("queueName", "");
long timeout = call.get("timeout", -1);
JSONObject json = new JSONObject(true);
if (serviceName.length() > 0 && queueName.length() > 0) {
try {
MessageContainer<byte[]> message = Data.gridBroker.receive(YaCyServices.valueOf(serviceName), new GridQueue(queueName), timeout);
// message can be null if a timeout occurred
if (message == null) {
json.put(ObjectAPIHandler.SUCCESS_KEY, false);
json.put(ObjectAPIHandler.COMMENT_KEY, "timeout");
} else {
String url = message.getFactory().getConnectionURL();
byte[] payload = message.getPayload();
json.put(ObjectAPIHandler.MESSAGE_KEY, payload == null ? "" : new String(payload, StandardCharsets.UTF_8));
json.put(ObjectAPIHandler.SUCCESS_KEY, true);
if (url != null)
json.put(ObjectAPIHandler.SERVICE_KEY, url);
}
} catch (IOException e) {
json.put(ObjectAPIHandler.SUCCESS_KEY, false);
json.put(ObjectAPIHandler.COMMENT_KEY, e.getMessage());
}
} else {
json.put(ObjectAPIHandler.SUCCESS_KEY, false);
json.put(ObjectAPIHandler.COMMENT_KEY, "the request must contain a serviceName and a queueName");
}
return new ServiceResponse(json);
}
Aggregations