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
Refcards
Trend Reports

Events

View Events Video Library

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

  • Observability in Spring Boot 4
  • The Serverless Illusion: When “Pay for What You Use” Becomes Expensive
  • Detecting Advanced Persistent Threats Using Behavioral Analytics and Log Correlation
  • Unlocking Smart Meter Insights with Smart Datastream
  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.4K 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

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

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook