# Netty

## Netty Component

The netty component in Apache Camel is a socket communication component, based on the JBoss Netty community offering (available under an Apache 2.0 license). Netty is a NIO client server framework which enables quick and easy development of network applications such as protocol servers and clients. Netty greatly simplifies and streamlines network programming such as TCP and UDP socket server.

This Apache Camel component supports both producer and consumer endpoints.

The netty component has several options and allows fine-grained control of a number of TCP/UDP communication parameters (buffer sizes, keepAlives, tcpNoDelay etc) and facilitates both In-Only and In-Out communication on a Apache Camel route.

## URI format

The URI scheme for a netty component is as follows

    netty:tcp://localhost:99999[?options]
    netty:udp://remotehost:99999/[?options]

This component supports producer and consumer endpoints for both TCP and UDP.

You can append query options to the URI in the following format, `?option=value&option=value&...`

## Options

Table 63 list the Netty uri options:

Table 63. URI options

Name | Default Value | Description
--- | --- | ---
`keepAlive` | `true` | Setting to ensure socket is not closed due to inactivity
`tcpNoDelay` | `true` | Setting to improve TCP protocol performance
`broadcast` | `false` | Setting to choose Multicast over UDP
`connectTimeout` | `10000` | Time to wait for a socket connection to be available. Value is in millis.
`reuseAddress` | `true` | Setting to facilitate socket multiplexing
`sync` | `true` | Setting to set endpoint as one-way or request-response
`synchronous` | `false` | Apache Camel 2.10 Sets whether the `Asynchronous Routing Engine` is used. Set to `false`, the `Asynchronous Routing Engine` is used; otherwise, it is not.
`ssl` | `false` | Setting to specify whether SSL encryption is applied to this endpoint
`sendBufferSize` | `65536 bytes` | The TCP/UDP buffer sizes to be used during outbound communication. Size is bytes.
`receiveBufferSize` | `65536 bytes` | The TCP/UDP buffer sizes to be used during inbound communication. Size is bytes.
`corePoolSize` | `10` | The number of allocated threads at component startup. Defaults to 10
`maxPoolSize` | `100` | The maximum number of threads that may be allocated to this endpoint. Defaults to 100
`disconnect` | `false` | Whether or not to disconnect(close) from Netty Channel right after use. Can be used for both consumer and producer.
`lazyChannelCreation` | `true` | Channels can be lazily created to avoid exceptions, if the remote server is not up and running when the Apache Camel producer is started.
`transferExchange` | `false` | Only used for TCP. You can transfer the exchange over the wire instead of just the body. The following fields are transferred: In body, Out body, fault body, In headers, Out headers, fault headers, exchange properties, exchange exception. This requires that the objects are serializable. Apache Camel will exclude any non-serializable objects and log it at WARN level.
`disconnectOnNoReply` | `true` | If sync is enabled then this option dictates NettyConsumer if it should disconnect where there is no reply to send back.
`noReplyLogLevel` | `WARN` | If sync is enabled this option dictates NettyConsumer which logging level to use when logging a there is no reply to send back. Values are: `FATAL, ERROR, INFO, DEBUG, OFF`.
`allowDefaultCodec` | `true` | Apache Camel 2.4: The netty component installs a default codec if both, encoder/deocder is null and textline is false. Setting allowDefaultCodec to false prevents the netty component from installing a default codec as the first element in the filter chain.
`textline` | `false` | Apache Camel 2.4: Only used for TCP. If no codec is specified, you can use this flag to indicate a text line based codec; if not specified or the value is false, then Object Serialization is assumed over TCP.
`delimiter` | `LINE` | Apache Camel 2.4: The delimiter to use for the textline codec. Possible values are `LINE` and `NULL`.
`decoderMaxLineLength` | `1024` | Apache Camel 2.4: The max line length to use for the textline codec.
`autoAppendDelimiter` | `true` | Apache Camel 2.4: Whether or not to auto append missing end delimiter when sending using the textline codec.
`encoding` | `null` | Apache Camel 2.4: The encoding (a charset name) to use for the textline codec. If not provided, Camel will use the JVM default Charset.
`workerCount` | `null` | Apache Camel 2.9: Working in nio mode, netty uses its default WorkerCount parameter, which is `cpu_core_threads*2`. Use this option to override netty's default WorkerCount.
`sslContextParametersRef` | `null` | Apache Camel 2.9: Reference to an `org.apache.camel.util.jsse.SSLContextParameters` in the Registry. This reference overrides any configured `SSLContextParameters` at the component level. See XREF to Using the JSSE Configuration Utiltity [TBAdded]
`receiveBufferSizePredictor` | `null` | Apache Camel 2.9: Configures the buffer size predictor. For details, see the Jetty documentation.
`orderedThreadPoolExecutor` | `true` | Apache Camel 2.10.2: Specifies whether to use an ordered thread pool to ensure that events are processed in order on the same channel. For details, see the Netty javadoc of `org.jboss.netty.handler.exception.OrderedMemoryAwareThreadPoolExecutor`.
`maximumPoolSize` | `16` | Apache Camel 2.10.2: Specifies the core pool size for the ordered thread pool, if it's in use.
`producerPoolMaxActive` | `-1` | Apache Camel 2.10.3: Producer only. Sets the cap on the number of objects that can be allocated by the pool (checked out to clients, or idle awaiting checkout) at a given time. Use a negative value for no limit.
`producerPoolMinIdle` | `0` | Apache Camel 2.10.3: Producer only. Sets the minimum number of instances allowed in the producer pool before the evictor thread (if active) spawns new objects.
`producerPoolMaxIdle` | `100` | Apache Camel 2.10.3: Producer only. Sets the cap on the number of idle instances in the pool.
`producerPoolMinEvictableIdle` | `30000` | Apache Camel 2.10.3: Producer only. Sets the minimum amount of time, in milliseconds, that an object can sit idle in the pool before it's eligible for eviction by the idle object evictor.

