Skip to content

Commit cd9a1ab

Browse files
jjz921024sazzad16anotherJJz
authored
Get enriched Connection information (#3745)
* Adds a new method `toIdentityString()` that returns the identifier info for the connection * Update src/main/java/redis/clients/jedis/Connection.java Co-authored-by: M Sazzadul Hoque <[email protected]> * fix NPE about invoke `toIdentityString` before connect * add test case --------- Co-authored-by: M Sazzadul Hoque <[email protected]> Co-authored-by: anotherJJz <[email protected]>
1 parent 2af43de commit cd9a1ab

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

Diff for: src/main/java/redis/clients/jedis/Connection.java

+60
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.io.Closeable;
66
import java.io.IOException;
77
import java.net.Socket;
8+
import java.net.SocketAddress;
89
import java.net.SocketException;
910
import java.nio.ByteBuffer;
1011
import java.nio.CharBuffer;
@@ -37,6 +38,8 @@ public class Connection implements Closeable {
3738
private int soTimeout = 0;
3839
private int infiniteSoTimeout = 0;
3940
private boolean broken = false;
41+
private boolean strValActive;
42+
private String strVal;
4043

4144
public Connection() {
4245
this(Protocol.DEFAULT_HOST, Protocol.DEFAULT_PORT);
@@ -72,6 +75,63 @@ public String toString() {
7275
return "Connection{" + socketFactory + "}";
7376
}
7477

78+
public String toIdentityString() {
79+
if (strValActive == broken && strVal != null) {
80+
return strVal;
81+
}
82+
83+
int id = hashCode();
84+
String classInfo = getClass().toString();
85+
86+
if (socket == null) {
87+
StringBuilder buf = new StringBuilder(56)
88+
.append("[")
89+
.append(classInfo)
90+
.append(", id: 0x")
91+
.append(id)
92+
.append(']');
93+
return buf.toString();
94+
}
95+
96+
SocketAddress remoteAddr = socket.getRemoteSocketAddress();
97+
SocketAddress localAddr = socket.getLocalSocketAddress();
98+
if (remoteAddr != null) {
99+
StringBuilder buf = new StringBuilder(101)
100+
.append("[")
101+
.append(classInfo)
102+
.append(", id: 0x")
103+
.append(id)
104+
.append(", L:")
105+
.append(localAddr)
106+
.append(broken? " ! " : " - ")
107+
.append("R:")
108+
.append(remoteAddr)
109+
.append(']');
110+
strVal = buf.toString();
111+
} else if (localAddr != null) {
112+
StringBuilder buf = new StringBuilder(64)
113+
.append("[")
114+
.append(classInfo)
115+
.append(", id: 0x")
116+
.append(id)
117+
.append(", L:")
118+
.append(localAddr)
119+
.append(']');
120+
strVal = buf.toString();
121+
} else {
122+
StringBuilder buf = new StringBuilder(56)
123+
.append("[")
124+
.append(classInfo)
125+
.append(", id: 0x")
126+
.append(id)
127+
.append(']');
128+
strVal = buf.toString();
129+
}
130+
131+
strValActive = broken;
132+
return strVal;
133+
}
134+
75135
public final RedisProtocol getRedisProtocol() {
76136
return protocol;
77137
}

Diff for: src/test/java/redis/clients/jedis/ConnectionTest.java

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package redis.clients.jedis;
22

33
import org.junit.After;
4+
import org.junit.Assert;
45
import org.junit.Test;
56

67
import redis.clients.jedis.exceptions.JedisConnectionException;
@@ -40,4 +41,14 @@ public void checkCloseable() {
4041
client.connect();
4142
client.close();
4243
}
44+
45+
@Test
46+
public void checkIdentityString() {
47+
client = new Connection("localhost", 6379);
48+
Assert.assertFalse(client.toIdentityString().contains("6379"));
49+
client.connect();
50+
Assert.assertTrue(client.toIdentityString().contains("6379"));
51+
client.close();
52+
Assert.assertTrue(client.toIdentityString().contains("6379"));
53+
}
4354
}

0 commit comments

Comments
 (0)