AHCPage edited by Claus IbsenChanges (1)
Full ContentAsync Http Client (AHC) ComponentAvailable as of Camel 2.8 The ahc: component provides HTTP based endpoints for consuming external HTTP resources (as a client to call external servers using HTTP). 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>
AhcOperationFailedExceptionThis exception contains the following information:
Calling using GET or POSTThe following algorithm is used to determine if either GET or POST HTTP method should be used: Configuring URI to callYou 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 ParametersThe 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 (GET/POST/PUT/DELETE/HEAD/OPTIONS/TRACE) to the HTTP producerThe 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 headerName="CamelHttpMethod"> <constant>POST</constant> </setHeader> <to uri="ahc:http://www.google.com"/> <to uri="mock:results"/> </route> </camelContext> Configuring charsetIf 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 URIIn 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 MessageMap 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 CodeYou 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 AsyncHttpClientThe AsyncHttpClient client uses a AsyncHttpClientConfig to configure the client. See the documentation at The example below shows how to use a builder to create the AsyncHttpClientConfig which we configure on the AhcComponent. // create a client config builder AsyncHttpClientConfig.Builder builder = new AsyncHttpClientConfig.Builder(); // use the builder to set the options we want, in this case we want to follow redirects and try // at most 3 retries to send a request to the host AsyncHttpClientConfig config = builder.setFollowRedirects(true).setMaxRequestRetry(3).build(); // lookup AhcComponent AhcComponent component = context.getComponent("ahc", AhcComponent.class); // and set our custom client config to be used component.setClientConfig(config); The AsyncHttpClientConfig doesn't support getters/setters so its not easy to create/configure using a Spring bean style (eg the <bean> tag in the XML file). See Also
Change Notification Preferences
View Online
|
View Changes
|
Add Comment
|
- [CONF] Apache Camel > AHC confluence
- [CONF] Apache Camel > AHC confluence
- [CONF] Apache Camel > AHC confluence