Hello guys,
Here a bit of test with suspend/resume on Grizzly WS
1.9.15
First of all the problem
1) when i try to suspend/resume a
request, i have some problem like "random" error (code is pretty big to
try to track them)
So, i have try to build an example class that will
just do resume/suspend, just as a test.
This is what's happen
--------
1-mag-2009 13.43.44 com.sun.grizzly.http.DefaultProtocolFilter
execute
INFO: ProcessorTask exception
java.lang.NullPointerException
at com.sun.grizzly.http.ProcessorTask.postResponse(ProcessorTask.java:
676)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.
java:656)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.
java:905)
at com.sun.grizzly.http.DefaultProtocolFilter.execute
(DefaultProtocolFilter.java:161)
at com.sun.grizzly.
DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:
136)
at com.sun.grizzly.DefaultProtocolChain.execute
(DefaultProtocolChain.java:103)
at com.sun.grizzly.
DefaultProtocolChain.execute(DefaultProtocolChain.java:89)
at com.sun.
grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
at
com.sun.grizzly.ProtocolChainContextTask.doCall
(ProtocolChainContextTask.java:53)
at com.sun.grizzly.
SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
at com.
sun.grizzly.ContextTask.run(ContextTask.java:69)
at java.util.
concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:
886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run
(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
-------------------------
Simply using firefox and refreshing a couple
of time the page.
And here is the class
-------------------------
Some note about the class
1) start a GWS
2) attach an adapter that will
simply suspend the request
3) spawn a thread, that pool constantly a
blockingqueue, take the result, wait a bit, and flush it out.
4) write
a method that on resume send out an xml
Any idea?
-------------------------
import com.sun.grizzly.http.embed.
GrizzlyWebServer;
import com.sun.grizzly.tcp.CompletionHandler;
import
com.sun.grizzly.tcp.http11.GrizzlyAdapter;
import com.sun.grizzly.tcp.
http11.GrizzlyRequest;
import com.sun.grizzly.tcp.http11.
GrizzlyResponse;
import java.io.IOException;
import java.util.
concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
public class Suspend {
public Suspend() {
GrizzlyWebServer
gws = new GrizzlyWebServer();
gws.addGrizzlyAdapter(new
SuspendTest());
gws.getSelectorThread().setDisplayConfiguration
(true);
try {
gws.start();
} catch
(IOException e) {
e.printStackTrace(); //To change body of
catch statement use File | Settings | File Templates.
}
}
static LinkedBlockingQueue<GrizzlyResponse> queue = new
LinkedBlockingQueue<GrizzlyResponse>();
static {
new
Thread() {
public void run() {
while (true)
{
try {
GrizzlyResponse s =
queue.poll(1, TimeUnit.HOURS);
Thread.sleep
(1000);
s.resume();
} catch
(Exception e) {
}
}
}
}.start();
}
public static void main(String args[]) {
new Suspend();
}
class SuspendTest extends
GrizzlyAdapter {
public void service(final GrizzlyRequest
grizzlyRequest, final GrizzlyResponse grizzlyResponse) {
final String SID = "blabla";
if (!grizzlyResponse.
isSuspended()) {
grizzlyResponse.suspend(100000,
this,new CompletionHandler<SuspendTest>() {
public void resumed(SuspendTest attachment) {
try {
attachment.resume(grizzlyResponse,SID);
attachment.afterService(grizzlyRequest,
grizzlyResponse);
} catch (Exception e) {
}
}
public void cancelled(SuspendTest attachment) {
throw new Error("Request cancelled?!?");
}
});
queue.add(grizzlyResponse);
return;
}
System.out.println("BABABA");
}
private
void resume(GrizzlyResponse grizzlyResponse,final String SID) {
System.out.println("RESUMING");
grizzlyResponse.
setHeader("Set-Cookie", "SID="+SID);
grizzlyResponse.
setCharacterEncoding("UTF-8");
grizzlyResponse.setStatus
(200);
grizzlyResponse.setContentType("text/xml");
try {
grizzlyResponse.getWriter().print
("<bla>hi</bla>");
} catch (IOException e) {
e.printStackTrace(); //To change body of catch
statement use File | Settings | File Templates.
}
}
}
}
Arriva Tiscali Mobile! Acquista la tua SIM Tiscali a soli €5 e scopri la semplicità e la convenienza del nuovo servizio per il tuo cellulare. Passa a Tiscali Mobile
http://abbonati.tiscali.it/promo/tiscalimobile/