Michael Bryan

Michael Bryan

1554170665

Redis for Java Developers: Tutorial and Code Examples

Redis 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&lt;String&gt; 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&lt;String, Integer&gt; 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.

Originally published by Nikita Koksharov at https://dzone.com

Learn More

☞ Complete Java Masterclass

☞ Complete Step By Step Java For Testers

☞ Java Web Service Complete Guide - SOAP + REST + Buide App

☞ Selenium WebDriver with Java - Basics to Advanced& Interview

☞ Java Persistence: Hibernate and JPA Fundamentals

☞ Java Swing (GUI) Programming: From Beginner to Expert

☞ Java Basics: Learn to Code the Right Way

#java #redis

What is GEEK

Buddha Community

Redis for Java Developers: Tutorial and Code Examples
Tyrique  Littel

Tyrique Littel

1600135200

How to Install OpenJDK 11 on CentOS 8

What is OpenJDK?

OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE). It contains the virtual machine, the Java Class Library, and the Java compiler. The difference between the Oracle OpenJDK and Oracle JDK is that OpenJDK is a source code reference point for the open-source model. Simultaneously, the Oracle JDK is a continuation or advanced model of the OpenJDK, which is not open source and requires a license to use.

In this article, we will be installing OpenJDK on Centos 8.

#tutorials #alternatives #centos #centos 8 #configuration #dnf #frameworks #java #java development kit #java ee #java environment variables #java framework #java jdk #java jre #java platform #java sdk #java se #jdk #jre #open java development kit #open source #openjdk #openjdk 11 #openjdk 8 #openjdk runtime environment

Samanta  Moore

Samanta Moore

1621137960

Guidelines for Java Code Reviews

Get a jump-start on your next code review session with this list.

Having another pair of eyes scan your code is always useful and helps you spot mistakes before you break production. You need not be an expert to review someone’s code. Some experience with the programming language and a review checklist should help you get started. We’ve put together a list of things you should keep in mind when you’re reviewing Java code. Read on!

1. Follow Java Code Conventions

2. Replace Imperative Code With Lambdas and Streams

3. Beware of the NullPointerException

4. Directly Assigning References From Client Code to a Field

5. Handle Exceptions With Care

#java #code quality #java tutorial #code analysis #code reviews #code review tips #code analysis tools #java tutorial for beginners #java code review

Java Development Company in USA | Java Application Development Company

Large enterprises usually have unique requirements for their website and mobile app development and there can be no better technology to fulfil that requirement than Java Development. Java can add that unique element to your development and make it stand out among competitors.

Want to develop a unique website or mobile app with Java?

WebClues Infotech with its highly skilled team and updated with the latest technology is the way to go for your development needs as there can be no one better to work in Java. After successfully delivering 1500+ projects we are ready to serve you with our expertise in Web & Mobile App Development.

Want to know more about our Java Development Service?

Visit: https://www.webcluesinfotech.com/java-development/

Share your requirements https://www.webcluesinfotech.com/contact-us/

View Portfolio https://www.webcluesinfotech.com/portfolio/

#java development company in usa #java application development company #java development company #java application development #java software development company #hire java developers

Byte Cipher

1617709630

Hire Dedicated Java Developer and Offshore Java Developers

Do you want to hire dedicated Java Developer and offshore Java developers from a reputed software development company in India? You must have to visit ByteCipher Pvt Ltd, we have dedicated and skilled Java developers to provide you world class Java web application development services. For more information about our Java development services and to know how to hire Java developers in India, click here

#hire dedicated java developer #java web application development services #java web developer #hire java developers in india #offshore java developers

Java Application Development Services in United States

Are you looking for the best and most reliable Java application development services in USA? We at AppClues Infotech is one of the leading and most trusted Java app development services provider company that help to create successful and highly scalable applications for your specific business requirements.

If you have any types of Java app development projects then just share your requirement with us and get the perfect consultation for your project.

For more info:
Website: https://www.appcluesinfotech.com/
Email: info@appcluesinfotech.com
Call: +1-978-309-9910

#java application development services in united states #custom java development company in usa #custom java application development services in usa #hire java app developers in usa #hire java app developer in india/usa #top java app development company in usa