terentev
terentev

Reputation: 664

Is netty receive events concurrency ? How about DownStream and Upsream events?

events docs http://docs.jboss.org/netty/3.2/api/org/jboss/netty/channel/ChannelEvent.html

For example: Can get two events at the same time?

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
    Thread.sleep(10000);
}

if messageReceived take long time, can i receive another messageReceived in another thread? Or netty have queue of all events ?

another example

int i=0;

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
    i++;
}

is increment correct?

Another question.

Is upstream events and downstream events working concurrently ?

Upvotes: 2

Views: 2524

Answers (3)

terentev
terentev

Reputation: 664

found in javadoc

Thread safety

handleUpstream will be invoked sequentially by the same thread (i.e. an I/O thread) and therefore a handler does not need to worry about being invoked with a new upstream event before the previous upstream event is finished.

Thread safety

handleDownstream may be invoked by more than one thread simultaneously. If the handler accesses a shared resource or stores stateful information, you might need proper synchronization in the handler implementation.

messageReceived is Upstream event, and will be invoked sequentially

Upvotes: 1

Norman Maurer
Norman Maurer

Reputation: 23567

Thats not complete right..

If you use a new handler instance for every channel you don't need to make them thread-safe. The only exception is when you implement a DownstreamHandler, as downstream events can be fired by any thread.

So as long as you only care about ChannelUpstreamHandler you don't need todo any synchronization (if you use one handler per channel)

Upvotes: 4

Kylar
Kylar

Reputation: 9334

Yes, the netty framework keeps a pool of threads. It will call as many 'messageRecieved' methods concurrently as it has threads. Each method that you execute needs to be thread safe.

Upvotes: 0

Related Questions