Search in sources :

Example 1 with CommandResponse

use of com.shulie.instrument.simulator.api.CommandResponse in project LinkAgent by shulieTech.

the class ConfigFetcherModule method getSimulatorDetail.

/**
 * 获取simulator配置的接口,目前只获取静默开关状态
 * @param args
 * @return
 */
@Command("getSimulatorDetail")
public CommandResponse<Object> getSimulatorDetail(Map<String, String> args) {
    SimulatorDetail simulatorDetail = new SimulatorDetail();
    simulatorDetail.setIsSilent(PradarSwitcher.silenceSwitchOn() == true ? 1 : 0);
    CommandResponse<Object> commandResponse = new CommandResponse<Object>();
    commandResponse.setSuccess(true);
    commandResponse.setResult(simulatorDetail);
    return commandResponse;
}
Also used : SimulatorDetail(com.shulie.instrument.module.config.fetcher.config.SimulatorDetail) CommandResponse(com.shulie.instrument.simulator.api.CommandResponse) Command(com.shulie.instrument.simulator.api.annotation.Command)

Example 2 with CommandResponse

use of com.shulie.instrument.simulator.api.CommandResponse in project LinkAgent by shulieTech.

the class ModuleHttpServlet method doResponse.

private void doResponse(Object content, HttpServletRequest req, HttpServletResponse resp) throws IOException {
    String useApi = StringUtils.trim(req.getParameter("useApi"));
    PrintWriter writer = resp.getWriter();
    try {
        if (Boolean.valueOf(useApi)) {
            String json = JSON.toJSONString(content, SerializerFeature.DisableCircularReferenceDetect);
            resp.setContentType("application/json");
            writer.write(json);
            writer.flush();
        } else {
            if (content instanceof CommandResponse) {
                CommandResponse commandResponse = (CommandResponse) content;
                if (commandResponse.isSuccess()) {
                    writer.write(commandResponse.getResult() == null ? "" : commandResponse.getResult().toString());
                } else {
                    writer.write(commandResponse.getMessage() == null ? "" : commandResponse.getMessage());
                }
            } else {
                writer.write(content == null ? "" : content.toString());
            }
        }
    } finally {
        writer.flush();
        writer.close();
    }
}
Also used : CommandResponse(com.shulie.instrument.simulator.api.CommandResponse) PrintWriter(java.io.PrintWriter)

Example 3 with CommandResponse

use of com.shulie.instrument.simulator.api.CommandResponse in project LinkAgent by shulieTech.

the class ThreadModule method processBlockingThread.

private CommandResponse processBlockingThread(Integer samplingInterval) {
    ThreadUtil.BlockingLockInfo blockingLockInfo = ThreadUtil.findMostBlockingLock();
    if (blockingLockInfo.threadInfo == null) {
        CommandResponse commandResponse = new CommandResponse();
        commandResponse.setSuccess(false);
        commandResponse.setMessage("No most blocking thread found!\n");
        return commandResponse;
    } else {
        CommandResponse commandResponse = new CommandResponse();
        commandResponse.setSuccess(true);
        Map<Long, ThreadStat> allThreadStats = ThreadUtil.getAllThreadStats(samplingInterval);
        commandResponse.setResult(ThreadUtil.getThreadInfo(blockingLockInfo, allThreadStats.get(blockingLockInfo.threadInfo.getThreadId())));
        return commandResponse;
    }
}
Also used : CommandResponse(com.shulie.instrument.simulator.api.CommandResponse) ThreadUtil(com.shulie.instrument.simulator.module.util.ThreadUtil) ThreadStat(com.shulie.instrument.simulator.module.model.thread.ThreadStat)

Example 4 with CommandResponse

use of com.shulie.instrument.simulator.api.CommandResponse in project LinkAgent by shulieTech.

the class DefaultModuleCommandInvoker method invokeCommand.

