1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.example.objectecho;
17
18 import org.jboss.netty.channel.ChannelEvent;
19 import org.jboss.netty.channel.ChannelHandlerContext;
20 import org.jboss.netty.channel.ChannelState;
21 import org.jboss.netty.channel.ChannelStateEvent;
22 import org.jboss.netty.channel.ExceptionEvent;
23 import org.jboss.netty.channel.MessageEvent;
24 import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
25
26 import java.util.concurrent.atomic.AtomicLong;
27
28
29
30
31
32 public class ObjectEchoServerHandler extends SimpleChannelUpstreamHandler {
33
34 private final AtomicLong transferredMessages = new AtomicLong();
35
36 public long getTransferredMessages() {
37 return transferredMessages.get();
38 }
39
40 @Override
41 public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
42 if (e instanceof ChannelStateEvent && ((ChannelStateEvent) e).getState() != ChannelState.INTEREST_OPS) {
43 System.err.println(e);
44 }
45 super.handleUpstream(ctx, e);
46 }
47
48 @Override
49 public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
50
51 transferredMessages.incrementAndGet();
52 e.getChannel().write(e.getMessage());
53 }
54
55 @Override
56 public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
57 e.getCause().printStackTrace();
58 e.getChannel().close();
59 }
60 }