use of com.serotonin.m2m2.web.mvc.rest.v1.model.thread.ThreadModel in project ma-modules-public by infiniteautomation.
the class ThreadMonitorRestController method getThreads.
@ApiOperation(value = "Get all threads", notes = "Larger stack depth will slow this request")
@RequestMapping(method = RequestMethod.GET, produces = { "application/json" })
public ResponseEntity<List<ThreadModel>> getThreads(HttpServletRequest request, @ApiParam(value = "Limit size of stack trace", allowMultiple = false, defaultValue = "10") @RequestParam(value = "stackDepth", defaultValue = "10") int stackDepth, @ApiParam(value = "Return as file", allowMultiple = false, defaultValue = "false") @RequestParam(value = "asFile", defaultValue = "false") boolean asFile, @ApiParam(value = "Order by this member", allowMultiple = false, required = false) @RequestParam(value = "orderBy", required = false) String orderBy) {
RestProcessResult<List<ThreadModel>> result = new RestProcessResult<List<ThreadModel>>(HttpStatus.OK);
this.checkUser(request, result);
if (result.isOk()) {
List<ThreadModel> models = new ArrayList<ThreadModel>();
Thread[] allThreads = this.getAllThreads();
ThreadMXBean manager = ManagementFactory.getThreadMXBean();
for (Thread t : allThreads) {
ThreadInfo info = manager.getThreadInfo(t.getId(), stackDepth);
ThreadModel model;
if (info != null)
model = new ThreadModel(t.getId(), t.getPriority(), t.getName(), info, manager.getThreadCpuTime(t.getId()), manager.getThreadUserTime(t.getId()));
else
model = new ThreadModel(t.getId(), t.getPriority(), t.getName(), manager.getThreadCpuTime(t.getId()), manager.getThreadUserTime(t.getId()));
models.add(model);
}
// Do we need to order this list?
if (orderBy != null) {
// Determine what to order by
final ThreadModelProperty orderProperty = ThreadModelProperty.convert(orderBy);
Collections.sort(models, new Comparator<ThreadModel>() {
@Override
public int compare(ThreadModel left, ThreadModel right) {
switch(orderProperty) {
case PRIORITY:
return left.getPriority() - right.getPriority();
case NAME:
return left.getName().compareTo(right.getName());
case CPU_TIME:
if (left.getCpuTime() > right.getCpuTime())
return 1;
else if ((left.getCpuTime() < right.getCpuTime()))
return -1;
else
return 0;
case USER_TIME:
if (left.getUserTime() > right.getUserTime())
return 1;
else if ((left.getUserTime() < right.getUserTime()))
return -1;
else
return 0;
case STATE:
return left.getState().compareTo(right.getState());
case LOCATION:
case ID:
default:
return (int) (left.getId() - right.getId());
}
}
});
}
if (asFile) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd-HH_mm_ss_'threads.json'");
String filename = sdf.format(new Date());
result.addHeader("Content-Disposition", "inline;filename=" + filename);
return result.createResponseEntity(models, MediaType.APPLICATION_OCTET_STREAM);
} else
return result.createResponseEntity(models);
}
return result.createResponseEntity();
}
Aggregations