Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Netty - Network - Application development the easy way

Netty - Network - Application development the easy way

This presentation was hold as part of JAX 2013 in Mainz and cover Netty 4.

Norman Maurer

April 24, 2013
Tweet

More Decks by Norman Maurer

Other Decks in Programming

Transcript

  1. Network - Application development the easy way
    @netty_project
    http://netty.io
    https://github.com/netty/netty

    View Slide

  2. NORMAN MAURER
    Red Hat (JBoss) - EAP Core Team
    Former contractor for Apple Inc
    Author - Netty in Action
    Apache Software Foundation Member
    Netty / Vert.x Core-Developer and all things NIO
    Java and Scala
    Twitter: @normanmaurer
    Github: https://github.com/normanmaurer

    View Slide

  3. WHAT IS NETTY ?
    “Netty is a NIO client server framework which
    enable quick and easy development of
    network applications such as protocol servers
    and clients...”

    View Slide

  4. YET ANOTHER NETWORK FRAMEWORK ?
    YES!

    View Slide

  5. “ @!# WTF, WHY SHOULD I CARE
    ? ”

    View Slide

  6. FEATURES

    View Slide

  7. FEATURES
    Asynchronous
    Simple and unified API
    High-Performing
    Listeners and Callbacks supported
    Zero-file-copy
    Buffer Pooling
    Gathering / Scattering
    ... and many more

    View Slide

  8. ASYNCHRONOUS, WHAT DOES THIS MEAN ?

    View Slide

  9. ASYNCHRONOUS...
    All I/O Operations don't block!
    Get notified once the operation completes
    Be able to share one Thread across many connections

    View Slide

  10. “But synchronous / blocking IO served me
    well! Again, why should I care ?”

    View Slide

  11. BLOCKING-IO MAY NOT SCALE!
    GOOD LUCK WITH SERVING 1 MILLION CONCURRENT
    CONNECTIONS
    “ Usually a Thread takes memory from
    256kb to 1mb for the stack space! ”

    View Slide

  12. NETTY TO THE RESCUE
    “Ouch....”

    View Slide

  13. ASYNCHRONOUS IN ACTION
    C
    h
    a
    n
    n
    e
    l c
    h
    a
    n
    n
    e
    l = .
    .
    .
    C
    h
    a
    n
    n
    e
    l
    F
    u
    t
    u
    r
    e c
    f = c
    h
    a
    n
    n
    e
    l
    .
    w
    r
    i
    t
    e
    (
    d
    a
    t
    a
    )
    ;
    c
    f
    .
    a
    d
    d
    L
    i
    s
    t
    e
    n
    e
    r
    (
    n
    e
    w C
    h
    a
    n
    n
    e
    l
    F
    u
    t
    u
    r
    e
    L
    i
    s
    t
    e
    n
    e
    r
    (
    ) {
    @
    O
    v
    e
    r
    r
    i
    d
    e
    p
    u
    b
    l
    i
    c v
    o
    i
    d o
    p
    e
    r
    a
    t
    i
    o
    n
    C
    o
    m
    p
    l
    e
    t
    e
    (
    C
    h
    a
    n
    n
    e
    l
    F
    u
    t
    u
    r
    e f
    u
    t
    u
    r
    e
    )
    t
    h
    r
    o
    w
    s E
    x
    c
    e
    p
    t
    i
    o
    n {
    i
    f
    (
    !
    f
    u
    t
    u
    r
    e
    .
    i
    s
    S
    u
    c
    c
    e
    s
    s
    (
    ) {
    f
    u
    t
    u
    r
    e
    .
    c
    a
    u
    s
    e
    (
    )
    .
    p
    r
    i
    n
    t
    S
    t
    a
    c
    k
    t
    r
    a
    c
    e
    (
    )
    ;
    .
    .
    .
    }
    .
    .
    .
    }
    }
    )
    ;
    .
    .
    .
    c
    f
    .
    s
    y
    n
    c
    (
    )
    ;

    View Slide

  14. TRANSPORTS
    NIO
    OIO
    AIO
    Local
    Embedded
    “And more to come ....”

    View Slide

  15. SUPPORTED PROTOCOLS
    TCP
    UDP
    SCTP*
    UDT
    Serial
    * O
    n
    l
    y s
    u
    p
    p
    o
    r
    t
    e
    d o
    n l
    i
    n
    u
    x

    View Slide

  16. SIMPLE STATE MODEL

    View Slide

  17. STATE FLOW

    View Slide

  18. DATA CONTAINERS
    ByteBuf
    MessageBuf
    “ByteBuffer on steroids!”
    “Queue like thing which can hold Message of
    type T”

    View Slide

  19. BYTEBUF
    Separate index for reader/writer
    CompositeByteBuf
    Direct and Heap implementations
    Resizable with max capacity
    Support for reference-counting
    Method-Chaining
    B
    y
    t
    e
    B
    u
    f b
    u
    f = .
    .
    .
    ;
    b
    u
    f
    .
    w
    r
    i
    t
    e
    I
    n
    t
    (
    1
    )
    .
    w
    r
    i
    t
    e
    B
    y
    t
    e
    s
    (
    d
    a
    t
    a
    )
    .
    w
    r
    i
    t
    e
    B
    o
    o
    l
    e
    a
    n
    (
    t
    r
    u
    e
    )
    .
    .
    .

    View Slide

  20. MESSAGEBUF
    Extend Queue
    Support reference-counting
    Type-safe!

    View Slide

  21. CHANNELHANDLER
    “Time to process some data, heh ?”

    View Slide

  22. CHANNELHANDLER
    “Your business logic needs a place, and this is
    where it belongs too”

    View Slide

  23. CHANNELHANDLER FACTS
    Inbound vs. Outbound
    State vs. Operation
    Byte or Message based
    Always called by the assigned EventExecutor
    Type-safe

    View Slide

  24. EXAMPLE USE CASES
    Convert data a.k.a Encoder / Decoder
    Business-Logic
    Handle state changes

    View Slide

  25. PROVIDED CODECS
    “We have a lot of them!”

    View Slide

  26. PROVIDED CODECS
    HTTP
    Websockets
    SPDY
    Serialization
    JBoss Marshalling
    Protobufs
    SSL
    Zlib

    View Slide

  27. ADAPTERS
    ChannelInboundMessageHandlerAdapter
    ChannelInboundByteHandlerAdapter
    ChannelOutboundMessageHandlerAdapter
    ChannelOutboundByteHandlerAdapter
    “Makes your life easier”

    View Slide

  28. ADAPTER IN ACTION
    @
    S
    h
    a
    r
    a
    b
    l
    e
    p
    u
    b
    l
    i
    c c
    l
    a
    s
    s E
    c
    h
    o
    4
    S
    e
    r
    v
    e
    r
    H
    a
    n
    d
    l
    e
    r e
    x
    t
    e
    n
    d
    s
    C
    h
    a
    n
    n
    e
    l
    I
    n
    b
    o
    u
    n
    d
    B
    y
    t
    e
    H
    a
    n
    d
    l
    e
    r
    A
    d
    a
    p
    t
    e
    r {
    @
    O
    v
    e
    r
    r
    i
    d
    e
    p
    u
    b
    l
    i
    c v
    o
    i
    d i
    n
    b
    o
    u
    n
    d
    B
    u
    f
    f
    e
    r
    U
    p
    d
    a
    t
    e
    d
    (
    C
    h
    a
    n
    n
    e
    l
    H
    a
    n
    d
    l
    e
    r
    C
    o
    n
    t
    e
    x
    t c
    t
    x
    ,
    B
    y
    t
    e
    B
    u
    f i
    n
    ) {
    B
    y
    t
    e
    B
    u
    f o
    u
    t = c
    t
    x
    .
    n
    e
    x
    t
    O
    u
    t
    b
    o
    u
    n
    d
    B
    y
    t
    e
    B
    u
    f
    f
    e
    r
    (
    )
    ;
    o
    u
    t
    .
    d
    i
    s
    c
    a
    r
    d
    R
    e
    a
    d
    B
    y
    t
    e
    s
    (
    )
    .
    w
    r
    i
    t
    e
    B
    y
    t
    e
    s
    (
    i
    n
    )
    ;
    c
    t
    x
    .
    f
    l
    u
    s
    h
    (
    )
    ;
    }
    @
    O
    v
    e
    r
    r
    i
    d
    e
    p
    u
    b
    l
    i
    c v
    o
    i
    d e
    x
    c
    e
    p
    t
    i
    o
    n
    C
    a
    u
    g
    h
    t
    (
    C
    h
    a
    n
    n
    e
    l
    H
    a
    n
    d
    l
    e
    r
    C
    o
    n
    t
    e
    x
    t c
    t
    x
    ,
    T
    h
    r
    o
    w
    a
    b
    l
    e c
    a
    u
    s
    e
    ) {
    c
    t
    x
    .
    c
    l
    o
    s
    e
    (
    )
    ;
    }
    }

    View Slide

  29. DECODER
    ByteToByteDecoder
    ByteToMessageDecoder
    MessageToMessageDecoder

    View Slide

  30. ENCODER
    ByteToByteEncoder
    MessageToByteEncoder
    MessageToMessageEncoder

    View Slide

  31. HYBRIDS
    ByteToByteCodec
    ByteToMessageCodec
    MessageToMessageCodec

    View Slide

  32. OTHER OFTEN USEFUL DECODERS
    ReplayDecoder
    FixedLengthFrameDecoder
    LengthFieldBasedFrameDecoder
    LineBasedFrameDecoder
    DelimiterBasedFrameDecoder

    View Slide

  33. WATCH OUT!
    Repeat with me:
    “I promise to never block the IO-Thread!”

    View Slide

  34. You
    “Uhm well.... but my ChannelHandler needs to
    block. I'm screwed now ?”

    View Slide

  35. “Hold on.... Netty is here for the rescue!”

    View Slide

  36. CHANNELPIPELINE
    The home for all your ChannelHandlers

    View Slide

  37. CHANNELPIPELINE
    Holds the ChannelHandlers for a Channel
    All events will get passed through it
    On the fly modification
    One-to-one relation between Channel and ChannelPipeline

    View Slide

  38. CHANNELPIPELINE
    Kind of a unix-pipe-like thing...
    $ e
    c
    h
    o "
    N
    e
    t
    t
    y i
    s s
    h
    i
    t
    .
    .
    .
    .
    " | s
    e
    d -
    e '
    s
    /
    i
    s /
    i
    s t
    h
    e /
    '
    N
    e
    t
    t
    y i
    s t
    h
    e s
    h
    i
    t
    .
    .
    .
    .
    “You see, everything is adjustable!”

    View Slide

  39. “But my ChannelHandler still block!”
    “ Ah yes, I forgot... ”

    View Slide

  40. ADDING CHANNELHANDLERS
    C
    h
    a
    n
    n
    e
    l c
    h = .
    .
    .
    ;
    C
    h
    a
    n
    n
    e
    l
    P
    i
    p
    e
    l
    i
    n
    e p = c
    h
    .
    p
    i
    p
    e
    l
    i
    n
    e
    (
    )
    ;
    E
    v
    e
    n
    t
    E
    x
    e
    c
    u
    t
    o
    r e
    1 = n
    e
    w D
    e
    f
    a
    u
    l
    t
    E
    v
    e
    n
    t
    E
    x
    e
    c
    u
    t
    o
    r
    (
    1
    6
    )
    ;
    E
    v
    e
    n
    t
    E
    x
    e
    c
    u
    t
    o
    r e
    2 = n
    e
    w D
    e
    f
    a
    u
    l
    t
    E
    v
    e
    n
    t
    E
    x
    e
    c
    u
    t
    o
    r
    (
    8
    )
    ;
    /
    / E
    x
    e
    c
    u
    t
    e
    d i
    n E
    v
    e
    n
    t
    L
    o
    o
    p T
    h
    r
    e
    a
    d
    p
    .
    a
    d
    d
    L
    a
    s
    t
    (
    n
    e
    w M
    y
    P
    r
    o
    t
    o
    c
    o
    l
    C
    o
    d
    e
    c
    (
    )
    )
    ;
    /
    / E
    x
    e
    c
    u
    t
    e
    d i
    n o
    n
    e o
    f t
    h
    e T
    h
    r
    e
    a
    d
    s o
    f e
    1
    p
    .
    a
    d
    d
    L
    a
    s
    t
    (
    e
    1
    , n
    e
    w M
    y
    D
    a
    t
    a
    b
    a
    s
    e
    A
    c
    c
    e
    s
    s
    i
    n
    g
    H
    a
    n
    d
    l
    e
    r
    (
    )
    )
    ;
    /
    / E
    x
    e
    c
    u
    t
    e
    d i
    n o
    n
    e o
    f t
    h
    e T
    h
    r
    e
    a
    d
    s o
    f e
    1
    p
    .
    a
    d
    d
    L
    a
    s
    t
    (
    e
    2
    , n
    e
    w M
    y
    H
    a
    r
    d
    D
    i
    s
    k
    A
    c
    c
    e
    s
    s
    i
    n
    g
    H
    a
    n
    d
    l
    e
    r
    (
    )
    )
    ;

    View Slide

  41. EVENT-LOOP
    Someone needs to do the heavy work!

    View Slide

  42. EVENT-LOOP
    One-to-Many relation between EventLoop and Channel
    Process "events" and hand over work to the
    ChannelPipeline

    View Slide

  43. All the submitted Tasks will get executed in
    the IO-Thread!
    “ Yay, no need to worry about
    synchronization! ”

    View Slide

  44. EVENTLOOP HIERARCHY
    “All the ScheduleExecutorService goodies for
    free!”

    View Slide

  45. EVENTLOOP - HOW DOES IT WORK

    View Slide

  46. RUN A TASK IN THE EVENTLOOP
    p
    u
    b
    l
    i
    c c
    l
    a
    s
    s M
    y
    H
    a
    n
    d
    l
    e
    r e
    x
    t
    e
    n
    d
    s
    C
    h
    a
    n
    n
    e
    l
    O
    u
    t
    b
    o
    u
    n
    d
    M
    e
    s
    s
    a
    g
    e
    H
    a
    n
    d
    l
    e
    r
    A
    d
    a
    p
    t
    e
    r {
    @
    O
    v
    e
    r
    r
    i
    d
    e
    p
    u
    b
    l
    i
    c v
    o
    i
    d f
    l
    u
    s
    h
    (
    C
    h
    a
    n
    n
    e
    l
    H
    a
    n
    d
    l
    e
    r
    C
    o
    n
    t
    e
    x
    t c
    t
    x
    ,
    C
    h
    a
    n
    n
    e
    l
    P
    r
    o
    m
    i
    s
    e p
    r
    o
    m
    i
    s
    e
    ) {
    c
    t
    x
    .
    f
    l
    u
    s
    h
    (
    p
    r
    o
    m
    i
    s
    e
    )
    ;
    i
    f (
    !
    p
    r
    o
    m
    i
    s
    e
    .
    i
    s
    D
    o
    n
    e
    (
    ) {
    /
    / s
    c
    h
    e
    d
    u
    l
    e t
    a
    s
    k f
    o
    r i
    n 3
    0 s
    e
    c
    o
    n
    d
    s
    c
    t
    x
    .
    e
    x
    e
    c
    u
    t
    o
    r
    (
    )
    .
    s
    c
    h
    e
    d
    u
    l
    e
    (
    n
    e
    w M
    y
    W
    r
    i
    t
    e
    T
    i
    m
    e
    o
    u
    t
    T
    a
    s
    k
    (
    p
    r
    o
    m
    i
    s
    e
    )
    , 3
    0
    , T
    i
    m
    e
    U
    n
    i
    t
    .
    S
    E
    C
    O
    N
    D
    S
    )
    ;
    }
    /
    / R
    u
    n a
    n a
    r
    b
    i
    t
    r
    a
    r
    y t
    a
    s
    k f
    r
    o
    m a
    n I
    /
    O t
    h
    r
    e
    a
    d
    .
    c
    t
    x
    .
    e
    x
    e
    c
    u
    t
    o
    r
    (
    )
    .
    e
    x
    e
    c
    u
    t
    e
    (
    n
    e
    w R
    u
    n
    n
    a
    b
    l
    e
    (
    ) { .
    .
    . }
    )
    ;
    }
    }

    View Slide

  47. BOOTSTRAP YOUR APPLICATION
    “It's time to accept some real traffic”

    View Slide

  48. BOOTSTRAP
    Fluent-API a.k.a DSL
    Graceful shutdown
    Lightweight

    View Slide

  49. BOOTSTRAP A SERVER
    S
    e
    r
    v
    e
    r
    B
    o
    o
    t
    s
    t
    r
    a
    p b = n
    e
    w S
    e
    r
    v
    e
    r
    B
    o
    o
    t
    s
    t
    r
    a
    p
    (
    )
    ;
    t
    r
    y {
    b
    .
    g
    r
    o
    u
    p
    (
    n
    e
    w N
    i
    o
    E
    v
    e
    n
    t
    L
    o
    o
    p
    G
    r
    o
    u
    p
    (
    )
    , n
    e
    w N
    i
    o
    E
    v
    e
    n
    t
    L
    o
    o
    p
    G
    r
    o
    u
    p
    (
    )
    )
    .
    c
    h
    a
    n
    n
    e
    l
    (
    N
    i
    o
    S
    e
    r
    v
    e
    r
    S
    o
    c
    k
    e
    t
    C
    h
    a
    n
    n
    e
    l
    .
    c
    l
    a
    s
    s
    )
    .
    l
    o
    c
    a
    l
    A
    d
    d
    r
    e
    s
    s
    (
    n
    e
    w I
    n
    e
    t
    S
    o
    c
    k
    e
    t
    A
    d
    d
    r
    e
    s
    s
    (
    p
    o
    r
    t
    )
    )
    .
    c
    h
    i
    l
    d
    H
    a
    n
    d
    l
    e
    r
    (
    n
    e
    w C
    h
    a
    n
    n
    e
    l
    I
    n
    i
    t
    i
    a
    l
    i
    z
    e
    r
    <
    S
    o
    c
    k
    e
    t
    C
    h
    a
    n
    n
    e
    l
    >
    (
    ) {
    @
    O
    v
    e
    r
    r
    i
    d
    e
    p
    u
    b
    l
    i
    c v
    o
    i
    d i
    n
    i
    t
    C
    h
    a
    n
    n
    e
    l
    (
    S
    o
    c
    k
    e
    t
    C
    h
    a
    n
    n
    e
    l c
    h
    )
    t
    h
    r
    o
    w
    s E
    x
    c
    e
    p
    t
    i
    o
    n {
    c
    h
    .
    p
    i
    p
    e
    l
    i
    n
    e
    (
    )
    .
    a
    d
    d
    L
    a
    s
    t
    (
    n
    e
    w Y
    o
    u
    r
    C
    h
    a
    n
    n
    e
    l
    H
    a
    n
    d
    l
    e
    r
    (
    )
    )
    ;
    }
    }
    )
    ;
    C
    h
    a
    n
    n
    e
    l
    F
    u
    t
    u
    r
    e f = b
    .
    b
    i
    n
    d
    (
    )
    .
    s
    y
    n
    c
    (
    )
    ;
    f
    .
    c
    h
    a
    n
    n
    e
    l
    (
    )
    .
    c
    l
    o
    s
    e
    F
    u
    t
    u
    r
    e
    (
    )
    .
    s
    y
    n
    c
    (
    )
    ;
    } f
    i
    n
    a
    l
    l
    y {
    b
    .
    s
    h
    u
    t
    d
    o
    w
    n
    (
    )
    ;
    }

    View Slide

  50. BOOTSTRAP A CLIENT
    B
    o
    o
    t
    s
    t
    r
    a
    p b = n
    e
    w B
    o
    o
    t
    s
    t
    r
    a
    p
    (
    )
    ;
    t
    r
    y {
    b
    .
    g
    r
    o
    u
    p
    (
    n
    e
    w N
    i
    o
    E
    v
    e
    n
    t
    L
    o
    o
    p
    G
    r
    o
    u
    p
    (
    )
    )
    .
    c
    h
    a
    n
    n
    e
    l
    (
    N
    i
    o
    S
    o
    c
    k
    e
    t
    C
    h
    a
    n
    n
    e
    l
    .
    c
    l
    a
    s
    s
    )
    .
    r
    e
    m
    o
    t
    e
    A
    d
    d
    r
    e
    s
    s
    (
    n
    e
    w I
    n
    e
    t
    S
    o
    c
    k
    e
    t
    A
    d
    d
    r
    e
    s
    s
    (
    "
    1
    0
    .
    0
    .
    0
    .
    1
    "
    , 2
    5
    )
    )
    .
    h
    a
    n
    d
    l
    e
    r
    (
    n
    e
    w C
    h
    a
    n
    n
    e
    l
    I
    n
    i
    t
    i
    a
    l
    i
    z
    e
    r
    <
    S
    o
    c
    k
    e
    t
    C
    h
    a
    n
    n
    e
    l
    >
    (
    ) {
    @
    O
    v
    e
    r
    r
    i
    d
    e
    p
    u
    b
    l
    i
    c v
    o
    i
    d i
    n
    i
    t
    C
    h
    a
    n
    n
    e
    l
    (
    S
    o
    c
    k
    e
    t
    C
    h
    a
    n
    n
    e
    l c
    h
    ) {
    c
    h
    .
    p
    i
    p
    e
    l
    i
    n
    e
    (
    )
    .
    a
    d
    d
    L
    a
    s
    t
    (
    n
    e
    w Y
    o
    u
    r
    C
    h
    a
    n
    n
    e
    l
    H
    a
    n
    d
    l
    e
    r
    (
    )
    )
    ;
    }
    }
    )
    ;
    C
    h
    a
    n
    n
    e
    l
    F
    u
    t
    u
    r
    e f = b
    .
    c
    o
    n
    n
    e
    c
    t
    (
    )
    .
    s
    y
    n
    c
    (
    )
    ;
    f
    .
    c
    h
    a
    n
    n
    e
    l
    (
    )
    .
    c
    l
    o
    s
    e
    F
    u
    t
    u
    r
    e
    (
    )
    .
    s
    y
    n
    c
    (
    )
    ;
    } f
    i
    n
    a
    l
    l
    y {
    b
    .
    s
    h
    u
    t
    d
    o
    w
    n
    (
    )
    ;
    }

    View Slide

  51. GENERAL
    ASL2 license
    Hosted at Github
    Community-driven

    View Slide

  52. COMPANIES
    Red Hat
    Twitter
    Facebook
    Boundary
    Typesafe
    “And many more... ”

    View Slide

  53. OPENSOURCE PROJECTS
    HornetQ
    Infinispan
    Vert.x
    Play Framework
    Finangle
    Cassandra
    “And again many more....”

    View Slide

  54. THINGS TO COME
    Chained ChannelPipelines
    Native edge-triggered Transport*
    Lambda support

    View Slide

  55. OTHER NIO FRAMEWORKS
    Apache MINA
    Grizzly
    HawtDispatch*
    JBoss XNIO*

    View Slide

  56. GET INVOLVED
    Mailinglist
    https://groups.google.com/forum/#!forum/netty
    IRC
    #netty irc.freenode.org
    Website
    http://netty.io
    GitHub
    https://github.com/netty/netty/

    View Slide

  57. TOO LOW LEVEL ?
    “You may like Vert.x which is based on Netty
    and also polyglot!”

    View Slide

  58. NOT ENOUGH INFO?
    http://www.manning.com/maurer/
    “You may want to buy my book...”

    View Slide

  59. “Questions please!”

    View Slide

  60. THANKS!

    View Slide