Search in sources :

Example 6 with AsynchronousProcessor

use of org.atmosphere.cpr.AsynchronousProcessor in project atmosphere by Atmosphere.

the class DefaultWebSocketProcessor method configure.

public WebSocketProcessor configure(AtmosphereConfig config) {
    this.framework = config.framework();
    this.webSocketProtocol = framework.getWebSocketProtocol();
    destroyable = Boolean.parseBoolean(framework.getAtmosphereConfig().getInitParameter(RECYCLE_ATMOSPHERE_REQUEST_RESPONSE));
    executeAsync = Boolean.parseBoolean(framework.getAtmosphereConfig().getInitParameter(WEBSOCKET_PROTOCOL_EXECUTION));
    allow1005StatusCode = Boolean.parseBoolean(framework.getAtmosphereConfig().getInitParameter(ALLOW_WEBSOCKET_STATUS_CODE_1005_AS_DISCONNECT));
    String s = framework.getAtmosphereConfig().getInitParameter(IN_MEMORY_STREAMING_BUFFER_SIZE);
    if (s != null) {
        byteBufferMaxSize = Integer.parseInt(s);
        charBufferMaxSize = byteBufferMaxSize;
    }
    if (executeAsync) {
        asyncExecutor = ExecutorsFactory.getAsyncOperationExecutor(config, "WebSocket");
    } else {
        asyncExecutor = VoidExecutorService.VOID;
    }
    scheduler = ExecutorsFactory.getScheduler(config);
    optimizeMapping();
    closingTime = Long.valueOf(config.getInitParameter(ApplicationConfig.CLOSED_ATMOSPHERE_THINK_TIME, "0"));
    invokeInterceptors = Boolean.valueOf(config.getInitParameter(INVOKE_ATMOSPHERE_INTERCEPTOR_ON_WEBSOCKET_MESSAGE, "true"));
    config.startupHook(framework -> {
        if (AsynchronousProcessor.class.isAssignableFrom(framework.getAsyncSupport().getClass())) {
            asynchronousProcessor = AsynchronousProcessor.class.cast(framework.getAsyncSupport());
        } else {
            asynchronousProcessor = new AsynchronousProcessor(framework.getAtmosphereConfig()) {

                @Override
                public Action service(AtmosphereRequest req, AtmosphereResponse res) throws IOException, ServletException {
                    return framework.getAsyncSupport().service(req, res);
                }
            };
        }
    });
    return this;
}
Also used : ServletException(jakarta.servlet.ServletException) AtmosphereResponse(org.atmosphere.cpr.AtmosphereResponse) Action(org.atmosphere.cpr.Action) AtmosphereRequest(org.atmosphere.cpr.AtmosphereRequest) AsynchronousProcessor(org.atmosphere.cpr.AsynchronousProcessor) IOException(java.io.IOException)

Example 7 with AsynchronousProcessor

use of org.atmosphere.cpr.AsynchronousProcessor in project atmosphere by Atmosphere.

the class EncoderDecoderTest method create.

@BeforeMethod
public void create() throws Throwable {
    framework = new AtmosphereFramework();
    framework.setDefaultBroadcasterClassName(SimpleBroadcaster.class.getName());
    framework.addAnnotationPackage(ManagedMessage.class);
    framework.setAsyncSupport(new AsynchronousProcessor(framework.getAtmosphereConfig()) {

        @Override
        public Action service(AtmosphereRequest req, AtmosphereResponse res) throws IOException, ServletException {
            return suspended(req, res);
        }

        public void action(AtmosphereResourceImpl r) {
            try {
                resumed(r.getRequest(), r.getResponse());
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ServletException e) {
                e.printStackTrace();
            }
        }
    }).init(new ServletConfig() {

        @Override
        public String getServletName() {
            return "void";
        }

        @Override
        public ServletContext getServletContext() {
            return mock(ServletContext.class);
        }

        @Override
        public String getInitParameter(String name) {
            return null;
        }

        @Override
        public Enumeration<String> getInitParameterNames() {
            return null;
        }
    });
    latch.set(new CountDownLatch(1));
}
Also used : AtmosphereResponse(org.atmosphere.cpr.AtmosphereResponse) Enumeration(java.util.Enumeration) ServletConfig(jakarta.servlet.ServletConfig) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) SimpleBroadcaster(org.atmosphere.util.SimpleBroadcaster) ServletException(jakarta.servlet.ServletException) AtmosphereRequest(org.atmosphere.cpr.AtmosphereRequest) AsynchronousProcessor(org.atmosphere.cpr.AsynchronousProcessor) AtmosphereFramework(org.atmosphere.cpr.AtmosphereFramework) ServletContext(jakarta.servlet.ServletContext) AtmosphereResourceImpl(org.atmosphere.cpr.AtmosphereResourceImpl) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 8 with AsynchronousProcessor

