New to netty

Frederic Bregier fredbregier at free.fr
Thu Dec 11 13:13:45 EST 2008


Hi Manish,

It seems you want to synchronize the client with the reception of
the answer of the server, right ?
As the reception is asynchronous, you have to implement in another way
than relying on Netty for that. Let me explain what I do for that.

You have to implement your logic both in the client (outside Netty)
and in the SimpleChannelHandler (so using Netty where you handle connection,
send
and receive).
I would suggest you to implement something similar as follow:
- Add to your business logic a blockingQueue or similar.
- When you create your client, create this blockingQueue too (like 
  ConcurrentLinkedQueue).
- The client initialize all necessary stuff for the connection to be made.
- When the client hasked the connection, just wait for the connection to be
done.
- Then still in the client just wait on this blockingQueue to have a
response.
- In the "channelConnected" method of your SimpleChannelHandler of your
client,
  the client part sends its message to the server.
- In the "messageReceived" method of your SimpleChannelHandler of your
client,
  when the client part receives the answer from the server, just put it into
  the blocking queue.
- It will release the wait on the blockingQueue on the client side, such
that
  you have your "business" synchronisation.

A little graphic view if not clear (I feel like I am not so clear ;-)

Client.class
 - BlockingQueue<msg>
 - Create a ClientBootStrap to connect to the server
 - Create your own SimpleChannelHandler giving it your BlockingQueue and the
message
   you want to send to the server
 - Connect to the server using the ClientBootStrap
 - wait on msg = BlockingQueue.take()
             => YouSimpleChannelHandler.class
                 - channelConnected => send the message to the server
                 - messageReceived => add the message receive from the
server into the
                   BlockingQueue (if session is no more useful, close it
now)
             <=
 - end of wait on take()
 - now you can continue (close the session if you want, or whatever you have
to)

Hope this helps,
Frederic

-----
Hardware/Software Architect
-- 
View this message in context: http://n2.nabble.com/New-to-netty-tp1625621p1644452.html
Sent from the Netty User Group mailing list archive at Nabble.com.




More information about the netty-users mailing list