You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

There are two different ways to send messages to any Camel Endpoint from a POJO

@EndpointInject

To allow sending of messages from POJOs you can use @EndpointInject() annotation. This will inject either a ProducerTemplate or CamelTemplate so that the bean can send message exchanges.

e.g. lets send a message to the foo.bar queue in ActiveMQ at some point


public class Foo {
  @EndpointInject(uri="activemq:foo.bar")
  ProducerTemplate producer;

  public void doSomething() {
    if (whatever) {
      producer.sendBody("<hello>world!</hello>");
    }
  }
}

The downside of this is that your code is now dependent on a Camel API, the ProducerTemplate. We recommend Hiding Middleware APIs from your application code so the next option might be more suitable.

@Produce

You can add the @Produce annotation to an injection point (a field or property setter) using some interface you use in your business logic. e.g.

public interface MyListener {
    String sayHello(String name);
}

public class MyBean {
    @Produce(uri = "activemq:foo")
    protected MyListener producer;

    public void doSomething() {
        // lets send a message
        String response = producer.sayHello("James");
    }
}

Here we have an injected client side proxy, the MyListener instance. When we invoke methods on this interface the method call is turned into an object and using the Camel Spring Remoting mechanism it is sent to the endpoint - in this case the ActiveMQ endpoint to queue foo; then the caller blocks for a response.

If you want to make asynchronous message sends then use an @InOnly annotation on the injection point.

  • No labels