Search in sources :

Example 1 with EventBus

use of act.event.EventBus in project actframework by actframework.

the class EventListener method excute.

public static void excute(MsgTemplate template) {
    EventBus eventBus = App.instance().getInstance(EventBus.class);
    eventBus.trigger("message-send", template);
}
Also used : EventBus(act.event.EventBus)

Example 2 with EventBus

use of act.event.EventBus in project actframework by actframework.

the class NetworkHandler method handle.

public void handle(final ActionContext ctx, final NetworkDispatcher dispatcher) {
    if (isDestroyed()) {
        return;
    }
    Exception refreshError = null;
    if (Act.isDev()) {
        try {
            boolean updated = app.checkUpdates(false);
            if (updated && !app.hasBlockIssue()) {
                app.jobManager().on(SysEventId.POST_START, new Runnable() {

                    @Override
                    public void run() {
                        handle(ctx, dispatcher);
                    }
                }, true);
                dispatcher.keep();
                return;
            }
        } catch (Exception e) {
            refreshError = e;
        }
    }
    final H.Request req = ctx.req();
    String url = req.url();
    H.Method method = req.method();
    url = contentSuffixProcessor.apply(req, url);
    try {
        url = urlContextProcessor.apply(req, url);
    } catch (NotFound notFound) {
        ctx.handler(AlwaysNotFound.INSTANCE);
        ctx.saveLocal();
        AlwaysNotFound.INSTANCE.apply(ctx);
        return;
    }
    Timer timer = metric.startTimer(MetricInfo.ROUTING);
    final RequestHandler requestHandler = router().getInvoker(method, url, ctx);
    ctx.handler(requestHandler);
    timer.stop();
    boolean resourceGetter = requestHandler instanceof ResourceGetter || requestHandler instanceof FileGetter;
    if (null != refreshError && !resourceGetter) {
        ctx.saveLocal();
        handleException(refreshError, ctx, "Error refreshing app");
        ActionContext.clearCurrent();
        return;
    }
    NetworkJob job = new NetworkJob() {

        @Override
        public void run() {
            Timer timer = Metric.NULL_METRIC.startTimer("null");
            if (metric != Metric.NULL_METRIC) {
                String key = S.concat(MetricInfo.HTTP_HANDLER, ":", requestHandler.toString());
                timer = metric.startTimer(key);
            }
            ctx.saveLocal();
            EventBus eventBus = app.eventBus();
            try {
                eventBus.emit(new PreHandle(ctx));
                requestHandler.handle(ctx);
            } catch (Result r) {
                if (isError(r)) {
                    ctx.handler(FastRequestHandler.DUMB);
                }
                try {
                    r = RequestHandlerProxy.GLOBAL_AFTER_INTERCEPTOR.apply(r, ctx);
                } catch (Exception e) {
                    logger.error(e, "Error calling global after interceptor");
                    r = ActErrorResult.of(e);
                }
                if (null == ctx.handler() || isError(r)) {
                    ctx.handler(FastRequestHandler.DUMB);
                }
                H.Format fmt = req.accept();
                if (H.Format.UNKNOWN == fmt) {
                    fmt = req.contentType();
                }
                ctx.prepareRespForWrite().addHeaderIfNotAdded(H.Header.Names.CONTENT_TYPE, fmt.contentType());
                r.apply(req, ctx.prepareRespForWrite());
            } catch (Exception e) {
                handleException(e, ctx, "Error handling network request");
            } finally {
                // we don't destroy ctx here in case it's been passed to
                // another thread
                eventBus.emit(new PostHandle(ctx));
                ActionContext.clearCurrent();
                timer.stop();
            }
        }
    };
    if (method.unsafe() || !requestHandler.express(ctx)) {
        dispatcher.dispatch(job);
    } else {
        job.run();
    }
}
Also used : H(org.osgl.http.H) PostHandle(act.handler.event.PostHandle) EventBus(act.event.EventBus) ResourceGetter(act.handler.builtin.ResourceGetter) NotAppliedException(org.osgl.exception.NotAppliedException) PreHandle(act.handler.event.PreHandle) FileGetter(act.handler.builtin.FileGetter) Result(org.osgl.mvc.result.Result) ErrorResult(org.osgl.mvc.result.ErrorResult) ActErrorResult(act.view.ActErrorResult) Timer(act.metric.Timer) FastRequestHandler(act.handler.builtin.controller.FastRequestHandler) RequestHandler(act.handler.RequestHandler) AlwaysNotFound(act.handler.builtin.AlwaysNotFound) NotFound(org.osgl.mvc.result.NotFound)

