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

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

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

Related

  • Distributed Computing Simplified
  • How To Install CMAK, Apache Kafka, Java 18, and Java 19 [Video Tutorials]
  • Exploring Hazelcast With Spring Boot
  • Implement a Distributed Database to Your Java Application

Trending

  • Building Resilient Networks: Limiting the Risk and Scope of Cyber Attacks
  • Building Resilient Identity Systems: Lessons from Securing Billions of Authentication Requests
  • Mastering Fluent Bit: Installing and Configuring Fluent Bit on Kubernetes (Part 3)
  • Can You Run a MariaDB Cluster on a $150 Kubernetes Lab? I Gave It a Shot
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. How To Create a Failover Client Using the Hazelcast Viridian Serverless

How To Create a Failover Client Using the Hazelcast Viridian Serverless

Learn how to update the code in a Java client to automatically connect to a secondary, failover cluster if it cannot connect to its original, primary cluster.

By 
Fawaz Ghali, PhD user avatar
Fawaz Ghali, PhD
DZone Core CORE ·
Mar. 11, 23 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
7.7K Views

Join the DZone community and get the full member experience.

Join For Free

Failover is an important feature of systems that rely on near-constant availability. In Hazelcast, a failover client automatically redirects its traffic to a secondary cluster when the client cannot connect to the primary cluster. Consider using a failover client with WAN replication as part of your disaster recovery strategy. In this tutorial, you’ll update the code in a Java client to automatically connect to a secondary, failover cluster if it cannot connect to its original, primary cluster. You’ll also run a simple test to make sure that your configuration is correct and then adjust it to include exception handling. You'll learn how to collect all the resources that you need to create a failover client for a primary and secondary cluster, create a failover client based on the sample Java client, test failover and add exception handling for operations.

Step 1: Set Up Clusters and Clients

Create two Viridian Serverless clusters that you’ll use as your primary and secondary clusters and then download and connect sample Java clients to them.

  1. Create the Viridian Serverless cluster that you’ll use as your primary cluster. When the cluster is ready to use, the Quick Connection Guide is displayed.
  2. Select the Java icon and follow the on-screen instructions to download, extract, and connect the preconfigured Java client to your primary cluster.
  3. Create the Viridian Serverless cluster that you’ll use as your secondary cluster.
  4. Follow the instructions in the Quick Connection Guide to download, extract, and connect the preconfigured Java client to your secondary cluster.

You now have two running clusters, and you’ve checked that both Java clients can connect.

Step 2: Configure a Failover Client

To create a failover client, update the configuration and code of the Java client for your primary cluster.

Start by adding the keystore files from the Java client of your secondary cluster.

  1. Go to the directory where you extracted the Java client for your secondary cluster and then navigate to src/main/resources.
  2. Rename the client.keystore file to client2.keystore and rename the client.truststore file to client2.truststore to avoid overwriting the files in your primary cluster keystore.
  3. Copy both files over to the src/main/resources directory of your primary cluster.

Update the code in the Java client (ClientwithSsl.java) of your primary cluster to include a failover class and the connection details for your secondary cluster. You can find these connection details in the Java client of your secondary cluster.

  1. Go to the directory where you extracted the Java client for your primary cluster and then navigate to src/main/java/com/hazelcast/cloud/.
  2. Open the Java client (ClientwithSsl.java) and make the following updates. An example failover client is also available for download.

Step 3: Verify Failover

Check that your failover client automatically connects to the secondary cluster when your primary cluster is stopped.

  1. Make sure that both Viridian Serverless clusters are running.
  2. Connect your failover client to the primary cluster in the same way as you did in Step 1.
  3. Stop your primary cluster. From the dashboard of your primary cluster, in Cluster Details, select Pause. In the console, you’ll see the following messages in order as the client disconnects from your primary cluster and reconnects to the secondary cluster:
  • CLIENT_DISCONNECTED
  • CLIENT_CONNECTED
  • CLIENT_CHANGED_CLUSTER

If you’re using the nonStopMapExample in the sample Java client, your client stops. This is expected because write operations are not retryable when a cluster is disconnected. The client has sent a put request to the cluster but has not received a response, so the result of the request is unknown. To prevent the client from overwriting more recent write operations, this write operation is stopped and an exception is thrown.

Step 4: Exception Handling

Update the nonStopMapExample() function in your failover client to trap the exception that is thrown when the primary cluster disconnects.

  1. Add the following try-catch block to the while loop in the nonStopMapExample() function. This code replaces the original map.put() function.

    try {
        map.put("key-" + randomKey, "value-" + randomKey);
    } catch (Exception e) {
        // Captures exception from disconnected client
        e.printStackTrace();
    }


2. Verify your code again (repeat Step 3). This time the client continues to write map entries after it connects to the secondary cluster.

Disaster recovery Hazelcast cluster Java (programming language) WAN optimization

Published at DZone with permission of Fawaz Ghali, PhD. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Distributed Computing Simplified
  • How To Install CMAK, Apache Kafka, Java 18, and Java 19 [Video Tutorials]
  • Exploring Hazelcast With Spring Boot
  • Implement a Distributed Database to Your Java Application

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!