## Registry based Options

Codec Handlers and SSL Keystores can be enlisted in the Registry, such as in the Spring XML file. The values that could be passed in, are the following:

Table 64. Registry-based options

Name | Description
--- | ---
`passphrase` | password setting to use in order to encrypt/decrypt payloads sent using SSH
`keyStoreFormat` | keystore format to be used for payload encryption. Defaults to "JKS" if not set
`securityProvider` | Security provider to be used for payload encryption. Defaults to "SunX509" if not set.
`keyStoreFile` | Client side certificate keystore to be used for encryption
`trustStoreFile` | Server side certificate keystore to be used for encryption
`sslHandler` | Reference to a class that could be used to return an SSL Handler
`encoder` | A custom Handler class that can be used to perform special marshalling of outbound payloads. Must override `org.jboss.netty.channel.ChannelDownStreamHandler`.
`encorders` | A list of encoder to be used. You can use a String which have values separated by comma, and have the values be looked up in the Registry. Just remember to prefix the value with # so Apache Camel knows it should lookup.
`decoder` | A custom Handler class that can be used to perform special marshalling of inbound payloads. Must override `org.jboss.netty.channel.ChannelUpStreamHandler`.
`decoders` | A list of decorder to be used. You can use a String which have values separated by comma, and have the values be looked up in the Registry. Just remember to prefix the value with # so Apache Camel knows it should lookup.

## Using nonshareable encoders or decoders

If your encoders or decoders are not shareable (for example, they do not have the `@Shareable` class annotation), they must implement the `org.apache.camel.component.netty.ChannelHandlerFactory` interface, and return a new instance in the `newChannelHandler` method. This is required to ensure that the encoder/decoder can be used safely. If not, the Netty component will log a `WARN` when an endpoint is created.

| Note
The Netty component offers an `org.apache.camel.component.netty.ChannelHandlerFactories` factory class that provides a number of commonly used methods.

## Netty Producer

In Producer mode, the component provides the ability to send payloads to a socket endpoint using either TCP or UDP protocols (with optional SSL support).

The producer mode supports both one-way and request-response based operations.

## Netty Consumer

In Consumer mode, the component provides the ability to:

- listen on a specified socket using either TCP or UDP protocols (with optional SSL support),

- receive requests on the socket using text/xml, binary and serialized object based payloads and

- send them along on a route as message exchanges.

The consumer mode supports both one-way and request-response based operations.

