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");
}
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());
}
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());
}
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();
}
});
}
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");
}
Aggregations