Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
from(uri).to("bean:myBean?method=methodName");

Using a property to define the endpoint

Available as of Camel 2.11

The following annotations @Consume, @Produce, @EndpointInject, now offers a property attribute you can use to define the endpoint as a property on the bean. Then Camel will use the getter method to access the property.

Info
titleThis applies for them all

The explanation below applies for all the three annotations, eg @Consume, @Produce, and @EndpointInject

For example

Code Block

public class MyService {
  private String serviceEndpoint;
  
  public void setServiceEndpoint(String uri) {
     this.serviceEndpoint = uri;
  }

  public String getServiceEndpoint() {
     return serviceEndpoint
  }

  @Consume(property = "serviceEndpoint")
  public void onService(String input) {
     ...
  }
}

The bean MyService has a property named serviceEndpoint which has getter/setter for the property. Now we want to use the bean for POJO Consuming, and hence why we use @Consume in the onService method. Notice how we use the property = "serviceEndpoint to configure the property that has the endpoint url.

If you define the bean in Spring XML or Blueprint, then you can configure the property as follows:

Code Block
xml
xml

<bean id="myService" class="com.foo.MyService">
  <property name="serviceEndpoint" value="activemq:queue:foo"/>
</bean>

This allows you to configure the bean using any standard IoC style.

Camel offers a naming convention which allows you to not have to explicit name the property.
Camel uses this algorithm to find the getter method. The method must be a getXXX method.

1. Use the property name if explicit given
2. If no property name was configured, then use the method name
3. Try to get the property with name*Endpoint* (eg with Endpoint as postfix)
4. Try to get the property with the name as is (eg no postfix or postfix)
5. If the property name starts with on then skip omit that, and try step 3 and 4 again.

So in the example above, we could have defined the @Consume annotation as

Code Block

  @Consume(property = "service")
  public void onService(String input) {

Now the property is named 'service' which then would match step 3 from the algorithm, and have Camel invoke the getServiceEndpoint method.

We could also have omitted the property attribute, to make it implicit

Code Block

  @Consume
  public void onService(String input) {

Now Camel match step 5, and loose the starting on in the name, and look for 'service' as the property. And because there is a getServiceEndpoint method, Camel will use that.

Which approach to use?

Using the @Consume annotations are simpler when you are creating a simple route with a single well defined input URI.

...