Skip to content

Latest commit

 

History

History
208 lines (150 loc) · 7.89 KB

File metadata and controls

208 lines (150 loc) · 7.89 KB
title Quickstart: Use Azure Managed Redis in Java
description In this quickstart, you create a new Java app that uses Azure Managed Redis
author KarlErickson
ms.author karler
ms.reviewer zhihaoguo
ms.date 05/18/2025
ms.topic quickstart
ms.custom
devx-track-java
devx-track-javaee
mode-api
mvc
devx-track-extended-java
ignite-2024
build-2025
ms.devlang java

Quickstart: Use Azure Managed Redis in Java with Jedis Redis client

In this quickstart, you incorporate Azure Managed Redis into a Java app using the Jedis Redis client. Your cache is a secure, dedicated cache that is accessible from any application within Azure.

Skip to the code on GitHub

Clone the repo Java quickstart on GitHub.

Prerequisites

Create an Azure Managed Redis instance

[!INCLUDE managed-redis-create]

Set up the working environment

The following steps show you how to set up the working environment for the Java app.

export REDIS_CACHE_HOSTNAME=<your-host-name>.redis.cache.windows.net
export REDIS_CACHE_PORT=10000

Replace the placeholders with the following values:

  • <your-host-name>: The DNS host name. To get the host name and ports for your cache, select Overview from the Resource menu. The host name is of the form <DNS name>.redis.cache.windows.net.

    :::image type="content" source="includes/media/redis-cache-access-keys/redis-cache-hostname-ports.png" alt-text="Screenshot showing Azure Cache for Redis properties.":::

  • <your-client-id>: The application (client) ID of your Azure AD application registration.

  • <your-client-secret>: The client secret of your Azure AD application registration.

  • <your-tenant-id>: Your Azure Active Directory tenant ID.

    [!NOTE] The above example uses client secret authentication. You can also configure the redis-authx-entraid library to use other authentication methods such as managed identity or client certificate by modifying the EntraIDTokenAuthConfigBuilder configuration in your code.

Create a new Java app

  1. Use maven to generate a new quickstart app:

    mvn archetype:generate \
        -DarchetypeGroupId=org.apache.maven.archetypes \
        -DarchetypeArtifactId=maven-archetype-quickstart \
        -DarchetypeVersion=1.3 \
        -DinteractiveMode=false \
        -DgroupId=example.demo \
        -DartifactId=redis-jedis-test \
        -Dversion=1.0
  2. Change to the new redis-jedis-test project directory.

  3. Open the pom.xml file. In the file, you see a dependency for Jedis:

    [!NOTE] Microsoft has entered into a partnership with Redis, Inc. As part of this collaboration, Microsoft Entra ID authentication support has been moved from Azure SDK to Redis Entra ID extensions. The new redis-authx-entraid library provides enhanced authentication capabilities and is the recommended approach for Microsoft Entra ID authentication with Azure Managed Redis.

    <dependency>
        <groupId>redis.clients.authentication</groupId>
        <artifactId>redis-authx-entraid</artifactId>
        <version>0.1.1-beta2</version>
    </dependency>
    
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>6.0.0</version> 
    </dependency>
  4. Close the pom.xml file.

  5. Open App.java and see the code with the following code:

    package example.demo;
    
    import com.azure.identity.DefaultAzureCredentialBuilder;
    import redis.clients.authentication.core.TokenAuthConfig;
    import redis.clients.authentication.entraid.AzureTokenAuthConfigBuilder;
    import redis.clients.jedis.DefaultJedisClientConfig;
    import redis.clients.jedis.HostAndPort;
    import redis.clients.jedis.UnifiedJedis;
    import redis.clients.jedis.authentication.AuthXManager;
    
    import java.util.Set;
    
    /**
    * Redis test with Microsoft Entra ID authentication using redis-authx-entraid
    * For more information about Redis authentication extensions, see:
    * https://redis.io/docs/latest/develop/clients/jedis/amr/
    *
    */
    public class App
    {
        public static void main( String[] args )
        {
            String REDIS_CACHE_HOSTNAME = System.getenv("REDIS_CACHE_HOSTNAME");
            int REDIS_PORT = Integer.parseInt(System.getenv().getOrDefault("REDIS_CACHE_PORT", "10000"));
            String SCOPES = "https://redis.azure.com/.default"; // The scope for Azure Managed Redis
    
            // Build TokenAuthConfig for Microsoft Entra ID authentication
            TokenAuthConfig tokenAuthConfig = AzureTokenAuthConfigBuilder.builder()
                    .defaultAzureCredential(new DefaultAzureCredentialBuilder().build())
                    .scopes(Set.of(SCOPES))
                    .tokenRequestExecTimeoutInMs(2000)
                    .build();
    
            DefaultJedisClientConfig config = DefaultJedisClientConfig.builder()
                    .authXManager(new AuthXManager(tokenAuthConfig))
                    .ssl(true)
                    .build();
    
            UnifiedJedis jedis = new UnifiedJedis(
                    new HostAndPort(REDIS_CACHE_HOSTNAME, REDIS_PORT),
                    config);
    
            // Test the connection
            System.out.println(String.format("Database size is %d", jedis.dbSize()));
    
            // Simple PING command
            System.out.println( "\nCache Command  : Ping" );
            System.out.println( "Cache Response : " + jedis.ping());
    
            // Simple get and put of integral data types into the cache
            System.out.println( "\nCache Command  : GET Message" );
            System.out.println( "Cache Response : " + jedis.get("Message"));
    
            System.out.println( "\nCache Command  : SET Message" );
            System.out.println( "Cache Response : " + jedis.set("Message", "Hello! The cache is working from Java!"));
    
            // Demonstrate "SET Message" executed as expected...
            System.out.println( "\nCache Command  : GET Message" );
            System.out.println( "Cache Response : " + jedis.get("Message"));
    
            jedis.close();
        }
    }

    This code shows you how to connect to an Azure Managed Redis instance using the cache host name and key environment variables. The code also stores and retrieves a string value in the cache. The PING commands are also executed.

  6. Close the App.java file.

Build and run the app

Execute the following Maven command to build and run the app:

mvn compile exec:java -D exec.mainClass=example.demo.App

In the following output, you can see that the Message key previously had a cached value. The value was updated to a new value using jedis.set. The app also executed the PING commands.

Cache Command  : Ping
Cache Response : PONG

Cache Command  : GET Message
Cache Response : Hello! The cache is working from Java!

Cache Command  : SET Message
Cache Response : OK

Cache Command  : GET Message
Cache Response : Hello! The cache is working from Java!

[!INCLUDE redis-cache-resource-group-clean-up]

Next steps

In this quickstart, you learned how to use Azure Managed Redis from a Java application.