From 73621a6f77ef69c7504b6e836ce3e9a9b23bff0a Mon Sep 17 00:00:00 2001 From: Katia Aresti Date: Thu, 25 Jul 2024 14:12:45 +0200 Subject: [PATCH] ISPN-16317 Evolve connector --- .../connect/TutorialsConnectorHelper.java | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/infinispan-remote/connect-to-infinispan-server/src/main/java/org/infinispan/tutorial/simple/connect/TutorialsConnectorHelper.java b/infinispan-remote/connect-to-infinispan-server/src/main/java/org/infinispan/tutorial/simple/connect/TutorialsConnectorHelper.java index 75e6c9f1..a3c1c8d2 100644 --- a/infinispan-remote/connect-to-infinispan-server/src/main/java/org/infinispan/tutorial/simple/connect/TutorialsConnectorHelper.java +++ b/infinispan-remote/connect-to-infinispan-server/src/main/java/org/infinispan/tutorial/simple/connect/TutorialsConnectorHelper.java @@ -1,5 +1,6 @@ package org.infinispan.tutorial.simple.connect; +import org.infinispan.client.hotrod.RemoteCache; import org.infinispan.client.hotrod.RemoteCacheManager; import org.infinispan.client.hotrod.configuration.ClientIntelligence; import org.infinispan.client.hotrod.configuration.ConfigurationBuilder; @@ -67,13 +68,11 @@ public static final RemoteCacheManager connect() { public static InfinispanContainer INFINISPAN_CONTAINER; public static final RemoteCacheManager connect(ConfigurationBuilder builder) { - RemoteCacheManager cacheManager = null; try { - builder.addServer().host(HOST).port(SINGLE_PORT); cacheManager = new RemoteCacheManager(builder.build()); - // Clear the cache in case it already exists from a previous running tutorial - cacheManager.getCache(TUTORIAL_CACHE_NAME).clear(); + //ping + System.out.println("Get cache names: " + cacheManager.getCacheNames()); } catch (Exception ex) { System.out.println("Unable to connect to a running server in localhost:11222. Try test containers"); if (cacheManager != null) { @@ -85,15 +84,24 @@ public static final RemoteCacheManager connect(ConfigurationBuilder builder) { if (cacheManager == null) { try { startInfinispanContainer(); - builder.addServer().host(HOST).port(INFINISPAN_CONTAINER.getFirstMappedPort()); + builder.addServer().host(HOST).port(INFINISPAN_CONTAINER.getMappedPort(SINGLE_PORT)); cacheManager = new RemoteCacheManager(builder.build()); - // Clear the cache in case it already exists from a previous running tutorial - cacheManager.getCache(TUTORIAL_CACHE_NAME).clear(); + //ping + System.out.println("Get cache names: " + cacheManager.getCacheNames()); } catch (Exception ex) { System.out.println("Infinispan Server start with Testcontainers failed. Exit"); System.exit(0); } } + if (cacheManager != null) { + // Clear the cache in case it already exists from a previous running tutorial + RemoteCache testCache = cacheManager.getCache(TUTORIAL_CACHE_NAME); + if (testCache != null) { + testCache.clear(); + } else { + System.out.println("Test cache does not exist"); + } + } // Return the connected cache manager return cacheManager; }