DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Microservices With Apache Camel and Quarkus (Part 5)
  • Reactive Event Streaming Architecture With Kafka, Redis Streams, Spring Boot, and HTTP Server-Sent Events (SSE)
  • Can Redis Be Used as a Relational Database?
  • Microservices With Apache Camel and Quarkus (Part 3)

Trending

  • LLMs for Bad Content Detection: Pros and Cons
  • Choreography Pattern: Optimizing Communication in Distributed Systems
  • How To Create a Resource Chart in JavaScript
  • The Winds of Change: How Generative AI is Revolutionizing Cybersecurity
  1. DZone
  2. Coding
  3. Frameworks
  4. Apache Camel Meets Redis

Apache Camel Meets Redis

Bilgin Ibryam user avatar by
Bilgin Ibryam
·
Feb. 20, 13 · Interview
Like (0)
Save
Tweet
Share
9.48K Views

Join the DZone community and get the full member experience.

Join For Free
The Lamborghini of Key-Value stores

Camel is the best of bread Integration framework and in this post I'm going to show you how to make it even more powerful by leveraging another great project - Redis.  Camel 2.11 is on its way to be released soon with lots of new features, bug fixes and components. Couple of these new components are authored by me, redis-component being my favourite one. Redis - a ligth key/value store is an amazing piece of Italian software designed for speed (same as Lamborghini - a two-seater Italian car designed for speed). Written in C and having an in-memory closer to the metal nature, Redis performs extremely well (Lamborgini's motto is "Closer to the Road"). Redis is often referred to as a data structure server since keys can contain strings, hashes, lists and sorted sets. A fast and light data structure server is like a super sportscars for software engineers - it just flies. If you want to find out more about Redis' and Lamborghini's unique performance characteristics google around and you will see for yourself.
Getting started with Redis is easy: download, make, and start a redis-server. After these steps, you ready to use it from your Camel application. The component uses internally Spring Data which in turn uses Jedis driver, but with possibility to switch to other Redis drivers. Here are few use cases where the camel-redis component is a good fit:

Idempotent Repository
The term idempotent is used in mathematics to describe a function that produces the same result if it is applied to itself. In Messaging this concepts translates into the a message that has the same effect whether it is received once or multiple times. In Camel this pattern is implemented using the IdempotentConsumer class which uses an Expression to calculate a unique message ID string for a given message exchange; this ID can then be looked up in the IdempotentRepository to see if it has been seen before; if it has the message is consumed; if its not then the message is processed and the ID is added to the repository. RedisIdempotentRepository is using a set structure to store and check for existing Ids.
<bean id="idempotentRepository" class="org.apache.camel.component.redis.processor.idempotent.RedisIdempotentRepository">
  <constructor-arg value="test-repo"/>
</bean>

<route>
  <from uri="direct:start"/>
    <idempotentConsumer messageIdRepositoryRef="idempotentRepository">
      <simple>${in.body.id}</simple>
      <to uri="mock:result"/>
    </idempotentConsumer>
</route>

Caching
One of the main uses of Redis is as LRU cache. It can store data inmemory as Memcached or can be tuned to be durable flushing data to a log file that can be replayed if the node restarts.The various policies when maxmemory is reached allows creating caches for specific needs:
  • volatile-lru remove a key among the ones with an expire set, trying to remove keys not recently used.
  • volatile-ttl remove a key among the ones with an expire set, trying to remove keys with short remaining time to live.
  • volatile-random remove a random key among the ones with an expire set.
  • allkeys-lru like volatile-lru, but will remove every kind of key, both normal keys or keys with an expire set.
  • allkeys-random like volatile-random, but will remove every kind of keys, both normal keys and keys with an expire set.
Once your Redis server is configured with the right policies and running, the operation you need to do are SET and GET:
<?xml version="1.0" encoding="UTF-8"?>
<route>
    <from uri="direct:start"/>
        <setHeader headerName="CamelRedis.Command">
            <constant>SET</constant>
        </setHeader>
        <setHeader headerName="CamelRedis.Key">
            <constant>keyOne</constant>
        </setHeader>
        <setHeader headerName="CamelRedis.Value">
            <constant>valueOne</constant>
        </setHeader>
    <to uri="redis://localhost:6379"/>
</route>

Interap pub/sub with Redis
Camel has various components for interacting between routes:
direct: provides direct, synchronous invocation in the same camel context.
seda: asynchronous behavior, where messages are exchanged on a BlockingQueue, again in the same camel context.
vm: asynchronous behavior like seda, but also supports communication across CamelContext as long as they are in the same JVM.
Complex applications usually consist of more than one standalone Camel instances running on separate machines. For this kind of scenarios, Camel provides jms, activemq, combination of AWS SNS with SQS, for messaging between instances.
Redis has a simpler solution for the Publish/Subscribe messaging paradigm. Subscribers subscribes to one or more channels, by specifying the channel names or using pattern matching for receiving messages from multiple channels. Then the publisher publishes the messages to a channel, and Redis makes sure it reaches all the matching subscribers.
<?xml version="1.0" encoding="UTF-8"?>
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
    <route startupOrder="1">
        <from uri="redis://localhost:6379?command=SUBSCRIBE&channels=testChannel"/>
        <to uri="mock:result"/>
    </route>

    <route startupOrder="2">
        <from uri="direct:start"/>
        <setHeader headerName="CamelRedis.Command">
            <constant>PUBLISH</constant>
        </setHeader>
        <setHeader headerName="CamelRedis.CHANNEL">
            <constant>testChannel</constant>
        </setHeader>
        <setHeader headerName="CamelRedis.MESSAGE">
            <constant>Test Message</constant>
        </setHeader>
        <to uri="redis://localhost:6379"/>
    </route>
</camelContext>

Other usages
Guaranteed Delivery: Camel supports this EIP using JMS, File, JPA and few other components. Here Redis can be used as lightweight key-value persistent store with its transaction support.
The Claim Check from the EIP patterns allows you to replace message content with a claim check (a unique key), which can be used to retrieve the message content at a later time. The message content can be stored temporarily in Redis.
Redis is also very popular for implementing counters, leaderboards, tagging systems and many more functionalities. Now, with two swiss army knives under your belt, the integrations to make are limited only by your imagination.

 

Redis (company) Apache Camel

Published at DZone with permission of Bilgin Ibryam, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Microservices With Apache Camel and Quarkus (Part 5)
  • Reactive Event Streaming Architecture With Kafka, Redis Streams, Spring Boot, and HTTP Server-Sent Events (SSE)
  • Can Redis Be Used as a Relational Database?
  • Microservices With Apache Camel and Quarkus (Part 3)

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: