Created
September 13, 2012 20:40
-
-
Save jjongsma/3717470 to your computer and use it in GitHub Desktop.
Channel close test case
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.barchart.osgi.nettosphere; | |
import java.io.IOException; | |
import org.atmosphere.cpr.AtmosphereHandler; | |
import org.atmosphere.cpr.AtmosphereResource; | |
import org.atmosphere.cpr.AtmosphereResourceEvent; | |
import org.atmosphere.cpr.AtmosphereResourceEventListenerAdapter; | |
import org.atmosphere.cpr.AtmosphereResponse; | |
import org.atmosphere.nettosphere.Config; | |
import org.atmosphere.nettosphere.Nettosphere; | |
public class ChannelCloseExample { | |
/** | |
* @param args | |
*/ | |
public static void main(final String[] args) { | |
Config.Builder cb = new Config.Builder().host("0.0.0.0").port(8080); | |
cb = cb.resource("/*", new TestHandler()); | |
final Nettosphere server = new Nettosphere.Builder().config(cb.build()) | |
.build(); | |
server.start(); | |
} | |
static class TestHandler implements AtmosphereHandler { | |
@Override | |
public void onRequest(final AtmosphereResource resource) | |
throws IOException { | |
// Works: | |
// resource.suspend(-1, true); | |
// resource.suspend(-1); | |
// resource.suspend(); | |
// Doesn't work: | |
resource.suspend(-1, false); | |
new TestResponder(resource).run(); | |
} | |
@Override | |
public void onStateChange(final AtmosphereResourceEvent event) | |
throws IOException { | |
} | |
@Override | |
public void destroy() { | |
} | |
} | |
static class TestResponder extends AtmosphereResourceEventListenerAdapter { | |
volatile boolean done = false; | |
final Thread job; | |
TestResponder(final AtmosphereResource resource) { | |
resource.addEventListener(this); | |
job = new Thread() { | |
@Override | |
public void run() { | |
try { | |
Thread.sleep(100); | |
done = true; | |
System.out.println("Job done"); | |
resource.resume(); | |
} catch (final InterruptedException e) { | |
System.out.println("Cancelled"); | |
} | |
} | |
}; | |
} | |
public void run() { | |
job.start(); | |
} | |
@Override | |
public void onResume(final AtmosphereResourceEvent event) { | |
System.out.println("onResume"); | |
if (event.isResumedOnTimeout() && !done) { | |
System.out.println("Cancelling task"); | |
job.interrupt(); | |
} | |
final AtmosphereResponse response = event.getResource() | |
.getResponse(); | |
// At some point in this loop, the channel gets closed. It usually | |
// happens after 2 or 3 loops for me, bump the iterations up to 100 | |
// if you can't reproduce it after a few requests | |
for (int i = 1; i < 10; i++) { | |
response.write(String.valueOf(i) + "\n"); | |
} | |
// Log warnings seem to be buffered | |
System.out.flush(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment