Amarendra Reddy
Amarendra Reddy

Reputation: 243

Use a method as producer in camel route

I have method which every now and then generates a string. I would like to register method as uri and produce a exchange method which will be used as input for a route.

The method is call by a different class

SampleClass sc = new SampleClass();
sc.sampleMethod("Hello");

Eg:

public class SampleClass{
    @Produce(uri = "direct:consumerMethod")
    ProducerTemplate producer;
    public sampleMethod(Object obj){
          producer.sendBody(object);
    }
}

The route is defined as below:

@Override
    public void configure() {
        from("direct:consumerMethod").process(new GenerateD());
    }

But the route doesnt call GenerateD class when i produce using the sampleMethod. Is this not feasible or am i doing something wrong?

Upvotes: 5

Views: 2297

Answers (3)

Amarendra Reddy
Amarendra Reddy

Reputation: 243

Finally this is what worked for my use case.

Starting camelcontext as below:

CamelContext camelContext = new DefaultCamelContext();
camelContext.addRoutes(new SampleRoute());
camelContext.start();

My routebuilder class :

    class SampleRoute extends RouteBuilder {

    @Override
    public void configure() {
        try
        {
            from("direct:consumerMethod").process(new DDT());
        }catch(Exception e)
        {
            e.printStackTrace();
        }

    }
}

I then create a interface which has a sendMessage method.

public interface DDTConsumer {

    public String sendMessage(Object object);

}

Now i implement this method to create an endpoint of this interface and send a message to the endpoint.

DDTConsumer ddt;
try {
    ddt = new ProxyBuilder(camelContext).endpoint("direct:consumerMethod").build(DDTConsumer.class);
    ddt.sendMessage(msg.getValue());
    } catch (Exception e) {
        e.printStackTrace();
    }

This solved my problem and the route is working fine now. Hope it helps others as well.

Upvotes: 1

sakura
sakura

Reputation: 2279

Use something like this, if you want to call somemethod

@Override
public void configure() {
    from("direct:consumerMethod").log(simple("${bean:generateD?method=generateDMethod}"));
}

The above expression will call the generateDMethod of generateD object (bean) and log the methods output to console (the default log writer). To make above expression work, you have to store generateD bean in the Registry, which will be further associated with your application's CamelContext. You can do the same as follows

@Autowired
private GenerateD generateD;

@Override
protected CamelContext createCamelContext() throws Exception {
    SimpleRegistry registry = new SimpleRegistry(); 
    registry.put("generateD", generateD); //the generateD bean,which can be used anywhere in the camelcontext
    SpringCamelContext camelContext = new SpringCamelContext();
    camelContext.setRegistry(registry); //add the registry
    camelContext.setApplicationContext(getApplicationContext());
    camelContext.start();
    return camelContext;
}

This adds the bean to camelContext. Please check my answer at this link to have complete example.

Upvotes: 0

mgyongyosi
mgyongyosi

Reputation: 2667

In your class where you have the sampleMethod(Object) add the following field:

@Produce(uri = "direct:consumerMethod")
ProducerTemplate template;

In your sampleMethod(Object) you can use the previously added template like this:

public sampleMethod(Object obj){
      template.sendBody(object);
}

And it should send a Message to the direct:consumerMethod route.

Upvotes: 0

Related Questions