How to access Default Idempotent Repository map from java dsl? - apache-camel

I am newbie to Apache Camel. How can i access Camel Default Idempotent Repository map in Java DSL.
Route:
from("file://C:/folderA?noop=true")
.to("file://C:/folderB")
.end();
When i say noop=true in route, then idempotent will be true. Now i need to get Idempotent map in java dsl. Please tell me how to access this?
Thanks in advance.

If you want to access the underlying map, you should specify your own idempotentRepository bean. Using the existing MemoryIdempotentRepository should be pretty easy.
//Instantiate repository and get map
IdempotentRepository<String> repo = MemoryIdempotentRepository.memoryIdempotentRepository()
Map<String, Object> map = repo.getCache();
//Bind the repo to the Camel Context Registry using the id "repo"
//This changes depending upon how you are running Camel
//In your RouteBuilder...
from("file://C:/folderA?noop=true&idempotentRepository=#repo")
.to("file://C:/folderB")
.end();

Related

Apache camel - How to retrieve Endpoint URIs for a given Component

I use Apache Camel 2.20x.
A camel component can be developed with a uri scheme for example "sample-component". Now the Endpoint of this component can actually extend an existing Endpoint say SQL which has uri syntax as "sql:<select query">.
Now I am listening to camel Exchange event (ExchangeSentEvent) When I retrive the uri from the event I get "sql:<select query">. But what I want is to get "sample-component". How can we achieve that. In simple terms following is the ask
How can we get all Endpoint uri schemes for a camel component.
Thanks in advance
Gk
ComponentName+Endpoint . You can see all the uri that the component will take. All camel components are named this way.There may be exceptions. also if u use intellij idea apache camel plugin it show .
examples
HttpEndpoint
TimerEndpoint
SedaEndpoint
DirectEndpoint
You can also find the consumer and producer classes of these components as follows.
HtppProducer if support HttpConsumer ..

Rest API integration using Apache camel

I am new to Apache Camel, I have requirement to integrate two systems using REST API using Apache camel. I will receive a JSON message on my apache camel rest api endpoint(from source system).This json will contain arrays, I have to extract each array content and post to another external api end point (target). So initially I tried to send incoming message to camel rest api as it is to the target external api endpoint. When I try that, then on application startup I get error. I searched for similar exception but couldn't find anything concrete as in most of the example, source of message was used as a timer component.
Can't we make a call to external rest api end point?
Camel version : 3.4.0
Spring boot : 2.3.1
My router builder code
restConfiguration()
.component("servlet").port(9090).host("localhost")
.dataFormatProperty("prettyPrint", "true");
rest().post("/incoming")
.consumes(MediaType.APPLICATION_JSON_VALUE)
.produces(MediaType.APPLICATION_JSON_VALUE)
.route()
.to("https://webhook.site/ff4a6f68-3b20-4bb2-afa1-c15ccae515ef");
Exception I am getting for target external endpoint
org.apache.camel.NoSuchEndpointException:
No endpoint could be found for:
https://webhook.site/ff4a6f68-3b20-4bb2-afa1-c15ccae515ef,
please check your classpath contains the needed Camel component jar.
Please let me know, where I am making mistake.
Thanks in advance.
Ani
You don't have camel-http as dependency so add the dependency with correct version
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-http</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency>
Since you are already using camel 3.x you might also want to consider using dynamic endpoint component, in case your destination URL is defined dynamically. For example:
from("direct:login")
.toD("http:myloginserver:8080/login?userid=${header.userName}");

Difference between CamelContext and Registry in Camel

I'm new to Camel and bit confused between CamelContext and Registry.
As far as I know that CamelContext is the base object like ApplicationContext in Spring that is used for add routes and maintains the camel life cycle.
Also we got the Registry object from CamelContext but not sure what is the main purpose of this registry.
My intention is to add the component in the context/registry so that JNDIBind can look up the components.
CamelContext: Kind of Camel Runtime that keeps everything in Camel together, e.g.: Endpoints, TypeConverter, Routes, Components and Registry(!).
Registry: allows you to lookup beans, which by default will be JNDI beans. If you use the spring integration it will be Spring's ApplicationContext.
Generally camel when used with spring makes use of the ApplicationContextRegistry to lookup components, endpoints etc with the name of the bean defined in spring-bean.xml file. In places where we need to use JNDIRegistry we would have to add that registry when creating the CamelContext. This is used in places where JNDI objects are shared accross multiple JVMs where JNDI is the best solution.
Please see different types of registry implementation for camel: camel registries

Apache camel Quartz endpoints : rescheduling at runtime

Is it possible to change cron expression of camel quartzendpoint at runtime using jmx or so ?
You can update the route itself (including configuration of the quartz endpoint). As an example you can see how Camel plugin of Hawt.io (http://hawt.io/plugins/camel/) does it
No, but you could experiment with CamelContext and see if you can add new routes at runtime.

Using Apache Camel ProducerTemplate to send JSON message to ActiveMQ

I have a camel route setup like the following to send a java object to an activemq queue.
from("direct:clientRequest")
.marshal().json(JsonLibrary.Jackson)
.to("activemq:queue:command");
I want to do the following:
Map the "clientRequest" uri to some Java method
Use ProducerTemplate's "sendBody" method to send a JSON form of a Java object to the activemq queue.
Is this possible?
I am asking this question after a lot of homework. Please suggest a way to do this.
You can use pojo producing in Apache Camel. See this page for more details
http://camel.apache.org/pojo-producing.html
And there is an example that shows more about using pojo producing/consuming in Camel
http://camel.apache.org/pojo-messaging-example.html

Resources