Client Channel and thread safety
Trustin Lee (이희승)
trustin at gmail.com
Mon Oct 12 21:56:52 EDT 2009
On Tue, Oct 13, 2009 at 4:02 AM, Shay Banon <kimchy at gmail.com> wrote:
> (sorry for posting this again, I was not subscribed to the mailing
> list, so I am not sure it got there...)
Your message actually appeared in the mailing list. Sorry about the late reply.
> Hi,
>
> Quick questions, when using client bootstrap, and connecting to a
> specific host, I get a channel back. My questions are:
>
> 1. Can the client bootstrap be used to connect to different host+port?
> (I am pretty sure yes, just making sure).
Yes. You can call connect() with different remote/local addresses as
many times as you want.
> 2. Is the Channel that you get back from the connect operation thread
> safe? i.e., can I do something like this:
>
> public void send() {
> ChannelBuffer cb = ... (build a new channel buffer, and fill it
> with content)
> shardsChannel.write(cb)
> }
>
> When channel is shared between threads, and the send method can be
> called concurrently?
The channel is thread safe. Therefore, you can call Channel.write()
from different threads.
As a side note, you must make sure that the channel has completed its
connection attempt. Otherwise, you will get NotYetConnectedException.
There are many ways to get notified when the connection attempt is
done - please refer to the examples.
HTH
— Trustin Lee, http://gleamynode.net/
More information about the netty-users
mailing list