Search code examples
javajsonapache-camelamqp

Apache Camel: output route message to AMQP


I am a novice to Apache Camel and is looking for a way to direct output of a route to an AMQP.

I could write a routine to send to AMQP myself, but I am looking for advice how it be done using Camel .to() when declaring a route? It is difficult to find a complete example online.

what I need to do:

  1. configure an amqp route to send output (server credentials etc.)
  2. Make sure that route is accessible as I want to handle miscofiguration.
  3. I need to send a JSON. Do I have to marshal my POJO to json as string, or can I use some autoconversion?

All that I would like to do without involving XML configurations, but based on annotations or code.


Solution

  • Here is an example of how to do it:

        from("direct://PublishToRabbitMQ")
    
            .setHeader("rabbitmq.ROUTING_KEY", constant("SOMEROUTINGKEY"))
            .setHeader("rabbitmq.EXCHANGE_NAME",constant("EXCHANGE_NAME"))          
            .setHeader("timestamp", constant(new Date(System.currentTimeMillis())))
            .doTry()
            .to("rabbitmq://localhost/EXCHANGE_NAME/?exchangeType=Topic&autoDelete=false&durable=false")
            .convertBodyTo(String.class)    
    
        .end();
    

    Add or remove parameters to suit your need.