Red Hat Training

A Red Hat training course is available for Red Hat Fuse

5.3. Message Endpoint

Overview

A message endpoint is the interface between an application and a messaging system. As shown in Figure 5.3, “Message Endpoint Pattern”, you can have a sender endpoint, sometimes called a proxy or a service consumer, which is responsible for sending In messages, and a receiver endpoint, sometimes called an endpoint or a service, which is responsible for receiving In messages.

Figure 5.3. Message Endpoint Pattern

Message endpoint pattern

Types of endpoint

Apache Camel defines two basic types of endpoint:
  • Consumer endpoint — Appears at the start of a Apache Camel route and reads In messages from an incoming channel (equivalent to a receiver endpoint).
  • Producer endpoint — Appears at the end of a Apache Camel route and writes In messages to an outgoing channel (equivalent to a sender endpoint). It is possible to define a route with multiple producer endpoints.

Endpoint URIs

In Apache Camel, an endpoint is represented by an endpoint URI, which typically encapsulates the following kinds of data:
  • Endpoint URI for a consumer endpoint — Advertises a specific location (for example, to expose a service to which senders can connect). Alternatively, the URI can specify a message source, such as a message queue. The endpoint URI can include settings to configure the endpoint.
  • Endpoint URI for a producer endpoint — Contains details of where to send messages and includes the settings to configure the endpoint. In some cases, the URI specifies the location of a remote receiver endpoint; in other cases, the destination can have an abstract form, such as a queue name.
An endpoint URI in Apache Camel has the following general form:
ComponentPrefix:ComponentSpecificURI
Where ComponentPrefix is a URI prefix that identifies a particular Apache Camel component (see "Apache Camel Component Reference" for details of all the supported components). The remaining part of the URI, ComponentSpecificURI, has a syntax defined by the particular component. For example, to connect to the JMS queue, Foo.Bar, you can define an endpoint URI like the following:
jms:Foo.Bar
To define a route that connects the consumer endpoint, file://local/router/messages/foo, directly to the producer endpoint, jms:Foo.Bar, you can use the following Java DSL fragment:
from("file://local/router/messages/foo").to("jms:Foo.Bar");
Alternatively, you can define the same route in XML, as follows:
<camelContext id="CamelContextID" xmlns="http://camel.apache.org/schema/spring">
  <route>
    <from uri="file://local/router/messages/foo"/>
    <to uri="jms:Foo.Bar"/>
  </route>
</camelContext>

Dynamic To

The <toD> parameter allows you to send a message to a dynamically computed endpoint using one or more expressions that are concatenated together.
By default, the Simple language is used to compute the endpoint. The following example sends a message to an endpoint defined by a header:
<route>
  <from uri="direct:start"/>
  <toD uri="${header.foo}"/>
</route>
In Java DSL the format for the same command is:
from("direct:start")
  .toD("${header.foo}");
The URI can also be prefixed with a literal, as shown in the following example:
<route>
  <from uri="direct:start"/>
  <toD uri="mock:${header.foo}"/>
</route>
In Java DSL the format for the same command is:
from("direct:start")
  .toD("mock:${header.foo}");
In the example above, if the value of header.foo is orange, the URI will resolve as mock:orange.
To use a language other than Simple, you need to define the language: parameter. See Part II, “Routing Expression and Predicate Languages”.
The format for using a different language is to use language:languagename: in the URI. For example, to use Xpath use the following format:
<route>
  <from uri="direct:start"/>
  <toD uri="language:xpath:/order/@uri/">
</route>
Here is the same example in Java DSL:
from("direct:start")
  .toD("language:xpath:/order/@uri");
If you do not specify language: then the endpoint is a component name. In some cases a component and a language have the same name, such as xquery.
You can concatenate multiple languages using a + sign. In the example below, the URI is a combination of Simple and Xpath languages. Simple is the default so the language does not have to be defined. After the + sign is the Xpath instruction, indicated by language:xpath.
<route>
  <from uri="direct:start"/>
  <toD uri="jms:${header.base}+language:xpath:/order/@id"/>
</route>
In Java DSL the format is as follows:
from("direct:start")
  .toD("jms:${header.base}+language:xpath:/order/@id");
Many languages can be concatenated at one time, just separate each with a + and specify each language with language:languagename.
The following options are available with toD:
Name Default Value Description
uri Mandatory: The URI to use.
pattern Set a specific Exchange Pattern to use when sending to the endpoint. The original MEP is restored afterwards.
cacheSize Configure the cache size of the ProducerCache, which caches producers for reuse. The default cache size is 1000, which will be used if no other value is specified. Setting the value to -1 turns off the cache completely.
ignoreInvalidEndpoint false Specifies whether to ignore an endpoint URI that could not be resolved. If disabled, Camel will throw an exception identifying the invalid endpoint URI.