## A UDP Netty endpoint using Request-Reply and serialized object payload

    RouteBuilder builder = new RouteBuilder() {
      public void configure() {
        from("netty:udp://localhost:5155?sync=true")
          .process(new Processor() {
             public void process(Exchange exchange) throws Exception {
               Poetry poetry = (Poetry) exchange.getIn().getBody();
               poetry.setPoet("Dr. Sarojini Naidu");
               exchange.getOut().setBody(poetry);
             }
           }
        }
    };

## A TCP based Netty consumer endpoint using One-way communication

    RouteBuilder builder = new RouteBuilder() {
      public void configure() {
           from("netty:tcp://localhost:5150")
               .to("mock:result");
      }
    };

## An SSL/TCP based Netty consumer endpoint using Request-Reply communication

- Using basic SSL/TLS configuration on the Jetty component

      JndiRegistry registry = new JndiRegistry(createJndiContext());
      registry.bind("password", "changeit");
      registry.bind("ksf", new File("src/test/resources/keystore.jks"));
      registry.bind("tsf", new File("src/test/resources/keystore.jks"));

      context.createRegistry(registry);
      context.addRoutes(new RouteBuilder() {
        public void configure() {
            String netty_ssl_endpoint =
               "netty:tcp://localhost:5150?sync=true&ssl=true&passphrase=#password"
               + "&keyStoreFile=#ksf&trustStoreFile=#tsf";
            String return_string =
               "When You Go Home, Tell Them Of Us And Say,"
               + "For Your Tomorrow, We Gave Our Today.";

            from(netty_ssl_endpoint)
             .process(new Processor() {
                public void process(Exchange exchange) throws Exception {
                  exchange.getOut().setBody(return_string);
                }
             }
        }
      });

- Programmatic configuration of the component

      KeyStoreParameters ksp = new KeyStoreParameters();
      ksp.setResource("/users/home/server/keystore.jks");
      ksp.setPassword("keystorePassword");

      KeyManagersParameters kmp = new KeyManagersParameters();
      kmp.setKeyStore(ksp);
      kmp.setKeyPassword("keyPassword");

      SSLContextParameters scp = new SSLContextParameters();
      scp.setKeyManagers(kmp);

      NettyComponent nettyComponent = getContext().getComponent("netty", NettyComponent.class);
      nettyComponent.setSslContextParameters(scp);

- Spring DSL-based configuration of the endpoint

      ...
        <camel:sslContextParameters
            id="sslContextParameters">
          <camel:keyManagers
             keyPassword="keyPassword">
            <camel:keyStore
                resource="/users/home/server/keystore.jks"
                password="keystorePassword"/>
          </camel:keyManagers>
        </camel:sslContextParameters>...
      ...
        <to uri="netty:tcp://localhost:5150?sync=true&ssl=true&sslContextParameters=#sslContextParameters"/>
      ...

## Using Multiple Codecs

In certain cases it may be necessary to add chains of encoders and decoders to the netty pipeline. To add multpile codecs to a Apache Camel netty endpoint the 'encoders' and 'decoders' uri parameters should be used. Like the 'encoder' and 'decoder' parameters they are used to supply references (to lists of ChannelUpstreamHandlers and ChannelDownstreamHandlers) that should be added to the pipeline. Note that if encoders is specified then the encoder param will be ignored, similarly for decoders and the decoder param.

The lists of codecs need to be added to the Apache Camel's registry so they can be resolved when the endpoint is created.

    ChannelHandlerFactory lengthDecoder=channelHandlerFactories.newLengthFieldBasedFrameDecoder(1048576,
                0, 4, 0, 4);

    StringDecoder stringDecoder = new StringDecoder();
    registry.bind("length-decoder", lengthDecoder);
    registry.bind("string-decoder", stringDecoder);

    LengthFieldPrepender lengthEncoder = new LengthFieldPrepender(4);
    StringEncoder stringEncoder = new StringEncoder();
    registry.bind("length-encoder", lengthEncoder);
    registry.bind("string-encoder", stringEncoder);

    List<ChannelUpstreamHandler> decoders = new ArrayList<ChannelUpstreamHandler>();
    decoders.add(lengthDecoder);
    decoders.add(stringDecoder);

    List<ChannelDownstreamHandler> encoders = new ArrayList<ChannelDownstreamHandler>();
    encoders.add(lengthEncoder);
    encoders.add(stringEncoder);

    registry.bind("encoders", encoders);
    registry.bind("decoders", decoders);

Spring's native collections support can be used to specify the codec lists in an application context

    <util:list id="decoders" list-class="java.util.LinkedList">
            <bean class="org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder">
                <constructor-arg value="1048576"/>
                <constructor-arg value="0"/>
                <constructor-arg value="4"/>
                <constructor-arg value="0"/>
                <constructor-arg value="4"/>
            </bean>
            <bean class="org.jboss.netty.handler.codec.string.StringDecoder"/>
        </util:list>

        <util:list id="encoders" list-class="java.util.LinkedList">
            <bean class="org.jboss.netty.handler.codec.frame.LengthFieldPrepender">
                <constructor-arg value="4"/>
            </bean>
            <bean class="org.jboss.netty.handler.codec.string.StringEncoder"/>
        </util:list>

        <bean id="length-encoder" class="org.jboss.netty.handler.codec.frame.LengthFieldPrepender">
            <constructor-arg value="4"/>
        </bean>
        <bean id="string-encoder" class="org.jboss.netty.handler.codec.string.StringEncoder"/>

        <bean id="length-decoder" class="org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder">
            <constructor-arg value="1048576"/>
            <constructor-arg value="0"/>
            <constructor-arg value="4"/>
            <constructor-arg value="0"/>
            <constructor-arg value="4"/>
        </bean>
        <bean id="string-decoder" class="org.jboss.netty.handler.codec.string.StringDecoder"/>

    </beans>

The bean names can then be used in netty endpoint definitions either as a comma separated list or contained in a List e.g.

             from("direct:multiple-codec").to("netty:tcp://localhost:{{port}}?encoders=#encoders&sync=false");

             from("netty:tcp://localhost:{{port}}?decoders=#length-decoder,#string-decoder&sync=false").to("mock:multiple-codec");
          }
        };
      }
    }