@Override
public <T> CommandResponse<T> invokeCommand(final String moduleId, String command, Map<String, String> args) {
    final CoreModule coreModule = coreModuleManager.get(moduleId);
    if (coreModule == null) {
        throw new ModuleRuntimeException(moduleId, ModuleRuntimeException.ErrorCode.MODULE_NOT_EXISTED);
    }
    // 匹配对应的方法
    final Method method = matchingModuleMethod(command, coreModule.getModule().getClass());
    if (method == null) {
        throw new ModuleRuntimeException(moduleId, ModuleRuntimeException.ErrorCode.MODULE_COMMAND_NOT_EXISTED);
    }
    // 自动释放I/O资源
    final List<Closeable> autoCloseResources = coreModule.append(new ReleaseResource<List<Closeable>>(new ArrayList<Closeable>()) {

        @Override
        public void release() {
            final List<Closeable> closeables = get();
            if (CollectionUtils.isEmpty(closeables)) {
                return;
            }
            for (final Closeable closeable : get()) {
                if (closeable instanceof Flushable) {
                    try {
                        ((Flushable) closeable).flush();
                    } catch (Exception cause) {
                        logger.warn("SIMULATOR: moduleId={} flush I/O occur error!", moduleId, cause);
                    }
                }
                try {
                    closeable.close();
                } catch (IOException e) {
                }
            }
        }
    });
    // 生成方法调用参数
    final Object[] parameterObjectArray = generateParameterObjectArray(method, args);
    final boolean isAccessible = method.isAccessible();
    final ClassLoader oriThreadContextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        method.setAccessible(true);
        Thread.currentThread().setContextClassLoader(coreModule.getClassLoaderFactory().getDefaultClassLoader());
        Object value = method.invoke(coreModule.getModule(), parameterObjectArray);
        if (logger.isDebugEnabled()) {
            logger.debug("SIMULATOR: invoke module {} method {} success.", moduleId, method.getName());
        }
        return (CommandResponse) value;
    } catch (IllegalAccessException iae) {
        logger.warn("SIMULATOR: invoke module {} method {} occur access denied.", moduleId, method.getName(), iae);
        throw new SimulatorException(iae);
    } catch (InvocationTargetException ite) {
        logger.warn("SIMULATOR: invoke module {} method {} occur error.", moduleId, method.getName(), ite.getTargetException());
        final Throwable targetCause = ite.getTargetException();
        throw new SimulatorException(targetCause);
    } finally {
        Thread.currentThread().setContextClassLoader(oriThreadContextClassLoader);
        method.setAccessible(isAccessible);
        coreModule.release(autoCloseResources);
    }
}
Also used : Closeable(java.io.Closeable) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) IOException(java.io.IOException) CommandResponse(com.shulie.instrument.simulator.api.CommandResponse) Flushable(java.io.Flushable) SimulatorException(com.shulie.instrument.simulator.core.exception.SimulatorException) ModuleRuntimeException(com.shulie.instrument.simulator.api.ModuleRuntimeException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ModuleRuntimeException(com.shulie.instrument.simulator.api.ModuleRuntimeException) ArrayList(java.util.ArrayList) List(java.util.List) CoreModule(com.shulie.instrument.simulator.core.CoreModule) SimulatorException(com.shulie.instrument.simulator.core.exception.SimulatorException)

Aggregations

CommandResponse (com.shulie.instrument.simulator.api.CommandResponse)4 SimulatorDetail (com.shulie.instrument.module.config.fetcher.config.SimulatorDetail)1 ModuleRuntimeException (com.shulie.instrument.simulator.api.ModuleRuntimeException)1 Command (com.shulie.instrument.simulator.api.annotation.Command)1 CoreModule (com.shulie.instrument.simulator.core.CoreModule)1 SimulatorException (com.shulie.instrument.simulator.core.exception.SimulatorException)1 ThreadStat (com.shulie.instrument.simulator.module.model.thread.ThreadStat)1 ThreadUtil (com.shulie.instrument.simulator.module.util.ThreadUtil)1 Closeable (java.io.Closeable)1 Flushable (java.io.Flushable)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1