How to handle request / response
Michael.Newcomb at gdc4s.com
Michael.Newcomb at gdc4s.com
Fri Jul 8 15:41:59 EDT 2011
This is an example of what I do for request/response. I edited it in
e-mail, so excuse any typos.
public interface Request<R>
extends ChannelFutureListener
{
long getId();
void setId(long id);
R getResponse()
throws InterruptedException, Exception;
void setResponse(Object response);
}
public interface Response
{
long getRequestId();
}
public abstract class RequestResponseChannelHandler
implements ChannelUpstreamHandler, ChannelDownstreamHandler
{
private final AtomicLong requestCount = new AtomicLong();
private final ConcurrentMap<Long, Request> requests = new
ConcurrentHashMap<Long, Request>();
public void handleUpstream(ChannelHandlerContext context, ChannelEvent
event)
throws Exception
{
if (event instanceof MessageEvent)
{
MessageEvent message = (MessageEvent) event;
if (message.getMessage() instanceof Response)
{
Request request = requests.remove(((Response)
message.getMessage()).getRequestId());
assert request != null;
request.setResponse(message);
}
else
{
context.sendUpstream(event);
}
}
else
{
context.sendUpstream(event);
}
}
public void handleDownstream(ChannelHandlerContext context,
ChannelEvent event)
throws Exception
{
if (event instanceof MessageEvent)
{
MessageEvent message = (MessageEvent) event;
if (message.getMessage() instanceof Request)
{
Request request = (Request) message.getMessage();
long id = requestCount.incrementAndGet();
request.setId(id);
requests.put(id, request);
}
}
context.sendDownstream(event);
}
}
Send request:
Request<String> r = new MyRequest();
channel.write(r);
String s = r.getResponse();
Handle response:
channel.write(new MyResponse(request.getId()));
You still need handlers do encode/decode the requests and responses, but
that is how I handle request/response.
Michael
-----Original Message-----
From: netty-users-bounces at lists.jboss.org
[mailto:netty-users-bounces at lists.jboss.org] On Behalf Of Jonathan
Arnold
Sent: Friday, July 08, 2011 9:16 AM
To: netty-users at lists.jboss.org
Subject: How to handle request / response
I'm a fairly inexperience Java programmer new to Netty and was
wondering what the "standard practice" is when it comes to handling
specific responses to specific requests.
In my case, I am doing RTSP requests to a server and I need to do
specific things based upon the actual request. So when I do a SETUP, I
need to get some information out of the response and then do other
things.
In a more general case, say I'm writing an app the uses a RESTful
interface. I request some information that comes back in, say, XML and
I want to process it in a special way.
How do I set up a handler for a response to a specific request? Is this
what the "event model" mentioned in the docs is used for? If so, I need
a little more information.
Or do I add another filter handler? Have some kind of callback? Use a
special response handler for each request?
Some real world source code examples would be great. The http examples
in the Netty code don't deal with this case. They are either one shots
or don't really do anything with the responses.
TIA.
--
Jonathan Arnold Webstream: http://hieronymus.soup.io
Talent wins games, but team work and intelligence wins championships.
Michael Jordan
_______________________________________________
netty-users mailing list
netty-users at lists.jboss.org
https://lists.jboss.org/mailman/listinfo/netty-users
More information about the netty-users
mailing list