@@ -90,7 +90,7 @@ private int findFreePort() {
9090 }
9191
9292 public void connect () throws IOException {
93- if (useEmbedded && isPortAvailable ( port ) && isPortAvailable ( port + 1 ) ) {
93+ if (useEmbedded ) {
9494 try {
9595 startEmbeddedServer ();
9696 } catch (IOException e ) {
@@ -141,7 +141,7 @@ public void connect() throws IOException {
141141 }
142142 } catch (JedisConnectionException e ) {
143143 connected = false ;
144- logger .error ("Failed to connect to Redis server at " + host + ":" + port , e );
144+ logger .error ("Failed to connect to Redis server at " + host + ":" + port );
145145 if (jedisPool != null ) {
146146 jedisPool .close ();
147147 jedisPool = null ;
@@ -219,6 +219,45 @@ private void stopEmbeddedServer() {
219219 }
220220 }
221221
222+ public boolean ping () {
223+ if (useMockFallback && mockRedis != null ) {
224+ return mockRedis .isRunning ();
225+ }
226+ if (useEmbedded && embeddedServer == null && !useMockFallback ) {
227+ logger .warn ("Cannot ping, embedded server is not running and mock fallback is disabled." );
228+ return false ;
229+ }
230+ if (jedisPool == null && !useEmbedded ) {
231+ logger .warn ("Cannot ping, JedisPool is not initialized." );
232+ return false ;
233+ }
234+
235+ if (useEmbedded && embeddedServer != null && embeddedServer .isActive ()) {
236+ return true ;
237+ } else if (useEmbedded ) {
238+ return false ;
239+ }
240+
241+ try (Jedis jedis = jedisPool .getResource ()) {
242+ String reply = jedis .ping ();
243+ boolean success = "PONG" .equalsIgnoreCase (reply );
244+ if (!success ) {
245+ logger .warn ("Ping failed, received unexpected reply: " + reply );
246+ } else {
247+ this .connected = true ;
248+ }
249+ return success ;
250+ } catch (JedisConnectionException e ) {
251+ logger .warn ("Could not connect to Redis server at " + host + ":" + port + " for ping: " + e .getMessage ());
252+ this .connected = false ;
253+ return false ;
254+ } catch (Exception e ) {
255+ logger .warn ("An unexpected error occurred during ping: " + e .getMessage (), e );
256+ this .connected = false ;
257+ return false ;
258+ }
259+ }
260+
222261
223262 public void disconnect () {
224263 if (useMockFallback && mockRedis != null ) {
0 commit comments