use of org.atmosphere.cpr.AsynchronousProcessor in project atmosphere by Atmosphere.

the class OnDisconnectInterceptor method inspect.

@Override
public Action inspect(final AtmosphereResource r) {
    if (Utils.webSocketMessage(r))
        return Action.CONTINUE;
    AtmosphereRequest request = ((AtmosphereResourceImpl) r).getRequest(false);
    String uuid = r.uuid();
    if (closeMessage(request)) {
        if (config.resourcesFactory() == null || uuid == null) {
            logger.debug("Illegal state for uuid {} and AtmosphereResourceFactory {}", r.uuid(), config.resourcesFactory());
            return Action.CANCELLED;
        }
        AtmosphereResource ss = config.resourcesFactory().find(uuid);
        if (ss == null) {
            logger.debug("No Suspended Connection found for {}. Using the AtmosphereResource associated with the close message", uuid);
            ss = r;
        }
        logger.debug("AtmosphereResource {} disconnected", uuid);
        // Block websocket closing detection
        ((AtmosphereResourceEventImpl) ss.getAtmosphereResourceEvent()).isClosedByClient(true);
        if (AsynchronousProcessor.class.isAssignableFrom(config.framework().getAsyncSupport().getClass())) {
            ((AsynchronousProcessor) config.framework().getAsyncSupport()).completeLifecycle(ss, false);
        }
        return Action.CANCELLED;
    }
    return Action.CONTINUE;
}
Also used : AtmosphereRequest(org.atmosphere.cpr.AtmosphereRequest) AtmosphereResource(org.atmosphere.cpr.AtmosphereResource) AsynchronousProcessor(org.atmosphere.cpr.AsynchronousProcessor) AtmosphereResourceImpl(org.atmosphere.cpr.AtmosphereResourceImpl) AtmosphereResourceEventImpl(org.atmosphere.cpr.AtmosphereResourceEventImpl)

Aggregations

AsynchronousProcessor (org.atmosphere.cpr.AsynchronousProcessor)8 AtmosphereRequest (org.atmosphere.cpr.AtmosphereRequest)8 ServletException (jakarta.servlet.ServletException)7 IOException (java.io.IOException)7 AtmosphereResourceImpl (org.atmosphere.cpr.AtmosphereResourceImpl)7 AtmosphereResponse (org.atmosphere.cpr.AtmosphereResponse)7 AtmosphereFramework (org.atmosphere.cpr.AtmosphereFramework)6 BeforeMethod (org.testng.annotations.BeforeMethod)6 SimpleBroadcaster (org.atmosphere.util.SimpleBroadcaster)5 ServletConfig (jakarta.servlet.ServletConfig)4 ServletContext (jakarta.servlet.ServletContext)4 Enumeration (java.util.Enumeration)4 Action (org.atmosphere.cpr.Action)2 AtmosphereResource (org.atmosphere.cpr.AtmosphereResource)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtmosphereResourceEventImpl (org.atmosphere.cpr.AtmosphereResourceEventImpl)1 AtmosphereHandlerAdapter (org.atmosphere.handler.AtmosphereHandlerAdapter)1