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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Advanced Brain-Computer Interfaces With Java
  • Simplify Java: Reducing Unnecessary Layers and Interfaces [Video]
  • Java 21 SequenceCollection: Unleash the Power of Ordered Collections
  • Projections/DTOs in Spring Data R2DBC

Trending

  • A Complete Guide to Modern AI Developer Tools
  • Medallion Architecture: Why You Need It and How To Implement It With ClickHouse
  • Top Book Picks for Site Reliability Engineers
  • Start Coding With Google Cloud Workstations
  1. DZone
  2. Coding
  3. Java
  4. RPC With Redis-Based Java Remote Services

RPC With Redis-Based Java Remote Services

Learn more about remote procedure call with Redis-based Java remote services.

By 
Nikita Koksharov user avatar
Nikita Koksharov
·
Updated Jul. 01, 19 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
21.1K Views

Join the DZone community and get the full member experience.

Join For Free

What Is RPC?

RPC (Remote Procedure Call) is a technique in distributed computing that involves making a client request in the form of a procedure call to a program running on a remote server.

RPC allows clients and servers to communicate without having to worry about the details of the network or the remote interaction. This allows developers to focus on building software, independent of concerns such as hardware, operating systems, and protocols. RPC is an alternative to other client-server architectures such as REST (representational state transfer).

Java Remote Services With Redis and Redisson

Redis is an in-memory data store that is one of the most popular solutions for implementing a NoSQL database. The Redisson client is a library for Redis that provides access to many common objects and constructions in the Java programming language.

In particular, Redisson allows developers to use Java remote services in order to execute remote procedure calls. The remote interface may have any type of method parameters and any type of return object, providing a true RPC implementation. Redisson stores the method request and the result of the execution inside a Redis database.

The relevant Redisson interface is RRemoteService. There are two types of RRemoteService instances: server-side and client-side.

The server-side instance is used to register Java Remote Service by its interface before it is invoked. Example code for SomeServiceInterface remote interface is below:

RRemoteService remoteService = redisson.getRemoteService();
SomeServiceImpl someServiceImpl = new SomeServiceImpl();
// register remote service before any remote invocation
// can handle only 1 invocation concurrently
remoteService.register(SomeServiceInterface.class, someServiceImpl);
// register remote service able to handle up to 12 invocations concurrently
remoteService.register(SomeServiceInterface.class, someServiceImpl, 12);

Note that the default setting is to handle only 1 concurrent invocation.

The client-side instance is used to invoke a remote method once the object has been registered. Example code is below:

RRemoteService remoteService = redisson.getRemoteService();
SomeServiceInterface service = remoteService.get(SomeServiceInterface.class);
String result = service.doSomeStuff(1L, "secondParam", new AnyParam());

The server-side and client-side instances must use the same remote interface and must be backed by Redisson instances that were created using the same configuration for server connections.

Redisson does not impose any limits as to the number of client and/or server instances.

APIs for RPC With Java Remote Services

You have three API options for performing RPC with Redis and Redisson:

  • Asynchronous

  • Reactive

  • RxJava2

The interface must match the methods in the remote interface. For example, if we have the following remote interface:

public interface RemoteInterface {

    Long someMethod1(Long param1, String param2);

    void someMethod2(MyObject param);

    MyObject someMethod3();

}

The asynchronous interface must be annotated with @RRemoteAsync and returns an object of the form org.redisson.api.RFuture and must have the following form:

@RRemoteAsync(RemoteInterface.class)
public interface RemoteInterfaceAsync {

      RFuture someMethod1(Long param1, String param2);

      RFuture someMethod2(MyObject param);

}

RedissonClient redisson = Redisson.create(config);
RRemoteService remoteService = redisson.getRemoteService();
RemoteInterfaceAsync asyncService = remoteService.get(RemoteInterfaceAsync.class);

asyncService.someMethod1(1L, "myparam");

The reactive interface must be annotated with @RRemoteReactive and returns an object of the form reactor.core.publisher.Mono and must have the following form:

@RRemoteReactive(RemoteInterface.class)
public interface RemoteInterfaceReactive {

     Mono someMethod1(Long param1, String param2);

     Mono someMethod2(MyObject param);
}

RedissonReactiveClient redisson = Redisson.createReactive(config);
RRemoteService remoteService = redisson.getRemoteService();
RemoteInterfaceReactive asyncService = remoteService.get(RemoteInterfaceReactive.class);

asyncService.someMethod1(1L, "myparam");

The RxJava2 interface must be annotated with @RRemoteRx and returns an object of one of three forms: io.reactivex.Completable, io.reactivex.Single, or io.reactivex.Maybe and must have the following form:

@RRemoteRx(RemoteInterface.class)
public interface RemoteInterfaceRx {

    Single someMethod1(Long param1, String param2);

    Completable someMethod2(MyObject param);
}

RedissonRxClient redisson = Redisson.createRx(config);
RRemoteService remoteService = redisson.getRemoteService();
RemoteInterfaceReactive asyncService = remoteService.get(RemoteInterfaceRx.class);

asyncService.someMethod1(1L, "myparam");

Let me know your thoughts in the comments. 

remote Java (programming language) Interface (computing)

Opinions expressed by DZone contributors are their own.

Related

  • Advanced Brain-Computer Interfaces With Java
  • Simplify Java: Reducing Unnecessary Layers and Interfaces [Video]
  • Java 21 SequenceCollection: Unleash the Power of Ordered Collections
  • Projections/DTOs in Spring Data R2DBC

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • 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:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!