Search in sources :

Example 1 with AtmosphereHandlerAdapter

use of org.atmosphere.handler.AtmosphereHandlerAdapter in project atmosphere by Atmosphere.

the class AtmosphereResourceStateRecoveryTest method longPollingAggregatedTest.

@Test(enabled = false)
public void longPollingAggregatedTest() throws ServletException, IOException, ExecutionException, InterruptedException {
    final AtomicReference<Object> ref = new AtomicReference<Object>();
    AtmosphereResourceImpl r = (AtmosphereResourceImpl) config.resourcesFactory().create(config, "1234567");
    r.setBroadcaster(config.getBroadcasterFactory().lookup("/1", true));
    recovery.configure(config);
    recovery.inspect(r);
    config.getBroadcasterFactory().lookup("/1", true).getBroadcasterConfig().setBroadcasterCache(new UUIDBroadcasterCache());
    config.getBroadcasterFactory().lookup("/2", true).getBroadcasterConfig().setBroadcasterCache(new UUIDBroadcasterCache());
    config.getBroadcasterFactory().lookup("/3", true).getBroadcasterConfig().setBroadcasterCache(new UUIDBroadcasterCache());
    config.getBroadcasterFactory().lookup("/4", true).getBroadcasterConfig().setBroadcasterCache(new UUIDBroadcasterCache());
    config.getBroadcasterFactory().lookup("/1", true).addAtmosphereResource(r);
    config.getBroadcasterFactory().lookup("/2", true).addAtmosphereResource(r);
    config.getBroadcasterFactory().lookup("/3", true).addAtmosphereResource(r);
    config.getBroadcasterFactory().lookup("/4", true).addAtmosphereResource(r);
    r.suspend();
    config.metaBroadcaster().broadcastTo("/1", "Initialize Cache").get();
    r.close();
    AtmosphereResourceImpl r2 = (AtmosphereResourceImpl) config.resourcesFactory().create(config, "1234567");
    // Set a different one to hit caching.
    r2.setBroadcaster(config.getBroadcasterFactory().lookup("/*", true));
    config.getBroadcasterFactory().lookup("/1", true).broadcast(("1")).get();
    config.getBroadcasterFactory().lookup("/2", true).broadcast(("2")).get();
    config.getBroadcasterFactory().lookup("/3", true).broadcast(("3")).get();
    config.getBroadcasterFactory().lookup("/4", true).broadcast(("4")).get();
    r2.transport(AtmosphereResource.TRANSPORT.LONG_POLLING).atmosphereHandler(new AtmosphereHandlerAdapter() {

        @Override
        public void onStateChange(AtmosphereResourceEvent event) throws IOException {
            ref.set(event.getMessage());
        }
    }).suspend();
    recovery.inspect(r2);
    assertTrue(List.class.isAssignableFrom(ref.get().getClass()));
    assertEquals(List.class.cast(ref.get()).size(), 4);
    StringBuilder b = new StringBuilder();
    for (Object o : List.class.cast(ref.get())) {
        b.append(o.toString());
    }
    assertEquals(b.toString(), "1234");
}
Also used : AtmosphereHandlerAdapter(org.atmosphere.handler.AtmosphereHandlerAdapter) AtomicReference(java.util.concurrent.atomic.AtomicReference) List(java.util.List) UUIDBroadcasterCache(org.atmosphere.cache.UUIDBroadcasterCache) Test(org.testng.annotations.Test)

Example 2 with AtmosphereHandlerAdapter

use of org.atmosphere.handler.AtmosphereHandlerAdapter in project atmosphere by Atmosphere.

the class AtmosphereSessionTest method testTrackAndTryAcquire.

