Skip to content

Instantly share code, notes, and snippets.

@jjongsma
Created September 18, 2012 17:33
Show Gist options
  • Save jjongsma/3744500 to your computer and use it in GitHub Desktop.
Save jjongsma/3744500 to your computer and use it in GitHub Desktop.
Duplicate action execution in Atmosphere
package com.barchart.osgi.nettosphere;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;
import org.atmosphere.cpr.AtmosphereHandler;
import org.atmosphere.cpr.AtmosphereResource;
import org.atmosphere.cpr.AtmosphereResourceEvent;
import org.atmosphere.cpr.AtmosphereResponse;
import org.atmosphere.nettosphere.Config;
import org.atmosphere.nettosphere.Nettosphere;
public class DuplicateActionExample {
static ConcurrentMap<AtmosphereResponse, AtomicInteger> requests = new ConcurrentHashMap<AtmosphereResponse, AtomicInteger>();
/**
* @param args
*/
public static void main(final String[] args) {
Config.Builder cb = new Config.Builder().host("0.0.0.0").port(8080);
// This one results in double execution
cb = cb.resource("/suspend", new TestHandler(true));
// This one results in browser spinning waiting for a response
cb = cb.resource("/nosuspend", new TestHandler(false));
final Nettosphere server = new Nettosphere.Builder().config(cb.build())
.build();
server.start();
}
private static class TestHandler implements AtmosphereHandler {
final boolean suspend;
private TestHandler(final boolean suspend_) {
suspend = suspend_;
}
@Override
public void onRequest(final AtmosphereResource resource)
throws IOException {
if (suspend) {
resource.suspend();
}
resource.resume();
resource.getResponse().write("Test");
}
@Override
public void onStateChange(final AtmosphereResourceEvent event)
throws IOException {
}
@Override
public void destroy() {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment