Use the method as a producer on a camel route

I have a method that generates a string from time to time. I would like to register the method as uri and create an exchange method that will be used as input for the route.

This method is a call to another class.

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

For instance:

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

The route is defined as follows:

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

But the route does not call the GenerateD class when I create the sampleMethod method. Is this impossible or am I doing something wrong?

+5
source share
3 answers

Finally, this is what worked for my use.

Running a camel as shown 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(); } } } 

Then I create an interface that has a sendMessage method.

 public interface DDTConsumer { public String sendMessage(Object object); } 

Now I implement this method to create the endpoint of this interface and send the 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 now the route is working fine. Hope this helps others as well.

+1
source

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

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

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

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

And he should send a message to the direct:consumerMethod route.

0
source

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 generateDMethod of the generateD (bean) object and register the methods that are printed to the console (the default log). To do the work on the expression, you must save the generateD bean in the registry, which will be associated with your CamelContext application. You can do the same as below.

 @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 a bean to the camelContext. Please check my answer on this link to have a complete example.

0
source

Source: https://habr.com/ru/post/1271003/


All Articles