Skip to content

Commit 0e51178

Browse files
authored
Added withServerTimeout and withKeepAliveInterval to HubConnectionBuilder for java client (#46172)
* Added withServerTimeout and withKeepAliveInterval to HubConnectionBuilder for java client
1 parent 19e357b commit 0e51178

File tree

3 files changed

+59
-5
lines changed

3 files changed

+59
-5
lines changed

src/SignalR/clients/java/signalr/core/src/main/java/com/microsoft/signalr/HttpHubConnectionBuilder.java

+25-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public class HttpHubConnectionBuilder {
2323
private Map<String, String> headers;
2424
private TransportEnum transportEnum;
2525
private Action1<OkHttpClient.Builder> configureBuilder;
26+
private long serverTimeout = HubConnection.DEFAULT_SERVER_TIMEOUT;
27+
private long keepAliveInterval = HubConnection.DEFAULT_KEEP_ALIVE_INTERVAL;
2628

2729
HttpHubConnectionBuilder(String url) {
2830
this.url = url;
@@ -140,13 +142,35 @@ public HttpHubConnectionBuilder setHttpClientBuilderCallback(Action1<OkHttpClien
140142
return this;
141143
}
142144

145+
/**
146+
* Sets serverTimeout for the {@link HubConnection}.
147+
*
148+
* @param timeoutInMilliseconds The serverTimeout to be set.
149+
* @return This instance of the HttpHubConnectionBuilder.
150+
*/
151+
public HttpHubConnectionBuilder withServerTimeout(long timeoutInMilliseconds) {
152+
this.serverTimeout = timeoutInMilliseconds;
153+
return this;
154+
}
155+
156+
/**
157+
* Sets keepAliveInterval for the {@link HubConnection}.
158+
*
159+
* @param intervalInMilliseconds The keepAliveInterval to be set.
160+
* @return This instance of the HttpHubConnectionBuilder.
161+
*/
162+
public HttpHubConnectionBuilder withKeepAliveInterval(long intervalInMilliseconds) {
163+
this.keepAliveInterval = intervalInMilliseconds;
164+
return this;
165+
}
166+
143167
/**
144168
* Builds a new instance of {@link HubConnection}.
145169
*
146170
* @return A new instance of {@link HubConnection}.
147171
*/
148172
public HubConnection build() {
149173
return new HubConnection(url, transport, skipNegotiate, httpClient, protocol, accessTokenProvider,
150-
handshakeResponseTimeout, headers, transportEnum, configureBuilder);
174+
handshakeResponseTimeout, headers, transportEnum, configureBuilder, serverTimeout, keepAliveInterval);
151175
}
152176
}

src/SignalR/clients/java/signalr/core/src/main/java/com/microsoft/signalr/HubConnection.java

+9-3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
* A connection used to invoke hub methods on a SignalR Server.
3131
*/
3232
public class HubConnection implements AutoCloseable {
33+
static final long DEFAULT_SERVER_TIMEOUT = 30 * 1000;
34+
static final long DEFAULT_KEEP_ALIVE_INTERVAL = 15 * 1000;
35+
3336
private static final byte RECORD_SEPARATOR = 0x1e;
3437
private static final List<Type> emptyArray = new ArrayList<>();
3538
private static final int MAX_NEGOTIATE_ATTEMPTS = 100;
@@ -49,8 +52,8 @@ public class HubConnection implements AutoCloseable {
4952
// These are all user-settable properties
5053
private String baseUrl;
5154
private List<OnClosedCallback> onClosedCallbackList;
52-
private long keepAliveInterval = 15 * 1000;
53-
private long serverTimeout = 30 * 1000;
55+
private long keepAliveInterval = DEFAULT_KEEP_ALIVE_INTERVAL;
56+
private long serverTimeout = DEFAULT_SERVER_TIMEOUT;
5457
private long handshakeResponseTimeout = 15 * 1000;
5558

5659
// Private property, modified for testing
@@ -120,7 +123,7 @@ Transport getTransport() {
120123

121124
HubConnection(String url, Transport transport, boolean skipNegotiate, HttpClient httpClient, HubProtocol protocol,
122125
Single<String> accessTokenProvider, long handshakeResponseTimeout, Map<String, String> headers, TransportEnum transportEnum,
123-
Action1<OkHttpClient.Builder> configureBuilder) {
126+
Action1<OkHttpClient.Builder> configureBuilder, long serverTimeout, long keepAliveInterval) {
124127
if (url == null || url.isEmpty()) {
125128
throw new IllegalArgumentException("A valid url is required.");
126129
}
@@ -159,6 +162,9 @@ Transport getTransport() {
159162
this.headers = headers;
160163
this.skipNegotiate = skipNegotiate;
161164

165+
this.serverTimeout = serverTimeout;
166+
this.keepAliveInterval = keepAliveInterval;
167+
162168
this.callback = (payload) -> ReceiveLoop(payload);
163169
}
164170

src/SignalR/clients/java/signalr/test/src/main/java/com/microsoft/signalr/HubConnectionTest.java

+25-1
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ public void removingMultipleHandlersWithOneCallToRemove() {
314314
Action action = () -> value.getAndUpdate((val) -> val + 1);
315315
Action secondAction = () -> {
316316
value.getAndUpdate((val) -> val + 2);
317-
317+
318318
complete.onComplete();
319319
};
320320

@@ -3962,4 +3962,28 @@ public void hubConnectionStopDuringConnecting() {
39623962

39633963
assertTrue(close.blockingAwait(30, TimeUnit.SECONDS));
39643964
}
3965+
3966+
@Test
3967+
public void serverTimeoutIsSetThroughBuilder()
3968+
{
3969+
long timeout = 60 * 1000;
3970+
HubConnection hubConnection = HubConnectionBuilder
3971+
.create("http://example.com")
3972+
.withServerTimeout(timeout)
3973+
.build();
3974+
3975+
assertEquals(timeout, hubConnection.getServerTimeout());
3976+
}
3977+
3978+
@Test
3979+
public void keepAliveIntervalIsSetThroughBuilder()
3980+
{
3981+
long interval = 60 * 1000;
3982+
HubConnection hubConnection = HubConnectionBuilder
3983+
.create("http://example.com")
3984+
.withKeepAliveInterval(interval)
3985+
.build();
3986+
3987+
assertEquals(interval, hubConnection.getKeepAliveInterval());
3988+
}
39653989
}

0 commit comments

Comments
 (0)