@Test
public void testTrackAndTryAcquire() throws IOException, ServletException, InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<AtmosphereSession> session = new AtomicReference<AtmosphereSession>();
    framework.addAtmosphereHandler("/acquire", new AtmosphereHandlerAdapter() {

        @Override
        public void onRequest(AtmosphereResource resource) throws IOException {
            if (session.get() == null) {
                session.set(new AtmosphereSession(resource));
            }
            resource.suspend(2, TimeUnit.SECONDS);
        }

        @Override
        public void onStateChange(AtmosphereResourceEvent event) throws IOException {
            latch.countDown();
        }
    });
    final String qs = "&X-Atmosphere-tracking-id=c8834462-c46e-4dad-a22f-b86aabe3f883&X-Atmosphere-Framework=2.0.4-javascript&X-Atmosphere-Transport=sse&X-Atmosphere-TrackMessageSize=true&X-atmo-protocol=true&_=1380799455333";
    AtmosphereRequest request = new AtmosphereRequestImpl.Builder().queryString(qs).pathInfo("/acquire").build();
    framework.doCometSupport(request, AtmosphereResponseImpl.newInstance());
    latch.await(10, TimeUnit.SECONDS);
    assertNull(session.get().acquire());
    final AtomicReference<AtmosphereResource> rrr = new AtomicReference<AtmosphereResource>();
    final CountDownLatch _latch = new CountDownLatch(1);
    framework.addAtmosphereHandler("/acquire", new AtmosphereHandlerAdapter() {

        @Override
        public void onRequest(final AtmosphereResource resource) throws IOException {
            resource.suspend(2, TimeUnit.SECONDS);
        }

        @Override
        public void onStateChange(AtmosphereResourceEvent event) throws IOException {
            try {
                rrr.set(session.get().tryAcquire());
                _latch.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    });
    request = new AtmosphereRequestImpl.Builder().queryString(qs).pathInfo("/acquire").build();
    framework.doCometSupport(request, AtmosphereResponseImpl.newInstance(request));
    _latch.await(10, TimeUnit.SECONDS);
    assertNotNull(rrr.get());
    new Thread() {

        public void run() {
            try {
                Thread.sleep(1000);
                AtmosphereRequest request = new AtmosphereRequestImpl.Builder().queryString(qs).pathInfo("/acquire").build();
                framework.doCometSupport(request, AtmosphereResponseImpl.newInstance(request));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }.start();
    assertNotNull(session.get().tryAcquire());
}
Also used : AtmosphereHandlerAdapter(org.atmosphere.handler.AtmosphereHandlerAdapter) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) Test(org.testng.annotations.Test)

Example 3 with AtmosphereHandlerAdapter

use of org.atmosphere.handler.AtmosphereHandlerAdapter in project atmosphere by Atmosphere.

the class AtmosphereSessionTest method testTrackAndTryAcquire.

@Test
public void testTrackAndTryAcquire() throws IOException, ServletException, InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<AtmosphereSession> session = new AtomicReference<AtmosphereSession>();
    framework.addAtmosphereHandler("/acquire", new AtmosphereHandlerAdapter() {

        @Override
        public void onRequest(AtmosphereResource resource) throws IOException {
            if (session.get() == null) {
                session.set(new AtmosphereSession(resource));
            }
            resource.suspend(2, TimeUnit.SECONDS);
        }

        @Override
        public void onStateChange(AtmosphereResourceEvent event) throws IOException {
            latch.countDown();
        }
    });
    final String qs = "&X-Atmosphere-tracking-id=c8834462-c46e-4dad-a22f-b86aabe3f883&X-Atmosphere-Framework=2.0.4-javascript&X-Atmosphere-Transport=sse&X-Atmosphere-TrackMessageSize=true&X-atmo-protocol=true&_=1380799455333";
    AtmosphereRequest request = new AtmosphereRequestImpl.Builder().queryString(qs).pathInfo("/acquire").build();
    framework.doCometSupport(request, AtmosphereResponseImpl.newInstance());
    latch.await(10, TimeUnit.SECONDS);
    assertNull(session.get().acquire());
    final AtomicReference<AtmosphereResource> rrr = new AtomicReference<AtmosphereResource>();
    final CountDownLatch _latch = new CountDownLatch(1);
    framework.addAtmosphereHandler("/acquire", new AtmosphereHandlerAdapter() {

        @Override
        public void onRequest(final AtmosphereResource resource) throws IOException {
            resource.suspend(2, TimeUnit.SECONDS);
        }

        @Override
        public void onStateChange(AtmosphereResourceEvent event) throws IOException {
            try {
                rrr.set(session.get().tryAcquire());
                _latch.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    });
    request = new AtmosphereRequestImpl.Builder().queryString(qs).pathInfo("/acquire").build();
    framework.doCometSupport(request, AtmosphereResponseImpl.newInstance(request));
    _latch.await(10, TimeUnit.SECONDS);
    assertNotNull(rrr.get());
    new Thread() {

        public void run() {
            try {
                Thread.sleep(1000);
                AtmosphereRequest request = new AtmosphereRequestImpl.Builder().queryString(qs).pathInfo("/acquire").build();
                framework.doCometSupport(request, AtmosphereResponseImpl.newInstance(request));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }.start();
    assertNotNull(session.get().tryAcquire());
}
Also used : AtmosphereHandlerAdapter(org.atmosphere.handler.AtmosphereHandlerAdapter) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) ServletException(jakarta.servlet.ServletException) Test(org.testng.annotations.Test)

Example 4 with AtmosphereHandlerAdapter

use of org.atmosphere.handler.AtmosphereHandlerAdapter in project atmosphere by Atmosphere.

the class AnnotationScanningTest method create.

@BeforeMethod
public void create() throws Throwable {
    framework = new AtmosphereFramework();
    framework.setDefaultBroadcasterClassName(SimpleBroadcaster.class.getName());
    framework.addAnnotationPackage(AnnotationScanningTest.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().addAtmosphereHandler("/a", new AtmosphereHandlerAdapter() {

        @Override
        public void onRequest(AtmosphereResource resource) throws IOException {
            resource.suspend();
        }
    });
}
Also used : SimpleBroadcaster(org.atmosphere.util.SimpleBroadcaster) ServletException(jakarta.servlet.ServletException) AtmosphereResponse(org.atmosphere.cpr.AtmosphereResponse) Action(org.atmosphere.cpr.Action) AtmosphereHandlerAdapter(org.atmosphere.handler.AtmosphereHandlerAdapter) AtmosphereRequest(org.atmosphere.cpr.AtmosphereRequest) AtmosphereResource(org.atmosphere.cpr.AtmosphereResource) AsynchronousProcessor(org.atmosphere.cpr.AsynchronousProcessor) AtmosphereFramework(org.atmosphere.cpr.AtmosphereFramework) IOException(java.io.IOException) AtmosphereResourceImpl(org.atmosphere.cpr.AtmosphereResourceImpl) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 5 with AtmosphereHandlerAdapter

use of org.atmosphere.handler.AtmosphereHandlerAdapter in project atmosphere by Atmosphere.

the class AtmosphereResourceStateRecoveryTest method longPollingAggregatedTest.

@Test(enabled = false)
public void longPollingAggregatedTest() throws ServletException, IOException, ExecutionException, InterruptedException {
    final AtomicReference<Object> ref = new AtomicReference<Object>();
    AtmosphereResourceImpl r = (AtmosphereResourceImpl) config.resourcesFactory().create(config, "1234567");
    r.setBroadcaster(config.getBroadcasterFactory().lookup("/1", true));
    recovery.configure(config);
    recovery.inspect(r);
    config.getBroadcasterFactory().lookup("/1", true).getBroadcasterConfig().setBroadcasterCache(new UUIDBroadcasterCache());
    config.getBroadcasterFactory().lookup("/2", true).getBroadcasterConfig().setBroadcasterCache(new UUIDBroadcasterCache());
    config.getBroadcasterFactory().lookup("/3", true).getBroadcasterConfig().setBroadcasterCache(new UUIDBroadcasterCache());
    config.getBroadcasterFactory().lookup("/4", true).getBroadcasterConfig().setBroadcasterCache(new UUIDBroadcasterCache());
    config.getBroadcasterFactory().lookup("/1", true).addAtmosphereResource(r);
    config.getBroadcasterFactory().lookup("/2", true).addAtmosphereResource(r);
    config.getBroadcasterFactory().lookup("/3", true).addAtmosphereResource(r);
    config.getBroadcasterFactory().lookup("/4", true).addAtmosphereResource(r);
    r.suspend();
    config.metaBroadcaster().broadcastTo("/1", "Initialize Cache").get();
    r.close();
    AtmosphereResourceImpl r2 = (AtmosphereResourceImpl) config.resourcesFactory().create(config, "1234567");
    // Set a different one to hit caching.
    r2.setBroadcaster(config.getBroadcasterFactory().lookup("/*", true));
    config.getBroadcasterFactory().lookup("/1", true).broadcast(("1")).get();
    config.getBroadcasterFactory().lookup("/2", true).broadcast(("2")).get();
    config.getBroadcasterFactory().lookup("/3", true).broadcast(("3")).get();
    config.getBroadcasterFactory().lookup("/4", true).broadcast(("4")).get();
    r2.transport(AtmosphereResource.TRANSPORT.LONG_POLLING).atmosphereHandler(new AtmosphereHandlerAdapter() {

        @Override
        public void onStateChange(AtmosphereResourceEvent event) throws IOException {
            ref.set(event.getMessage());
        }
    }).suspend();
    recovery.inspect(r2);
    assertTrue(List.class.isAssignableFrom(ref.get().getClass()));
    assertEquals(List.class.cast(ref.get()).size(), 4);
    StringBuilder b = new StringBuilder();
    for (Object o : List.class.cast(ref.get())) {
        b.append(o.toString());
    }
    assertEquals(b.toString(), "1234");
}
Also used : AtmosphereHandlerAdapter(org.atmosphere.handler.AtmosphereHandlerAdapter) AtomicReference(java.util.concurrent.atomic.AtomicReference) List(java.util.List) UUIDBroadcasterCache(org.atmosphere.cache.UUIDBroadcasterCache) Test(org.testng.annotations.Test)

Aggregations

AtmosphereHandlerAdapter (org.atmosphere.handler.AtmosphereHandlerAdapter)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Test (org.testng.annotations.Test)4 IOException (java.io.IOException)3 ServletException (jakarta.servlet.ServletException)2 List (java.util.List)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 UUIDBroadcasterCache (org.atmosphere.cache.UUIDBroadcasterCache)2 ServletException (javax.servlet.ServletException)1 Action (org.atmosphere.cpr.Action)1 AsynchronousProcessor (org.atmosphere.cpr.AsynchronousProcessor)1 AtmosphereFramework (org.atmosphere.cpr.AtmosphereFramework)1 AtmosphereRequest (org.atmosphere.cpr.AtmosphereRequest)1 AtmosphereResource (org.atmosphere.cpr.AtmosphereResource)1 AtmosphereResourceImpl (org.atmosphere.cpr.AtmosphereResourceImpl)1 AtmosphereResponse (org.atmosphere.cpr.AtmosphereResponse)1 SimpleBroadcaster (org.atmosphere.util.SimpleBroadcaster)1 BeforeMethod (org.testng.annotations.BeforeMethod)1