Search in sources :

Example 1 with ThreadModel

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();
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) ArrayList(java.util.ArrayList) Date(java.util.Date) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) ThreadInfo(java.lang.management.ThreadInfo) ArrayList(java.util.ArrayList) List(java.util.List) ThreadModel(com.serotonin.m2m2.web.mvc.rest.v1.model.thread.ThreadModel) ThreadModelProperty(com.serotonin.m2m2.web.mvc.rest.v1.model.thread.ThreadModelProperty) SimpleDateFormat(java.text.SimpleDateFormat) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)1 ThreadModel (com.serotonin.m2m2.web.mvc.rest.v1.model.thread.ThreadModel)1 ThreadModelProperty (com.serotonin.m2m2.web.mvc.rest.v1.model.thread.ThreadModelProperty)1 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)1 ThreadInfo (java.lang.management.ThreadInfo)1 ThreadMXBean (java.lang.management.ThreadMXBean)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 List (java.util.List)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1