Table of Contents
Available as of Camel 2.14
The ahc-ws component provides Websocket based endpoints for a client communicating with external servers over Websocket (as a client opening a websocket connection to an external server). The component uses the AHC component that in turn uses the Async Http Client library.
Maven users will need to add the following dependency to
their pom.xml for this component:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-ahc-ws</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency>ahc-ws://hostname[:port][/resourceUri][?options] ahc-wss://hostname[:port][/resourceUri][?options]
Will by default use port 80 for ahc-ws and 443 for ahc-wss.
As the AHC-WS component is based on the AHC component, you can use the various configuration options of the AHC component.
The AHC Websocket component supports 6 options which are listed below.
{% raw %}
| Name | Java Type | Description |
|---|---|---|
client |
| To use a custom AsyncHttpClient |
binding |
| To use a custom AhcBinding which allows to control how to bind between AHC and Camel. |
clientConfig |
| To configure the AsyncHttpClient to use a custom com.ning.http.client.AsyncHttpClientConfig instance. |
sslContextParameters |
| Reference to a org.apache.camel.util.jsse.SSLContextParameters in the Registry. Note that configuring this option will override any SSL/TLS configuration options provided through the clientConfig option at the endpoint or component level. |
allowJavaSerializedObject |
| Whether to allow java serialization when a request uses context-type=application/x-java-serialized-object This is by default turned off. If you enable this then be aware that Java will deserialize the incoming data from the request to Java and that can be a potential security risk. |
headerFilterStrategy |
| To use a custom HeaderFilterStrategy to filter header to and from Camel message. |
{% endraw %}
The AHC Websocket component supports 18 endpoint options which are listed below:
{% raw %}
| Name | Group | Default | Java Type | Description |
|---|---|---|---|---|
httpUri | common |
| Required The URI to use such as http://hostname:port/path | |
binding | common |
| To use a custom AhcBinding which allows to control how to bind between AHC and Camel. | |
bridgeEndpoint | common |
|
| If the option is true then the Exchange.HTTP_URI header is ignored and use the endpoint’s URI for request. You may also set the throwExceptionOnFailure to be false to let the AhcProducer send all the fault response back. |
bufferSize | common |
|
| The initial in-memory buffer size used when transferring data between Camel and AHC Client. |
headerFilterStrategy | common |
| To use a custom HeaderFilterStrategy to filter header to and from Camel message. | |
throwExceptionOnFailure | common |
|
| Option to disable throwing the AhcOperationFailedException in case of failed responses from the remote server. This allows you to get all responses regardless of the HTTP status code. |
transferException | common |
|
| If enabled and an Exchange failed processing on the consumer side and if the caused Exception was send back serialized in the response as a application/x-java-serialized-object content type (for example using Jetty or Servlet Camel components). On the producer side the exception will be deserialized and thrown as is instead of the AhcOperationFailedException. The caused exception is required to be serialized. This is by default turned off. If you enable this then be aware that Java will deserialize the incoming data from the request to Java and that can be a potential security risk. |
bridgeErrorHandler | consumer |
|
| Allows for bridging the consumer to the Camel routing Error Handler which mean any exceptions occurred while the consumer is trying to pickup incoming messages or the likes will now be processed as a message and handled by the routing Error Handler. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions that will be logged at WARN/ERROR level and ignored. |
sendMessageOnError | consumer |
|
| Whether to send an message if the web-socket listener received an error. |
exceptionHandler | consumer (advanced) |
| To let the consumer use a custom ExceptionHandler. Notice if the option bridgeErrorHandler is enabled then this options is not in use. By default the consumer will deal with exceptions that will be logged at WARN/ERROR level and ignored. | |
exchangePattern | consumer (advanced) |
| Sets the exchange pattern when the consumer creates an exchange. | |
connectionClose | producer |
|
| Define if the Connection Close header has to be added to HTTP Request. This parameter is false by default |
useStreaming | producer |
|
| To enable streaming to send data as multiple text fragments. |
clientConfig | advanced |
| To configure the AsyncHttpClient to use a custom com.ning.http.client.AsyncHttpClientConfig instance. | |
clientConfigOptions | advanced |
| To configure the AsyncHttpClientConfig using the key/values from the Map. | |
synchronous | advanced |
|
| Sets whether synchronous processing should be strictly used or Camel is allowed to use asynchronous processing (if supported). |
clientConfigRealmOptions | security |
| To configure the AsyncHttpClientConfig Realm using the key/values from the Map. | |
sslContextParameters | security |
| Reference to a org.apache.camel.util.jsse.SSLContextParameters in the Registry. This reference overrides any configured SSLContextParameters at the component level. See Using the JSSE Configuration Utility. Note that configuring this option will override any SSL/TLS configuration options provided through the clientConfig option at the endpoint or component level. |
{% endraw %}
An ahc-ws endpoint can either write data to the socket or read from the socket, depending on whether the endpoint is configured as the producer or the consumer, respectively.
In the route below, Camel will write to the specified websocket connection.
from("direct:start")
.to("ahc-ws://targethost");And the equivalent Spring sample:
<camelContext xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="direct:start"/> <to uri="ahc-ws://targethost"/> </route> </camelContext>
In the route below, Camel will read from the specified websocket connection.
from("ahc-ws://targethost")
.to("direct:next");And the equivalent Spring sample:
<camelContext xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="ahc-ws://targethost"/> <to uri="direct:next"/> </route> </camelContext>