or via Spring:

    <camelContext id="multiple-netty-codecs-context" xmlns="http://camel.apache.org/schema/spring">
        <route>
            <from uri="direct:multiple-codec"/>
            <to uri="netty:tcp://localhost:5150?encoders=#encoders&ync=false"/>
        </route>
        <route>
            <from uri="netty:tcp://localhost:5150?decoders=#length-decoder,#string-decoder&ync=false"/>
            <to uri="mock:multiple-codec"/>
        </route>
    </camelContext>

## Closing Channel When Complete

When acting as a server you sometimes want to close the channel when, for example, a client conversion is finished. You can do this by simply setting the endpoint option `disconnect=true`.

However you can also instruct Apache Camel on a per message basis as follows. To instruct Apache Camel to close the channel, you should add a header with the key `CamelNettyCloseChannelWhenComplete` set to a boolean `true` value. For instance, the example below will close the channel after it has written the bye message back to the client:

            from("netty:tcp://localhost:8080").process(new Processor() {
                public void process(Exchange exchange) throws Exception {
                    String body = exchange.getIn().getBody(String.class);
                    exchange.getOut().setBody("Bye " + body);
                    // some condition which determines if we should close
                    if (close) {
                        exchange.getOut().setHeader(NettyConstants.NETTY_CLOSE_CHANNEL_WHEN_COMPLETE, true);
                    }
                }
            });

## Adding custom channel pipeline factories to gain complete control over a created pipeline

Available as of Apache Camel 2.5

Custom channel pipelines provide complete control to the user over the handler/interceptor chain by inserting custom handler(s), encoder(s) and decoders without having to specify them in the Netty Endpoint URL in a very simple way.

In order to add a custom pipeline, a custom channel pipeline factory must be created and registered with the context through the context registry (`JNDIRegistry`,or the Spring `ApplicationContextRegistry` etc).

A custom pipeline factory must be constructed as follows

- A Producer linked channel pipeline factory must extend the abstract class, `ClientPipelineFactory`.

- A Consumer linked channel pipeline factory must extend the abstract class, `ServerPipelineFactory`.

- The classes can optionally override the `getPipeline()` method in order to insert custom handler(s), encoder(s) and decoder(s). Not overriding the `getPipeline()` method creates a pipeline with no handlers, encoders or decoders wired to the pipeline.

The example below shows how ServerChannel Pipeline factory may be created

    public class SampleServerChannelPipelineFactory extends ServerPipelineFactory {
        private int maxLineSize = 1024;

        public ChannelPipeline getPipeline() throws Exception {

            ChannelPipeline channelPipeline = Channels.pipeline();

            channelPipeline.addLast("encoder-SD", new StringEncoder(CharsetUtil.UTF_8));
            channelPipeline.addLast("decoder-DELIM", new DelimiterBasedFrameDecoder(maxLineSize, true, Delimiters.lineDelimiter()));
            channelPipeline.addLast("decoder-SD", new StringDecoder(CharsetUtil.UTF_8));
            // here we add the default Camel ServerChannelHandler for the consumer,
            to allow Camel to route the message etc.
            channelPipeline.addLast("handler", new ServerChannelHandler(consumer));

            return channelPipeline;
       }
    }

The custom channel pipeline factory can then be added to the registry and instantiated/utilized on a camel route as follows:

    Registry registry = camelContext.getRegistry();
    serverPipelineFactory = new TestServerChannelPipelineFactory();
    registry.bind("spf", serverPipelineFactory);
    context.addRoutes(new RouteBuilder() {
        public void configure() {
            String netty_ssl_endpoint =
              "netty:tcp://localhost:5150?serverPipelineFactory=#spf";
            String return_string =
              "When You Go Home, Tell Them Of Us And Say,"
              + "For Your Tomorrow, We Gave Our Today.";

            from(netty_ssl_endpoint)
              .process(new Processor() {
                public void process(Exchange exchange) throws Exception {
                  exchange.getOut().setBody(return_string);
                }
              }
        }
    });