Search in sources :

Example 1 with JobManager

use of act.job.JobManager in project actframework by actframework.

the class ReflectedHandlerInvoker method handle.

public Result handle(final ActionContext context) {
    if (disabled) {
        return ActNotFound.get();
    }
    if (null != throttleFilter) {
        Result throttleResult = throttleFilter.handle(context);
        if (null != throttleResult) {
            return throttleResult;
        }
    }
    Result result = pluginBeforeHandler.apply(context);
    if (null != result) {
        return result;
    }
    context.setReflectedHandlerInvoker(this);
    app.eventBus().emit(new ReflectedHandlerInvokerInvoke(this, context));
    if (isLargeResponse) {
        context.setLargeResponse();
    }
    if (null != filters) {
        context.fastjsonFilters(filters);
    }
    if (null != features) {
        context.fastjsonFeatures(features);
    }
    if (null != dateFormatPattern) {
        context.dateFormatPattern(dateFormatPattern);
    }
    if (byPassImplicityTemplateVariable && context.state().isHandling()) {
        context.byPassImplicitVariable();
    }
    context.templateChangeListener(templateChangeListener);
    if (noTemplateCache) {
        context.disableTemplateCaching();
    }
    context.currentMethod(method);
    String urlContext = this.controller.urlContext();
    if (S.notBlank(urlContext)) {
        context.urlContext(urlContext);
    }
    String templateContext = this.controller.templateContext();
    if (null != templateContext) {
        context.templateContext(templateContext);
    }
    preventDoubleSubmission(context);
    processForceResponse(context);
    if (forceDataBinding || context.state().isHandling()) {
        ensureJsonDTOGenerated(context);
    }
    final Object controller = controllerInstance(context);
    /*
         * We will send back response immediately when param validation
         * failed in either of the following cases:
         * 1) this is an ajax call
         * 2) the accept content type is **NOT** html
         */
    boolean failOnViolation = context.isAjax() || context.accept() != H.Format.HTML;
    final Object[] params = params(controller, context);
    if (failOnViolation && context.hasViolation()) {
        String msg = context.violationMessage(";");
        return ActBadRequest.create(msg);
    }
    if (async) {
        JobManager jobManager = context.app().jobManager();
        String jobId = jobManager.prepare(new TrackableWorker() {

            @Override
            protected void run(ProgressGauge progressGauge) {
                try {
                    invoke(handler, context, controller, params);
                } catch (Exception e) {
                    logger.warn(e, "Error executing async handler: " + handler);
                }
            }
        });
        context.setJobId(jobId);
        WebSocketConnectionManager wscm = app.getInstance(WebSocketConnectionManager.class);
        wscm.subscribe(context.session(), SimpleProgressGauge.wsJobProgressTag(jobId));
        jobManager.now(jobId);
        return new RenderJSON(C.Map("jobId", jobId));
    }
    try {
        return pluginAfterHandler.apply(invoke(handler, context, controller, params), context);
    } finally {
        if (hasOutputVar) {
            fillOutputVariables(controller, params, context);
        }
    }
}
Also used : WebSocketConnectionManager(act.ws.WebSocketConnectionManager) RenderJSON(org.osgl.mvc.result.RenderJSON) ReflectedHandlerInvokerInvoke(act.handler.event.ReflectedHandlerInvokerInvoke) JobManager(act.job.JobManager) JSONException(com.alibaba.fastjson.JSONException) NotAppliedException(org.osgl.exception.NotAppliedException) Result(org.osgl.mvc.result.Result) TrackableWorker(act.job.TrackableWorker)

Example 2 with JobManager

use of act.job.JobManager in project actframework by actframework.

the class EventBus method callOn.

private <T extends EventObject> void callOn(final T event, List<? extends ActEventListener> listeners, boolean async) {
    if (null == listeners) {
        return;
    }
    JobManager jobManager = null;
    if (async) {
        jobManager = app().jobManager();
    }
    Set<ActEventListener> toBeRemoved = C.newSet();
    for (final ActEventListener l : listeners) {
        if (!async) {
            boolean result = callOn(event, l);
            if (result && once) {
                toBeRemoved.add(l);
            }
        } else {
            jobManager.now(new Runnable() {

                @Override
                public void run() {
                    callOn(event, l);
                }
            });
        }
    }
    if (once && !toBeRemoved.isEmpty()) {
        listeners.removeAll(toBeRemoved);
    }
}
Also used : JobManager(act.job.JobManager)

Example 3 with JobManager

use of act.job.JobManager in project actframework by actframework.

the class EventBus method _emit.

private void _emit(Key key, ConcurrentMap<Key, List<SimpleEventListener>> listeners, boolean async) {
    final List<SimpleEventListener> list = listeners.get(key);
    if (null == list) {
        return;
    }
    final Object[] args = key.args;
    JobManager jobManager = async ? app().jobManager() : null;
    for (final SimpleEventListener listener : list) {
        if (async) {
            jobManager.now(new Runnable() {

                @Override
                public void run() {
                    callOn(listener, args);
                }
            });
        } else {
            callOn(listener, args);
        }
    }
}
Also used : JobManager(act.job.JobManager)

Example 4 with JobManager

use of act.job.JobManager in project actframework by actframework.

