Skip to content

Commit 42d6ecb

Browse files
committed
Add an option for non-blocking OCPP-J connects
Add a new JSONConfiguration parameter "CONNECT_NON_BLOCKING" of Boolean type, which, if true, causes the OCPP-J clients to make non-blocking connect() and disconnect() calls to the underlying WebSocket library. The completion of the connection or disconnection attempt will be communicated via the callbacks in the ClientEvents object passed to the OCPP library's connect() method. Note that a failed connection attempt (connection refused or timeout, if configured) yields a call to the connectionClosed() callback.
1 parent 10e1067 commit 42d6ecb

File tree

3 files changed

+91
-26
lines changed

3 files changed

+91
-26
lines changed

OCPP-J/src/main/java/eu/chargetime/ocpp/JSONConfiguration.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class JSONConfiguration {
3636
public static final String PING_INTERVAL_PARAMETER = "PING_INTERVAL";
3737
public static final String USERNAME_PARAMETER = "USERNAME";
3838
public static final String PASSWORD_PARAMETER = "PASSWORD";
39+
public static final String CONNECT_NON_BLOCKING_PARAMETER = "CONNECT_NON_BLOCKING";
3940
public static final String CONNECT_TIMEOUT_IN_MS_PARAMETER = "CONNECT_TIMEOUT_IN_MS";
4041
public static final String WEBSOCKET_WORKER_COUNT = "WEBSOCKET_WORKER_COUNT";
4142

OCPP-J/src/main/java/eu/chargetime/ocpp/WebSocketTransmitter.java

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,24 @@ public void onError(Exception ex) {
134134

135135
configure();
136136

137-
logger.debug("Trying to connect to: {}", resource);
137+
boolean isNonBlocking = isNonBlockingParameterSet();
138138

139-
try {
140-
client.connectBlocking();
141-
closed = false;
142-
} catch (Exception ex) {
143-
logger.warn("client.connectBlocking() failed", ex);
139+
logger.debug("Trying to connect to: {}{}", resource, isNonBlocking ? "" : " [blocking]");
140+
141+
if (isNonBlocking) {
142+
try {
143+
client.connect();
144+
closed = false;
145+
} catch (Exception ex) {
146+
logger.warn("client.connect() failed", ex);
147+
}
148+
} else {
149+
try {
150+
client.connectBlocking();
151+
closed = false;
152+
} catch (Exception ex) {
153+
logger.warn("client.connectBlocking() failed", ex);
154+
}
144155
}
145156
}
146157

@@ -175,16 +186,37 @@ public void disconnect() {
175186
if (client == null) {
176187
return;
177188
}
178-
try {
179-
client.closeBlocking();
180-
} catch (Exception ex) {
181-
logger.info("client.closeBlocking() failed", ex);
182-
} finally {
183-
client = null;
184-
closed = true;
189+
190+
boolean isNonBlocking = isNonBlockingParameterSet();
191+
192+
logger.debug("Disconnecting{}", isNonBlocking ? "" : " [blocking]");
193+
194+
if (isNonBlocking) {
195+
try {
196+
client.close();
197+
} catch (Exception ex) {
198+
logger.info("client.close() failed", ex);
199+
} finally {
200+
client = null;
201+
closed = true;
202+
}
203+
} else {
204+
try {
205+
client.closeBlocking();
206+
} catch (Exception ex) {
207+
logger.info("client.closeBlocking() failed", ex);
208+
} finally {
209+
client = null;
210+
closed = true;
211+
}
185212
}
186213
}
187214

215+
private boolean isNonBlockingParameterSet() {
216+
Object rawParam = configuration.getParameter(JSONConfiguration.CONNECT_NON_BLOCKING_PARAMETER);
217+
return rawParam instanceof Boolean ? (Boolean) rawParam : false;
218+
}
219+
188220
@Override
189221
public void send(Object request) throws NotConnectedException {
190222
if (client == null) {

ocpp-v2/src/main/java/eu/chargetime/ocpp/MultiProtocolWebSocketTransmitter.java

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,24 @@ public Socket createSocket(
165165

166166
configure();
167167

168-
logger.debug("Trying to connect to: {}", resource);
168+
boolean isNonBlocking = isNonBlockingParameterSet();
169169

170-
try {
171-
client.connectBlocking();
172-
closed = false;
173-
} catch (Exception ex) {
174-
logger.warn("client.connectBlocking() failed", ex);
170+
logger.debug("Trying to connect to: {}{}", resource, isNonBlocking ? "" : " [blocking]");
171+
172+
if (isNonBlocking) {
173+
try {
174+
client.connect();
175+
closed = false;
176+
} catch (Exception ex) {
177+
logger.warn("client.connect() failed", ex);
178+
}
179+
} else {
180+
try {
181+
client.connectBlocking();
182+
closed = false;
183+
} catch (Exception ex) {
184+
logger.warn("client.connectBlocking() failed", ex);
185+
}
175186
}
176187
}
177188

@@ -206,16 +217,37 @@ public void disconnect() {
206217
if (client == null) {
207218
return;
208219
}
209-
try {
210-
client.closeBlocking();
211-
} catch (Exception ex) {
212-
logger.info("client.closeBlocking() failed", ex);
213-
} finally {
214-
client = null;
215-
closed = true;
220+
221+
boolean isNonBlocking = isNonBlockingParameterSet();
222+
223+
logger.debug("Disconnecting{}", isNonBlocking ? "" : " [blocking]");
224+
225+
if (isNonBlocking) {
226+
try {
227+
client.close();
228+
} catch (Exception ex) {
229+
logger.info("client.close() failed", ex);
230+
} finally {
231+
client = null;
232+
closed = true;
233+
}
234+
} else {
235+
try {
236+
client.closeBlocking();
237+
} catch (Exception ex) {
238+
logger.info("client.closeBlocking() failed", ex);
239+
} finally {
240+
client = null;
241+
closed = true;
242+
}
216243
}
217244
}
218245

246+
private boolean isNonBlockingParameterSet() {
247+
Object rawParam = configuration.getParameter(JSONConfiguration.CONNECT_NON_BLOCKING_PARAMETER);
248+
return rawParam instanceof Boolean ? (Boolean) rawParam : false;
249+
}
250+
219251
@Override
220252
public void send(Object request) throws NotConnectedException {
221253
if (client == null) {

0 commit comments

Comments
 (0)