Example 3 with EventBus

use of act.event.EventBus 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 4 with EventBus

use of act.event.EventBus in project actframework by actframework.

the class ControllerEnhancerTest method setup.

@Before
public void setup() throws Exception {
    super.setup();
    invokeLog = mock(InvokeLog.class);
    scanner = new ControllerByteCodeScanner();
    scanner.setApp(mockApp);
    eventBus = mock(EventBus.class);
    classLoader = new TestingAppClassLoader(mockApp);
    classInfoRepository = mock(ClassInfoRepository.class);
    $.setProperty(classLoader, classInfoRepository, "classInfoRepository");
    infoSrc = classLoader.controllerClassMetaInfoManager();
    scannerManager = mock(AppCodeScannerManager.class);
    when(mockApp.classLoader()).thenReturn(classLoader);
    when(mockApp.scannerManager()).thenReturn(scannerManager);
    when(mockApp.eventBus()).thenReturn(eventBus);
    when(mockAppConfig.possibleControllerClass(anyString())).thenReturn(true);
    when(mockRouter.isActionMethod(anyString(), anyString())).thenReturn(false);
    C.List<AppByteCodeScanner> scanners = C.list(scanner);
    when(scannerManager.byteCodeScanners()).thenReturn(scanners);
    InvokeLogFactory.set(invokeLog);
    ActionContext.clearCurrent();
    ctx = ActionContext.create(mockApp, mockReq, mockResp);
    ctx.saveLocal();
    base = new File("./target/test-classes");
}
Also used : AppCodeScannerManager(act.app.AppCodeScannerManager) C(org.osgl.util.C) AppByteCodeScanner(act.app.AppByteCodeScanner) TestingAppClassLoader(act.app.TestingAppClassLoader) EventBus(act.event.EventBus) ClassInfoRepository(act.util.ClassInfoRepository) InvokeLog(testapp.util.InvokeLog) Before(org.junit.Before)

Example 5 with EventBus

use of act.event.EventBus in project actframework by actframework.

the class App method emit.

public void emit(SysEventId sysEvent) {
    if (logger.isTraceEnabled()) {
        logger.trace(S.concat("emitting event: ", sysEvent.name()));
    }
    currentState = sysEvent;
    eventEmitted().add(sysEvent);
    EventBus bus = eventBus();
    if (null != bus) {
        bus.emit(sysEvent);
    }
}
Also used : EventBus(act.event.EventBus)

Aggregations

EventBus (act.event.EventBus)5 AppByteCodeScanner (act.app.AppByteCodeScanner)2 AppCodeScannerManager (act.app.AppCodeScannerManager)2 TestingAppClassLoader (act.app.TestingAppClassLoader)2 ClassInfoRepository (act.util.ClassInfoRepository)2 Before (org.junit.Before)2 C (org.osgl.util.C)2 RequestHandler (act.handler.RequestHandler)1 AlwaysNotFound (act.handler.builtin.AlwaysNotFound)1 FileGetter (act.handler.builtin.FileGetter)1 ResourceGetter (act.handler.builtin.ResourceGetter)1 FastRequestHandler (act.handler.builtin.controller.FastRequestHandler)1 PostHandle (act.handler.event.PostHandle)1 PreHandle (act.handler.event.PreHandle)1 ParamValueLoaderManager (act.inject.param.ParamValueLoaderManager)1 JobManager (act.job.JobManager)1 JobByteCodeScanner (act.job.bytecode.JobByteCodeScanner)1 Timer (act.metric.Timer)1 ActErrorResult (act.view.ActErrorResult)1 File (java.io.File)1