Skip to content

Commit 1f46b88

Browse files
authored
WebSocket & WebNotifications: Stability and Logging Improvements (#591)
* Improvements to WebSocket implementation * Add logging * Resolve @iroqueta review * Improve messages * @iroqueta review * Refactoring and improvements * Removed unused services variable * Revet logging * Refactoring
1 parent 59b1737 commit 1f46b88

File tree

11 files changed

+283
-343
lines changed

11 files changed

+283
-343
lines changed

gxwebsocket/src/main/java/com/genexus/internet/websocket/GXWebSocket.java

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,49 +10,34 @@
1010
import com.genexus.websocket.Session;
1111

1212
@ServerEndpoint(value = "/gxwebsocket")
13-
public class GXWebSocket extends GXWebSocketCommon implements IGXWebSocketAsync {
14-
15-
private static GXWebSocket instance = null;
16-
17-
public GXWebSocket(){
18-
instance = this;
19-
}
20-
21-
public static IGXWebSocketAsync getInstance() {
22-
return instance;
13+
public class GXWebSocket {
14+
15+
private GXWebSocketService wsService;
16+
17+
public GXWebSocket() {
18+
wsService = GXWebSocketService.getService();
2319
}
24-
20+
2521
@OnOpen
26-
public void OnOpen (javax.websocket.Session session) {
27-
OnOpenCommon(new Session(session));
22+
public void onOpen(javax.websocket.Session session) {
23+
wsService.onOpen(new Session(session));
2824
}
2925

3026
@OnMessage
31-
public void OnMessage (String txt, javax.websocket.Session session) {
32-
OnMessageCommon(txt, new Session(session));
27+
public void onMessage(String txt, javax.websocket.Session session) {
28+
wsService.onMessage(txt, new Session(session));
3329
}
3430

3531
@OnClose
36-
public void myOnClose (javax.websocket.Session session, CloseReason reason) {
37-
myOnCloseCommon(new Session(session));
32+
public void onClose(javax.websocket.Session session, CloseReason reason) {
33+
wsService.onClose(new Session(session));
3834
}
3935

4036
@OnError
4137
public void onError(Throwable exception, javax.websocket.Session session) {
42-
onErrorCommon(exception, new Session(session));
38+
wsService.onError(exception, new Session(session));
4339
}
44-
45-
public SendResponseType send(String clientId, String message) {
46-
return sendCommon(clientId, message);
47-
}
48-
49-
public void broadcast(String message) {
50-
broadcastCommon(message);
51-
}
5240

53-
public boolean start() {
54-
return true;
55-
}
5641
}
5742

5843

gxwebsocket/src/main/java/com/genexus/websocket/Session.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public Integer getHashCode() {
1414
return new Integer(session.hashCode());
1515
}
1616

17-
public String getQueryString() {
17+
public String getId() {
1818
return session.getQueryString();
1919
}
2020

gxwebsocketjakarta/src/main/java/com/genexus/internet/websocket/GXWebSocket.java

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,49 +10,33 @@
1010
import com.genexus.websocket.Session;
1111

1212
@ServerEndpoint(value = "/gxwebsocket")
13-
public class GXWebSocket extends GXWebSocketCommon implements IGXWebSocketAsync {
14-
15-
private static GXWebSocket instance = null;
16-
17-
public GXWebSocket(){
18-
instance = this;
19-
}
20-
21-
public static IGXWebSocketAsync getInstance() {
22-
return instance;
13+
public class GXWebSocket {
14+
15+
private GXWebSocketService wsService;
16+
17+
public GXWebSocket() {
18+
wsService = GXWebSocketService.getService();
2319
}
24-
20+
2521
@OnOpen
26-
public void OnOpen (jakarta.websocket.Session session) {
27-
OnOpenCommon(new Session(session));
22+
public void onOpen(jakarta.websocket.Session session) {
23+
wsService.onOpen(new Session(session));
2824
}
29-
25+
3026
@OnMessage
31-
public void OnMessage (String txt, jakarta.websocket.Session session) {
32-
OnMessageCommon(txt, new Session(session));
27+
public void onMessage(String txt, jakarta.websocket.Session session) {
28+
wsService.onMessage(txt, new Session(session));
3329
}
3430

3531
@OnClose
36-
public void myOnClose (jakarta.websocket.Session session, CloseReason reason) {
37-
myOnCloseCommon(new Session(session));
32+
public void onClose(jakarta.websocket.Session session, CloseReason reason) {
33+
wsService.onClose(new Session(session));
3834
}
39-
35+
4036
@OnError
41-
public void onError(Throwable exception, jakarta.websocket.Session session) {
42-
onErrorCommon(exception, new Session(session));
43-
}
44-
45-
public SendResponseType send(String clientId, String message) {
46-
return sendCommon(clientId, message);
37+
public void onError(Throwable exception, jakarta.websocket.Session session) {
38+
wsService.onError(exception, new Session(session));
4739
}
48-
49-
public void broadcast(String message) {
50-
broadcastCommon(message);
51-
}
52-
53-
public boolean start() {
54-
return true;
55-
}
5640
}
5741

5842

gxwebsocketjakarta/src/main/java/com/genexus/websocket/Session.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import jakarta.websocket.RemoteEndpoint;
44
import java.io.IOException;
55

6-
public class Session implements ISession{
6+
public class Session implements ISession {
77
private jakarta.websocket.Session session;
88

99
public Session(jakarta.websocket.Session session) {
@@ -14,7 +14,7 @@ public Integer getHashCode() {
1414
return new Integer(session.hashCode());
1515
}
1616

17-
public String getQueryString() {
17+
public String getId() {
1818
return session.getQueryString();
1919
}
2020

java/src/main/java/com/genexus/Application.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
import java.io.Closeable;
44
import java.io.IOException;
55
import java.sql.SQLException;
6-
import java.util.Date;
7-
import java.util.Enumeration;
8-
import java.util.Properties;
9-
import java.util.Vector;
6+
import java.util.*;
107

118
import com.genexus.db.DBConnectionManager;
129
import com.genexus.db.DynamicExecute;
@@ -42,7 +39,6 @@ public class Application
4239
private static Boolean isJMXEnabled;
4340

4441
public static Class gxCfg = ApplicationContext.getInstance().getClass();
45-
//public static ModelContext clientContext;
4642
private static Vector<ICleanedup> toCleanup = new Vector<>();
4743
static LocalUtil localUtil;
4844
static Class ClassName = null;
@@ -753,10 +749,10 @@ public static boolean getShowConnectError()
753749

754750

755751
static boolean useSmartCache = false;
752+
756753
public static com.genexus.GXSmartCacheProvider getSmartCacheProvider(int handle)
757754
{
758755
useSmartCache = true;
759756
return getConnectionManager().getUserInformation(handle).getSmartCacheProvider();
760-
}
761-
757+
}
762758
}

java/src/main/java/com/genexus/internet/GXWebNotification.java

Lines changed: 44 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,35 @@
22
import com.genexus.ModelContext;
33
import com.genexus.diagnostics.core.ILogger;
44
import com.genexus.diagnostics.core.LogManager;
5-
import com.genexus.internet.websocket.IGXWebSocketAsync;
5+
import com.genexus.internet.websocket.GXWebSocketService;
66
import com.genexus.internet.websocket.SendResponseType;
77
import com.genexus.xml.GXXMLSerializable;
88

99
public class GXWebNotification {
1010

1111
public static final ILogger logger = LogManager.getLogger(GXWebNotification.class);
12+
private GXWebSocketService wsService;
13+
private HttpContext httpContext;
1214

13-
private static IGXWebSocketAsync ws;
14-
private HttpContext _ctx;
15-
16-
private short _errCode;
17-
private String _errDescription;
18-
19-
15+
private short errCode;
16+
private String errDescription;
17+
2018
public short getErrCode()
2119
{
22-
return _errCode;
20+
return errCode;
2321
}
2422

2523
public String getErrDescription()
2624
{
27-
return _errDescription;
25+
return errDescription;
2826
}
2927

3028
public GXWebNotification(ModelContext gxContext)
31-
{
32-
_ctx = (HttpContext) gxContext.getHttpContext();
33-
if (ws == null)
34-
{
35-
try {
36-
setError((short)1);
37-
Class<?> c = Class.forName("com.genexus.internet.websocket.GXWebSocket");
38-
java.lang.reflect.Method method = c.getDeclaredMethod("getInstance", (Class[])null);
39-
Object o = method.invoke(null, (Object[])null);
40-
ws = (IGXWebSocketAsync)o;
41-
if (ws != null)
42-
setError((short)0);
43-
} catch (Exception e) {
44-
logger.error("GXWebNotification", e);
45-
}
46-
}
47-
if (_errCode != 0)
48-
{
49-
logger.error("Could not create com.genexus.internet.GXWebSocket instance. Check whether WebServer requirements are met and WebNotifications Provider Generator Property is set");
50-
}
29+
{
30+
wsService = GXWebSocketService.getService();
31+
httpContext = (HttpContext) gxContext.getHttpContext();
5132
}
52-
33+
5334
public short notifyClient(String clientId, String message)
5435
{
5536
return notifyClientImpl(clientId, message);
@@ -62,43 +43,38 @@ public short notifyClient(String clientId, GXXMLSerializable message)
6243
}
6344

6445
public short notifyClientImpl(String clientId, String message)
65-
{
66-
if (ws != null)
46+
{
47+
SendResponseType result = wsService.send(clientId.trim(), message);
48+
switch (result)
6749
{
68-
SendResponseType result = ws.send(clientId.trim(), message);
69-
switch (result)
70-
{
71-
case OK:
72-
setError((short)0);
73-
break;
74-
case SessionNotFound:
75-
setError((short)2);
76-
break;
77-
case SessionInvalid:
78-
setError((short)3);
79-
break;
80-
case SendFailed:
81-
setError((short)4);
82-
break;
83-
default:
84-
break;
85-
}
50+
case OK:
51+
setError((short)0);
52+
break;
53+
case SessionNotFound:
54+
setError((short)2);
55+
break;
56+
case SessionInvalid:
57+
setError((short)3);
58+
break;
59+
case SendFailed:
60+
setError((short)4);
61+
break;
62+
default:
63+
break;
8664
}
87-
else
88-
setError((short)1);
89-
90-
return _errCode;
65+
66+
return errCode;
9167
}
9268

9369
public short notify(String message)
9470
{
95-
return notifyClient(_ctx.getClientId(), message);
71+
return notifyClient(httpContext.getClientId(), message);
9672
}
9773

9874

9975
public short notify(GXXMLSerializable message)
10076
{
101-
return notifyClient(_ctx.getClientId(), message);
77+
return notifyClient(httpContext.getClientId(), message);
10278
}
10379

10480

@@ -113,44 +89,39 @@ public void broadcast(String message)
11389
}
11490

11591
private void broadcastImpl(String message)
116-
{
117-
if (ws != null)
118-
{
119-
ws.broadcast(message);
120-
setError((short)0);
121-
}
122-
else
123-
setError((short)1);
92+
{
93+
wsService.broadcast(message);
94+
setError((short)0);
12495
}
12596

12697
private void setError(short i)
12798
{
128-
this._errCode = i;
99+
this.errCode = i;
129100
switch (i)
130101
{
131102
case 0:
132-
_errDescription = "OK";
103+
errDescription = "OK";
133104
break;
134105
case 1:
135-
_errDescription = "Could not start WebSocket Server";
106+
errDescription = "WebSocket Server has not been initialized yet. No incoming connections were received";
136107
break;
137108
case 2:
138-
_errDescription = "WebSocket Session not found";
109+
errDescription = "WebSocket Session not found. The client is not connected to socket server";
139110
break;
140111
case 3:
141-
_errDescription = "WebSocket Session is closed or invalid";
112+
errDescription = "WebSocket Session was found, but it's state was closed or invalid";
142113
break;
143114
case 4:
144-
_errDescription = "Message could not be delivered to client";
115+
errDescription = "Message could not be delivered to client because of a connection error";
145116
break;
146117
default:
147-
_errDescription = "Unknown error";
118+
errDescription = "Unknown error";
148119
break;
149120
}
150121
}
151122

152123
public String getClientId()
153124
{
154-
return _ctx.getClientId();
125+
return httpContext.getClientId();
155126
}
156127
}

0 commit comments

Comments
 (0)