the class ControllerByteCodeScannerTest method setup.

@Before
public void setup() throws Exception {
    super.setup();
    controllerScanner = new ControllerByteCodeScanner();
    jobScanner = new JobByteCodeScanner();
    scannerManager = mock(AppCodeScannerManager.class);
    classInfoRepository = mock(ClassInfoRepository.class);
    eventBus = mock(EventBus.class);
    when(mockApp.eventBus()).thenReturn(eventBus);
    jobManager = new JobManager(mockApp);
    classLoader = new TestingAppClassLoader(mockApp);
    $.setProperty(classLoader, classInfoRepository, "classInfoRepository");
    when(mockApp.classLoader()).thenReturn(classLoader);
    infoSrc = classLoader.controllerClassMetaInfoManager();
    when(mockApp.classLoader()).thenReturn(classLoader);
    when(mockApp.scannerManager()).thenReturn(scannerManager);
    when(mockApp.jobManager()).thenReturn(jobManager);
    when(mockAppConfig.possibleControllerClass(anyString())).thenReturn(true);
    when(mockRouter.isActionMethod(anyString(), anyString())).thenReturn(false);
    C.List<AppByteCodeScanner> scanners = $.cast(C.listOf(controllerScanner, jobScanner));
    // C.List<AppByteCodeScanner> scanners = C.list(controllerScanner);
    when(scannerManager.byteCodeScanners()).thenReturn(scanners);
    paramValueLoaderManager = mock(ParamValueLoaderManager.class);
    when(mockApp.service(ParamValueLoaderManager.class)).thenReturn(paramValueLoaderManager);
    controllerScanner.setApp(mockApp);
    jobScanner.setApp(mockApp);
    base = new File("./target/test-classes");
}
Also used : AppCodeScannerManager(act.app.AppCodeScannerManager) C(org.osgl.util.C) AppByteCodeScanner(act.app.AppByteCodeScanner) JobByteCodeScanner(act.job.bytecode.JobByteCodeScanner) ParamValueLoaderManager(act.inject.param.ParamValueLoaderManager) TestingAppClassLoader(act.app.TestingAppClassLoader) EventBus(act.event.EventBus) JobManager(act.job.JobManager) ClassInfoRepository(act.util.ClassInfoRepository) File(java.io.File) Before(org.junit.Before)

Example 5 with JobManager

use of act.job.JobManager in project actframework by actframework.

the class CommanderByteCodeScannerTest method setup.

@Before
public void setup() throws Exception {
    super.setup();
    scanner = new CommanderByteCodeScanner();
    scannerManager = mock(AppCodeScannerManager.class);
    jobManager = mock(JobManager.class);
    classLoader = new TestingAppClassLoader(mockApp);
    dispatcher = new CliDispatcher(mockApp);
    when(mockApp.classLoader()).thenReturn(classLoader);
    infoSrc = classLoader.commanderClassMetaInfoManager();
    when(mockApp.classLoader()).thenReturn(classLoader);
    when(mockApp.cliDispatcher()).thenReturn(dispatcher);
    when(mockApp.scannerManager()).thenReturn(scannerManager);
    when(mockApp.jobManager()).thenReturn(jobManager);
    when(mockAppConfig.possibleControllerClass(anyString())).thenReturn(true);
    when(mockRouter.isActionMethod(anyString(), anyString())).thenReturn(false);
    C.List<AppByteCodeScanner> scanners = $.cast(C.listOf(scanner));
    when(scannerManager.byteCodeScanners()).thenReturn(scanners);
    scanner.setApp(mockApp);
    base = new File("./target/test-classes");
}
Also used : CliDispatcher(act.cli.CliDispatcher) AppCodeScannerManager(act.app.AppCodeScannerManager) C(org.osgl.util.C) AppByteCodeScanner(act.app.AppByteCodeScanner) TestingAppClassLoader(act.app.TestingAppClassLoader) JobManager(act.job.JobManager) File(java.io.File) Before(org.junit.Before)

Aggregations

JobManager (act.job.JobManager)7 AppByteCodeScanner (act.app.AppByteCodeScanner)2 AppCodeScannerManager (act.app.AppCodeScannerManager)2 TestingAppClassLoader (act.app.TestingAppClassLoader)2 TrackableWorker (act.job.TrackableWorker)2 File (java.io.File)2 Before (org.junit.Before)2 C (org.osgl.util.C)2 SysEventId (act.app.event.SysEventId)1 CliDispatcher (act.cli.CliDispatcher)1 EventBus (act.event.EventBus)1 SimpleEventListenerMetaInfo (act.event.meta.SimpleEventListenerMetaInfo)1 ReflectedHandlerInvokerInvoke (act.handler.event.ReflectedHandlerInvokerInvoke)1 ParamValueLoaderManager (act.inject.param.ParamValueLoaderManager)1 JobByteCodeScanner (act.job.bytecode.JobByteCodeScanner)1 ClassInfoRepository (act.util.ClassInfoRepository)1 ProgressGauge (act.util.ProgressGauge)1 WebSocketConnectionManager (act.ws.WebSocketConnectionManager)1 JSONException (com.alibaba.fastjson.JSONException)1 NotAppliedException (org.osgl.exception.NotAppliedException)1