Async HTTP Client (AHC)
Since Camel 2.8
Only producer is supported
The AHC component provides HTTP based endpoints for consuming external HTTP resources (as a client to call external servers using HTTP). The component 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</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency>
URI format
ahc:http://hostname[:port][/resourceUri][?options] ahc:https://hostname[:port][/resourceUri][?options]
Will by default use port 80 for HTTP and 443 for HTTPS.
You can append query options to the URI in the following format,
?option=value&option=value&…
Configuring Options
Camel components are configured on two separate levels:
-
component level
-
endpoint level
Configuring Component Options
The component level is the highest level which holds general and common configurations that are inherited by the endpoints. For example a component may have security settings, credentials for authentication, urls for network connection and so forth.
Some components only have a few options, and others may have many. Because components typically have pre configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.
Configuring components can be done with the Component DSL, in a configuration file (application.properties|yaml), or directly with Java code.
Configuring Endpoint Options
Where you find yourself configuring the most is on endpoints, as endpoints often have many options, which allows you to configure what you need the endpoint to do. The options are also categorized into whether the endpoint is used as consumer (from) or as a producer (to), or used for both.
Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL as a type safe way of configuring endpoints.
A good practice when configuring options is to use Property Placeholders, which allows to not hardcode urls, port numbers, sensitive information, and other settings. In other words placeholders allows to externalize the configuration from your code, and gives more flexibility and reuse.
The following two sections lists all the options, firstly for the component followed by the endpoint.
Component Options
The Async HTTP Client (AHC) component supports 9 options, which are listed below.
Name | Description | Default | Type |
---|---|---|---|
lazyStartProducer (producer) |
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. |
boolean |
|
allowJavaSerializedObject (advanced) |
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. |
boolean |
|
autowiredEnabled (advanced) |
Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. |
true |
boolean |
binding (advanced) |
To use a custom AhcBinding which allows to control how to bind between AHC and Camel. |
AhcBinding |
|
client (advanced) |
To use a custom AsyncHttpClient. |
AsyncHttpClient |
|
clientConfig (advanced) |
To configure the AsyncHttpClient to use a custom com.ning.http.client.AsyncHttpClientConfig instance. |
AsyncHttpClientConfig |
|
headerFilterStrategy (filter) |
To use a custom org.apache.camel.spi.HeaderFilterStrategy to filter header to and from Camel message. |
HeaderFilterStrategy |
|
sslContextParameters (security) |
Reference to a org.apache.camel.support.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. |
SSLContextParameters |
|
useGlobalSslContextParameters (security) |
Enable usage of global SSL context parameters. |
boolean |
Endpoint Options
The Async HTTP Client (AHC) endpoint is configured using URI syntax:
ahc:httpUri
with the following path and query parameters:
Path Parameters (1 parameters)
Name | Description | Default | Type |
---|---|---|---|
httpUri (producer) |
Required The URI to use such as http://hostname:port/path. |
URI |
Query Parameters (13 parameters)
Name | Description | Default | Type |
---|---|---|---|
bridgeEndpoint (producer) |
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. |
boolean |
|
bufferSize (producer) |
The initial in-memory buffer size used when transferring data between Camel and AHC Client. |
4096 |
int |
connectionClose (producer) |
Define if the Connection Close header has to be added to HTTP Request. This parameter is false by default. |
boolean |
|
cookieHandler (producer) |
Configure a cookie handler to maintain a HTTP session. |
CookieHandler |
|
headerFilterStrategy (producer) |
To use a custom HeaderFilterStrategy to filter header to and from Camel message. |
HeaderFilterStrategy |
|
lazyStartProducer (producer) |
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. |
boolean |
|
throwExceptionOnFailure (producer) |
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. |
true |
boolean |
transferException (producer) |
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. |
boolean |
|
binding (advanced) |
To use a custom AhcBinding which allows to control how to bind between AHC and Camel. |
AhcBinding |
|
clientConfig (advanced) |
To configure the AsyncHttpClient to use a custom com.ning.http.client.AsyncHttpClientConfig instance. |
AsyncHttpClientConfig |
|
clientConfigOptions (advanced) |
To configure the AsyncHttpClientConfig using the key/values from the Map. |
Map |
|
clientConfigRealmOptions (security) |
To configure the AsyncHttpClientConfig Realm using the key/values from the Map. |
Map |
|
sslContextParameters (security) |
Reference to a org.apache.camel.support.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. |
SSLContextParameters |
Message Headers
Name | Type | Description |
---|---|---|
|
|
URI to call. Will override existing URI set directly on the endpoint. |
|
|
Request URI’s path, the header will be used to build the request URI
with the HTTP_URI. If the path is start with "/", http producer will try
to find the relative path based on the Exchange.HTTP_BASE_URI header or
the |
|
|
Camel 2.11 onwards: URI parameters. Will override existing URI parameters set directly on the endpoint. |
|
|
The HTTP response code from the external server. Is 200 for OK. |
|
|
Character encoding. |
|
|
The HTTP content type. Is set on both the IN and OUT message to provide
a content type, such as |
|
|
The HTTP content encoding. Is set on both the IN and OUT message to
provide a content encoding, such as |
Message Body
Camel will store the HTTP response from the external server on the OUT body. All headers from the IN message will be copied to the OUT message, so headers are preserved during routing. Additionally Camel will add the HTTP response headers as well to the OUT message headers.
Response code
Camel will handle according to the HTTP response code:
-
Response code is in the range 100..299, Camel regards it as a success response.
-
Response code is in the range 300..399, Camel regards it as a redirection response and will throw a
AhcOperationFailedException
with the information. -
Response code is 400+, Camel regards it as an external server failure and will throw a
AhcOperationFailedException
with the information.throwExceptionOnFailure
The option,
throwExceptionOnFailure
, can be set tofalse
to prevent theAhcOperationFailedException
from being thrown for failed response codes. This allows you to get any response from the remote server.
AhcOperationFailedException
This exception contains the following information:
-
The HTTP status code
-
The HTTP status line (text of the status code)
-
Redirect location, if server returned a redirect
-
Response body as a
java.lang.String
, if server provided a body as response
Calling using GET or POST
The following algorithm is used to determine if either GET
or POST
HTTP method should be used:
1. Use method provided in header.
2. GET
if query string is provided in header.
3. GET
if endpoint is configured with a query string.
4. POST
if there is data to send (body is not null).
5. GET
otherwise.
Configuring URI to call
You can set the HTTP producer’s URI directly form the endpoint URI. In
the route below, Camel will call out to the external server, oldhost
,
using HTTP.
from("direct:start")
.to("ahc:http://oldhost");
And the equivalent Spring sample:
<camelContext xmlns="http://activemq.apache.org/camel/schema/spring">
<route>
<from uri="direct:start"/>
<to uri="ahc:http://oldhost"/>
</route>
</camelContext>
You can override the HTTP endpoint URI by adding a header with the key,
Exchange.HTTP_URI
, on the message.
from("direct:start")
.setHeader(Exchange.HTTP_URI, constant("http://newhost"))
.to("ahc:http://oldhost");
Configuring URI Parameters
The ahc producer supports URI parameters to be sent to the HTTP
server. The URI parameters can either be set directly on the endpoint
URI or as a header with the key Exchange.HTTP_QUERY
on the message.
from("direct:start")
.to("ahc:http://oldhost?order=123&detail=short");
Or options provided in a header:
from("direct:start")
.setHeader(Exchange.HTTP_QUERY, constant("order=123&detail=short"))
.to("ahc:http://oldhost");
How to set the http method to the HTTP producer
The HTTP component provides a way to set the HTTP request method by setting the message header. Here is an example;
from("direct:start")
.setHeader(Exchange.HTTP_METHOD, constant("POST"))
.to("ahc:http://www.google.com")
.to("mock:results");
And the equivalent Spring sample:
<camelContext xmlns="http://activemq.apache.org/camel/schema/spring">
<route>
<from uri="direct:start"/>
<setHeader name="CamelHttpMethod">
<constant>POST</constant>
</setHeader>
<to uri="ahc:http://www.google.com"/>
<to uri="mock:results"/>
</route>
</camelContext>
Configuring charset
If you are using POST
to send data you can configure the charset
using the Exchange
property:
exchange.setProperty(Exchange.CHARSET_NAME, "iso-8859-1");
URI Parameters from the endpoint URI
In this sample we have the complete URI endpoint that is just what you
would have typed in a web browser. Multiple URI parameters can of course
be set using the &
character as separator, just as you would in the
web browser. Camel does no tricks here.
// we query for Camel at the Google page
template.sendBody("ahc:http://www.google.com/search?q=Camel", null);
URI Parameters from the Message
Map headers = new HashMap();
headers.put(Exchange.HTTP_QUERY, "q=Camel&lr=lang_en");
// we query for Camel and English language at Google
template.sendBody("ahc:http://www.google.com/search", null, headers);
In the header value above notice that it should not be prefixed with
?
and you can separate parameters as usual with the &
char.
Getting the Response Code
You can get the HTTP response code from the AHC component by getting the
value from the Out message header with Exchange.HTTP_RESPONSE_CODE
.
Exchange exchange = template.send("ahc:http://www.google.com/search", new Processor() {
public void process(Exchange exchange) throws Exception {
exchange.getIn().setHeader(Exchange.HTTP_QUERY, constant("hl=en&q=activemq"));
}
});
Message out = exchange.getOut();
int responseCode = out.getHeader(Exchange.HTTP_RESPONSE_CODE, Integer.class);
Configuring AsyncHttpClient
The AsyncHttpClient
client uses a AsyncHttpClientConfig
to configure
the client. See the documentation at
Async Http Client for
more details.
The example below shows how to use a builder to create the
AsyncHttpClientConfig
which we configure on the AhcComponent
.
The AsyncHttpClientConfigBean
class provides getters and setters for
the configuration options available in AsyncHttpClientConfig
. An
instance of AsyncHttpClientConfigBean
may be passed directly to the
AHC component or referenced in an endpoint URI using the clientConfig
URI parameter.
There is the ability to set configuration
options directly in the URI. URI parameters starting with
"clientConfig." can be used to set the various configurable properties
of AsyncHttpClientConfig
. The properties specified in the endpoint URI
are merged with those specified in the configuration referenced by the
"clientConfig" URI parameter with those being set using the
"clientConfig." parameter taking priority. The AsyncHttpClientConfig
instance referenced is always copied for each endpoint such that
settings on one endpoint will remain independent of settings on any
previously created endpoints. The example below shows how to configure
the AHC component using the "clientConfig." type URI parameters.
from("direct:start")
.to("ahc:http://localhost:8080/foo?clientConfig.maxRequestRetry=3&clientConfig.followRedirects=true")
SSL Support (HTTPS)
Using the JSSE Configuration Utility
The AHC component supports SSL/TLS configuration through the Camel JSSE Configuration Utility. This utility greatly decreases the amount of component specific code you need to write and is configurable at the endpoint and component levels. The following examples demonstrate how to use the utility with the AHC component.
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);
AhcComponent component = context.getComponent("ahc", AhcComponent.class);
component.setSslContextParameters(scp));
Spring DSL based configuration of 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="ahc:https://localhost/foo?sslContextParameters=#sslContextParameters"/>
...
Spring Boot Auto-Configuration
When using ahc with Spring Boot make sure to use the following Maven dependency to have support for auto configuration:
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-ahc-starter</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency>
The component supports 10 options, which are listed below.
Name | Description | Default | Type |
---|---|---|---|
camel.component.ahc.allow-java-serialized-object |
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. |
false |
Boolean |
camel.component.ahc.autowired-enabled |
Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. |
true |
Boolean |
camel.component.ahc.binding |
To use a custom AhcBinding which allows to control how to bind between AHC and Camel. The option is a org.apache.camel.component.ahc.AhcBinding type. |
AhcBinding |
|
camel.component.ahc.client |
To use a custom AsyncHttpClient. The option is a org.asynchttpclient.AsyncHttpClient type. |
AsyncHttpClient |
|
camel.component.ahc.client-config |
To configure the AsyncHttpClient to use a custom com.ning.http.client.AsyncHttpClientConfig instance. The option is a org.asynchttpclient.AsyncHttpClientConfig type. |
AsyncHttpClientConfig |
|
camel.component.ahc.enabled |
Whether to enable auto configuration of the ahc component. This is enabled by default. |
Boolean |
|
camel.component.ahc.header-filter-strategy |
To use a custom org.apache.camel.spi.HeaderFilterStrategy to filter header to and from Camel message. The option is a org.apache.camel.spi.HeaderFilterStrategy type. |
HeaderFilterStrategy |
|
camel.component.ahc.lazy-start-producer |
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. |
false |
Boolean |
camel.component.ahc.ssl-context-parameters |
Reference to a org.apache.camel.support.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. The option is a org.apache.camel.support.jsse.SSLContextParameters type. |
SSLContextParameters |
|
camel.component.ahc.use-global-ssl-context-parameters |
Enable usage of global SSL context parameters. |
false |
Boolean |