Hello,
In order to help finding what's going wrong with my implementation I
simplified as much as I can.
So I obtained a unique class that contain 2 static classes.
My server part is a 198 lines long class that contain 2 static classes.
With this simple testcase I also reproduce the problems that I encounter.
Here is the code of my simple server class. I also put in attachment my
complete project that contain a client part.
http://www.nabble.com/file/p20837112/arp-tries2.zip arp-tries2.zip
------------------------------------------------------------------------------------------------
package com.grizzly.test.arp.server;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import com.sun.grizzly.arp.AsyncExecutor;
import com.sun.grizzly.arp.AsyncFilter;
import com.sun.grizzly.arp.AsyncHandler;
import com.sun.grizzly.arp.AsyncTask;
import com.sun.grizzly.arp.DefaultAsyncHandler;
import com.sun.grizzly.http.DefaultProcessorTask;
import com.sun.grizzly.http.SelectorThread;
import com.sun.grizzly.tcp.ActionCode;
import com.sun.grizzly.tcp.Adapter;
import com.sun.grizzly.tcp.OutputBuffer;
import com.sun.grizzly.tcp.Request;
import com.sun.grizzly.tcp.Response;
import com.sun.grizzly.util.buf.ByteChunk;
/**
* Original code comes from
http://kasparov.skife.org/blog/src/java/grizzly-arp-basic.html
*
* @author Brian McCallister
* @author evrignaud
*/
public class SimpleArpServer
{
public static final String CALLBACK_KEY =
"com.grizzly.test.arp.server.FINISH_REQUEST";
public static void main(String[] args) throws IOException,
InstantiationException
{
System.out.println("Starting arp-tries2 testcase");
SelectorThread sel = new SelectorThread();
sel.setPort(8080);
sel.setAdapter(new ViewAdapter());
final AsyncHandler handler = new DefaultAsyncHandler();
handler.addAsyncFilter(new AsyncFilter()
{
public boolean doFilter(final AsyncExecutor executor)
{
final AsyncTask asyncTask = executor.getAsyncTask();
final AsyncHandler asyncHandler =
executor.getAsyncHandler();
final DefaultProcessorTask processorTask =
(DefaultProcessorTask) executor.getProcessorTask();
Request req = processorTask.getRequest();
if (req.requestURI().toString().equals("/refreshView"))
{
req.setAttribute(CALLBACK_KEY, new Runnable()
{
public void run()
{
asyncHandler.handle(asyncTask);
}
});
processorTask.invokeAdapter();
return false;
}
return true;
}
});
sel.setAsyncHandler(handler);
sel.setEnableAsyncExecution(true);
sel.setDisplayConfiguration(true);
sel.initEndpoint();
sel.startEndpoint();
}
private static final ExecutorService exec =
Executors.newFixedThreadPool(30);
static class ViewAdapter implements Adapter
{
public void service(Request req, Response res) throws Exception
{
if (req.requestURI().toString().equals("/refreshView") == false)
{
res.setStatus(400);
byte[] bytes = ByteChunk.convertToBytes("Bad request !\n");
ByteChunk chunk = new ByteChunk();
res.setContentLength(bytes.length);
res.setContentType("text/plain");
chunk.append(bytes, 0, bytes.length);
res.doWrite(chunk);
return;
}
exec.execute(new Refresher(req));
}
public void afterService(Request req, Response res) throws Exception
{
req.action(ActionCode.ACTION_POST_REQUEST, null);
}
}
static class Refresher implements Runnable
{
private Request req;
private Refresher(Request req)
{
this.req = req;
}
@Override
public void run()
{
try
{
Response res = req.getResponse();
res.setContentType("text/xml");
res.addHeader("Cache-Control", "private");
res.addHeader("Pragma", "no-cache");
res.setCharacterEncoding("UTF-8");
String viewId = req.getParameters().getParameter("viewId");
System.out.println("Refresh for view: " + viewId);
for (int resultNumber = 0; resultNumber < 30;
resultNumber++)
{
Thread.sleep(3000);
String result = "";
result += "<?xml version=\"1.0\"
encoding=\"windows-1252\"?>\n";
result += "<QueryResult>\n";
result += "\t<name>result" + resultNumber + "</name>\n";
result += "\t<viewId>" + viewId + "</viewId>\n";
result += "</QueryResult>\n";
sendData(req, result);
}
terminateResponse(req);
}
catch (Exception ex)
{
System.out.println(ex);
}
}
public void terminateResponse(Request request) throws IOException
{
// Terminate the response.
streamChunk(null, request.getResponse().getOutputBuffer(),
request.getResponse());
request.getResponse().setStatus(200);
// Really terminate the request
Runnable completion = (Runnable)
request.getAttribute(SimpleArpServer.CALLBACK_KEY);
completion.run();
}
public void sendData(Request request, String data) throws
IOException
{
try
{
Response response = request.getResponse();
streamChunk(data.getBytes(), response.getOutputBuffer(),
response);
}
catch (IOException e)
{
throw e;
}
catch (Exception e)
{
throw new IOException(e);
}
}
private static final byte[] CRLF_BYTES = { (byte) 13, (byte) 10 };
private static final byte ZERO_BYTE = (byte) 48;
private void streamChunk(byte[] bytes, OutputBuffer output, Response
response) throws IOException
{
ByteChunk chunk = new ByteChunk();
if ((bytes != null) && (bytes.length > 0))
{
byte[] chunkLength =
Integer.toHexString(bytes.length).getBytes("ASCII");
chunk.append(chunkLength, 0, chunkLength.length);
chunk.append(CRLF_BYTES, 0, CRLF_BYTES.length);
chunk.append(bytes, 0, bytes.length);
chunk.append(CRLF_BYTES, 0, CRLF_BYTES.length);
}
else
{ // Send final 'EOF' chunk for the response.
chunk.append(ZERO_BYTE);
chunk.append(CRLF_BYTES, 0, CRLF_BYTES.length);
}
output.doWrite(chunk, response);
response.flush();
}
}
}
------------------------------------------------------------------------------------------------
Thanks for your help,
Regards,
Etienne VRIGNAUD
--
View this message in context: http://www.nabble.com/Need-help-on-ARP-usage-tp20789925p20837112.html
Sent from the Grizzly - Users mailing list archive at Nabble.com.