Redis for Java Developers: Tutorial and Code Examples
This article will go over a few common use cases of Redisson.
Join the DZone community and get the full member experience.
Join For FreeRedis is one of the most popular NoSQL database solutions, and Java is one of the world's most popular programming languages. Although it seems natural for the two to work together, Redis doesn't come with native support for Java.
Instead, Java developers who want to integrate with Redis will need to use a Java client library. Redisson is a Redis-based in-memory data grid for Java that makes it easy for Java developers to work with Redis. Redisson provides implementations of many Java data structures to be distributed and scalable so that they can run on top of the Redis server.
This article will go over a few common use cases of Redisson so that you can see how easy it is to get started writing Java code for Redis.
How to Install Redisson
The easiest way to install Redisson is by adding it as a Maven or Gradle dependency:
Maven
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.10.5</version>
</dependency>
Gradle
compile 'org.redisson:redisson:3.10.5'
You can find the latest version number of Redisson by searching the Maven central repository.
How to Compile and Run Redisson
Once Redisson has been installed, compiling and running Redisson code just requires using the Java compiler:
javac RedissonExample.java
java RedissonExample
Java Lists in Redis
The code below is a simple demonstration of how to use the RList object in Redisson. RList is a distributed and concurrent implementation of Java's List collection.
import org.redisson.Redisson;
import org.redisson.api.RList;
import org.redisson.api.RedissonClient;
public class ListExamples {
public static void main(String[] args) {
// connects to 127.0.0.1:6379 by default
RedissonClient redisson = Redisson.create();
// implements java.util.List
RList<String> list = redisson.getList("myList");
list.add("1");
list.add("2");
list.add("3");
boolean contains = list.contains("1");
System.out.println("List size: " + list.size());
System.out.println("Is list contains value '1': " + contains);
for (String element : list) {
System.out.println("List element: " + element);
}
redisson.shutdown();
}
}
When you run the code above, you should get the following output:
List size: 3
Does list contain value '1': true
List element: 1
List element: 2
List element: 3
Java Maps in Redis
Redisson also includes RMap, a distributed and concurrent implementation of the Java Map collection:
import java.io.IOException;
import org.redisson.Redisson;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
public class MapExamples {
public static void main(String[] args) throws IOException {
// connects to 127.0.0.1:6379 by default
RedissonClient redisson = Redisson.create();
// implements java.util.concurrent.ConcurrentMap
RMap<String, Integer> map = redisson.getMap("myMap");
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
boolean contains = map.containsKey("a");
System.out.println("Map size: " + map.size());
System.out.println("Is map contains key 'a': " + contains);
Integer value = map.get("c");
System.out.println("Value mapped by key 'c': " + value);
boolean added = map.putIfAbsent("c", 4) == null;
System.out.println("Is value mapped by key 'c' added: " + added);
redisson.shutdown();
}
}
When you run the code above, you will see the following output:
Map size: 3
Map contains key 'a': true
Value mapped by key 'c': 3
Value mapped by key 'c' added: false
Java Locks in Redis
The code below demonstrates the usage of RLock, a distributed implementation of the reentrant lock in Java:
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
public class LockExamples {
public static void main(String[] args) throws InterruptedException {
// connects to 127.0.0.1:6379 by default
RedissonClient redisson = Redisson.create();
// implements java.util.concurrent.locks.Lock
RLock lock = redisson.getLock("lock");
lock.lock();
System.out.println("lock aquired");
Thread t = new Thread() {
public void run() {
RLock lock1 = redisson.getLock("lock");
lock1.lock();
System.out.println("lock aquired by thread");
lock1.unlock();
System.out.println("lock released by thread");
};
};
t.start();
t.join(1000);
lock.unlock();
System.out.println("lock released");
t.join();
redisson.shutdown();
}
}
This code will produce the following output:
lock aquired
lock released
lock aquired by thread
lock released by thread
Java AtomicLongs in Redis
Finally, this sample code demonstrates the usage of RAtomicLong, a distributed alternative to the AtomicLong class in Java for holding long values in a concurrent environment.
import org.redisson.Redisson;
import org.redisson.api.RAtomicLong;
import org.redisson.api.RedissonClient;
public class AtomicLongExamples {
public static void main(String[] args) {
// connects to 127.0.0.1:6379 by default
RedissonClient redisson = Redisson.create();
RAtomicLong atomicLong = redisson.getAtomicLong("myLong");
System.out.println("Init value: " + atomicLong.get());
atomicLong.incrementAndGet();
System.out.println("Current value: " + atomicLong.get());
atomicLong.addAndGet(10L);
System.out.println("Final value: " + atomicLong.get());
redisson.shutdown();
}
}
The output of this code will be:
Init value: 0
Current value: 1
Final value: 11
What are your thoughts? Let me know in the comments.
Opinions expressed by DZone contributors are their own.
Comments