Skip to content

The Unified and Autonomous Development Platform

Call sendStringByFuture in websocket to avoid the Jetty web socket lock failure issue

robin shinece0669a43cc4
diff --git a/commons.wicket/src/main/java/com/pmease/commons/wicket/websocket/DefaultWebSocketManager.java b/commons.wicket/src/main/java/com/pmease/commons/wicket/websocket/DefaultWebSocketManager.java
index 986162b5da..c397ae71af 100644
--- a/commons.wicket/src/main/java/com/pmease/commons/wicket/websocket/DefaultWebSocketManager.java
+++ b/commons.wicket/src/main/java/com/pmease/commons/wicket/websocket/DefaultWebSocketManager.java
@@ -1,153 +1,132 @@
-package com.pmease.commons.wicket.websocket;
-
-import java.io.IOException;
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.TimeUnit;
-
-import javax.annotation.Nullable;
-import javax.inject.Inject;
-import javax.inject.Singleton;
-
-import org.apache.wicket.Application;
-import org.apache.wicket.protocol.ws.api.IWebSocketConnection;
-import org.apache.wicket.protocol.ws.api.registry.IWebSocketConnectionRegistry;
-import org.apache.wicket.protocol.ws.api.registry.SimpleWebSocketConnectionRegistry;
-import org.eclipse.jetty.websocket.api.WebSocketPolicy;
-import org.hibernate.resource.transaction.spi.TransactionStatus;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.pmease.commons.hibernate.Sessional;
-import com.pmease.commons.hibernate.dao.Dao;
-import com.pmease.commons.wicket.page.CommonPage;
-
-@Singleton
-public class DefaultWebSocketManager implements WebSocketManager {
-
- private static final Logger logger = LoggerFactory.getLogger(DefaultWebSocketManager.class);
-
- private final Application application;
-
- private final Dao dao;
-
- private final ExecutorService executorService;
-
- private final ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
-
- private final WebSocketPolicy webSocketPolicy;
-
- private final Map<PageKey, Collection<WebSocketRegion>> regions = new ConcurrentHashMap<>();
-
- private final IWebSocketConnectionRegistry connectionRegistry = new SimpleWebSocketConnectionRegistry();
-
- @Inject
- public DefaultWebSocketManager(Application application, Dao dao,
- ExecutorService executorService, WebSocketPolicy webSocketPolicy) {
- this.application = application;
- this.dao = dao;
- this.executorService = executorService;
- this.webSocketPolicy = webSocketPolicy;
- }
-
- @Override
- public void onRegionChange(CommonPage page) {
- regions.put(new PageKey(page), page.getWebSocketRegions());
- }
-
- @Override
- public void onDestroySession(String sessionId) {
- for (Iterator<Map.Entry<PageKey, Collection<WebSocketRegion>>> it = regions.entrySet().iterator(); it.hasNext();) {
- if (it.next().getKey().getSessionId().equals(sessionId))
- it.remove();
- }
- }
-
- @Sessional
- @Override
- public void renderAsync(WebSocketRegion region, @Nullable PageKey sourcePageKey) {
- if (dao.getSession().getTransaction().getStatus() == TransactionStatus.ACTIVE) {
- dao.doAsyncAfterCommit(new Runnable() {
-
- @Override
- public void run() {
- render(region, sourcePageKey);
- }
-
- });
- } else {
- executorService.execute(new Runnable() {
-
- @Override
- public void run() {
- render(region, sourcePageKey);
- }
-
- });
- }
- }
-
- @Override
- public void render(WebSocketRegion region, @Nullable PageKey sourcePageKey) {
- try {
- for (IWebSocketConnection connection: connectionRegistry.getConnections(application)) {
- synchronized (connection) {
- PageKey pageKey = ((WebSocketConnection) connection).getPageKey();
- if (connection.isOpen()
- && (sourcePageKey == null || !sourcePageKey.equals(pageKey))
- && containsRegion(connection, region)) {
- connection.sendMessage(RENDER_CALLBACK);
- }
- }
- }
- } catch (Exception e) {
- logger.error("Error sending websocket message", e);
- }
- }
-
- private boolean containsRegion(IWebSocketConnection connection, WebSocketRegion region) {
- PageKey pageKey = ((WebSocketConnection)connection).getPageKey();
- Collection<WebSocketRegion> connectionRegions = regions.get(pageKey);
- if (connectionRegions != null) {
- for (WebSocketRegion connectionRegion: connectionRegions) {
- if (connectionRegion.contains(region)) {
- return true;
- }
- }
- }
- return false;
- }
-
- @Override
- public void start() {
- scheduledExecutorService.scheduleWithFixedDelay(new Runnable() {
-
- @Override
- public void run() {
- for (IWebSocketConnection connection: new SimpleWebSocketConnectionRegistry().getConnections(application)) {
- synchronized (connection) {
- if (connection.isOpen()) {
- try {
- connection.sendMessage(WebSocketManager.KEEP_ALIVE);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- }
- }
- }
-
- }, 0, webSocketPolicy.getIdleTimeout()/2, TimeUnit.MILLISECONDS);
- }
-
- @Override
- public void stop() {
- scheduledExecutorService.shutdown();
- }
-
-}
+package com.pmease.commons.wicket.websocket;
+
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+import javax.annotation.Nullable;
+import javax.inject.Inject;
+import javax.inject.Singleton;
+
+import org.apache.wicket.Application;
+import org.apache.wicket.protocol.ws.api.IWebSocketConnection;
+import org.apache.wicket.protocol.ws.api.registry.IWebSocketConnectionRegistry;
+import org.apache.wicket.protocol.ws.api.registry.SimpleWebSocketConnectionRegistry;
+import org.eclipse.jetty.websocket.api.WebSocketPolicy;
+import org.hibernate.resource.transaction.spi.TransactionStatus;
+
+import com.pmease.commons.hibernate.Sessional;
+import com.pmease.commons.hibernate.dao.Dao;
+import com.pmease.commons.wicket.page.CommonPage;
+
+@Singleton
+public class DefaultWebSocketManager implements WebSocketManager {
+
+ private final Application application;
+
+ private final Dao dao;
+
+ private final ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
+
+ private final WebSocketPolicy webSocketPolicy;
+
+ private final Map<PageKey, Collection<WebSocketRegion>> regions = new ConcurrentHashMap<>();
+
+ private final IWebSocketConnectionRegistry connectionRegistry = new SimpleWebSocketConnectionRegistry();
+
+ @Inject
+ public DefaultWebSocketManager(Application application, Dao dao, WebSocketPolicy webSocketPolicy) {
+ this.application = application;
+ this.dao = dao;
+ this.webSocketPolicy = webSocketPolicy;
+ }
+
+ @Override
+ public void onRegionChange(CommonPage page) {
+ regions.put(new PageKey(page), page.getWebSocketRegions());
+ }
+
+ @Override
+ public void onDestroySession(String sessionId) {
+ for (Iterator<Map.Entry<PageKey, Collection<WebSocketRegion>>> it = regions.entrySet().iterator(); it.hasNext();) {
+ if (it.next().getKey().getSessionId().equals(sessionId))
+ it.remove();
+ }
+ }
+
+ @Sessional
+ @Override
+ public void render(WebSocketRegion region, @Nullable PageKey sourcePageKey) {
+ if (dao.getSession().getTransaction().getStatus() == TransactionStatus.ACTIVE) {
+ dao.doAfterCommit(new Runnable() {
+
+ @Override
+ public void run() {
+ doRender(region, sourcePageKey);
+ }
+
+ });
+ } else {
+ doRender(region, sourcePageKey);
+ }
+ }
+
+ private void doRender(WebSocketRegion region, @Nullable PageKey sourcePageKey) {
+ for (IWebSocketConnection connection: connectionRegistry.getConnections(application)) {
+ PageKey pageKey = ((WebSocketConnection) connection).getPageKey();
+ if (connection.isOpen()
+ && (sourcePageKey == null || !sourcePageKey.equals(pageKey))
+ && containsRegion(connection, region)) {
+ try {
+ connection.sendMessage(RENDER_CALLBACK);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ }
+ }
+
+ private boolean containsRegion(IWebSocketConnection connection, WebSocketRegion region) {
+ PageKey pageKey = ((WebSocketConnection)connection).getPageKey();
+ Collection<WebSocketRegion> connectionRegions = regions.get(pageKey);
+ if (connectionRegions != null) {
+ for (WebSocketRegion connectionRegion: connectionRegions) {
+ if (connectionRegion.contains(region)) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ @Override
+ public void start() {
+ scheduledExecutorService.scheduleWithFixedDelay(new Runnable() {
+
+ @Override
+ public void run() {
+ for (IWebSocketConnection connection: new SimpleWebSocketConnectionRegistry().getConnections(application)) {
+ if (connection.isOpen()) {
+ try {
+ connection.sendMessage(WebSocketManager.KEEP_ALIVE);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ }
+ }
+
+ }, 0, webSocketPolicy.getIdleTimeout()/2, TimeUnit.MILLISECONDS);
+ }
+
+ @Override
+ public void stop() {
+ scheduledExecutorService.shutdown();
+ }
+
+}
diff --git a/commons.wicket/src/main/java/com/pmease/commons/wicket/websocket/WebSocketConnection.java b/commons.wicket/src/main/java/com/pmease/commons/wicket/websocket/WebSocketConnection.java
index cb45369c61..7cf137354d 100644
--- a/commons.wicket/src/main/java/com/pmease/commons/wicket/websocket/WebSocketConnection.java
+++ b/commons.wicket/src/main/java/com/pmease/commons/wicket/websocket/WebSocketConnection.java
@@ -73,7 +73,7 @@ public class WebSocketConnection extends AbstractWebSocketConnection
{
checkClosed();
- session.getRemote().sendString(message);
+ session.getRemote().sendStringByFuture(message);
return this;
}
@@ -84,7 +84,7 @@ public class WebSocketConnection extends AbstractWebSocketConnection
checkClosed();
ByteBuffer buf = ByteBuffer.wrap(message, offset, length);
- session.getRemote().sendBytes(buf);
+ session.getRemote().sendBytesByFuture(buf);
return this;
}
diff --git a/commons.wicket/src/main/java/com/pmease/commons/wicket/websocket/WebSocketManager.java b/commons.wicket/src/main/java/com/pmease/commons/wicket/websocket/WebSocketManager.java
index 360547cc5c..0914370877 100644
--- a/commons.wicket/src/main/java/com/pmease/commons/wicket/websocket/WebSocketManager.java
+++ b/commons.wicket/src/main/java/com/pmease/commons/wicket/websocket/WebSocketManager.java
@@ -1,29 +1,27 @@
-package com.pmease.commons.wicket.websocket;
-
-import javax.annotation.Nullable;
-
-import com.pmease.commons.wicket.page.CommonPage;
-
-public interface WebSocketManager {
-
- static final String ERROR_MESSAGE = "ErrorMessage";
-
- static final String RENDER_CALLBACK = "RenderCallback";
-
- static final String CONNECT_CALLBACK = "ConnectCallback";
-
- static final String KEEP_ALIVE = "KeepAlive";
-
- void onRegionChange(CommonPage page);
-
- void onDestroySession(String sessionId);
-
- void render(WebSocketRegion region, @Nullable PageKey sourcePageKey);
-
- void renderAsync(WebSocketRegion region, @Nullable PageKey sourcePageKey);
-
- void start();
-
- void stop();
-
-}
+package com.pmease.commons.wicket.websocket;
+
+import javax.annotation.Nullable;
+
+import com.pmease.commons.wicket.page.CommonPage;
+
+public interface WebSocketManager {
+
+ static final String ERROR_MESSAGE = "ErrorMessage";
+
+ static final String RENDER_CALLBACK = "RenderCallback";
+
+ static final String CONNECT_CALLBACK = "ConnectCallback";
+
+ static final String KEEP_ALIVE = "KeepAlive";
+
+ void onRegionChange(CommonPage page);
+
+ void onDestroySession(String sessionId);
+
+ void render(WebSocketRegion region, @Nullable PageKey sourcePageKey);
+
+ void start();
+
+ void stop();
+
+}
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/test/TestPage.html b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/test/TestPage.html
index b47194918b..795b443f3a 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/test/TestPage.html
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/test/TestPage.html
@@ -1,2 +1,3 @@
<wicket:extend>
+ <a wicket:id="test">test</a>
</wicket:extend>
\ No newline at end of file
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/test/TestPage.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/test/TestPage.java
index c69c96280a..b0cb500a9b 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/test/TestPage.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/test/TestPage.java
@@ -1,6 +1,7 @@
package com.pmease.gitplex.web.page.test;
import org.apache.wicket.markup.head.IHeaderResponse;
+import org.apache.wicket.markup.html.link.Link;
import com.pmease.commons.wicket.page.CommonPage;
@@ -10,6 +11,14 @@ public class TestPage extends CommonPage {
@Override
protected void onInitialize() {
super.onInitialize();
+
+ add(new Link<Void>("test") {
+
+ @Override
+ public void onClick() {
+ }
+
+ });
}
@Override
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/websocket/CodeCommentChangeBroadcaster.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/websocket/CodeCommentChangeBroadcaster.java
index 259461dfda..26abc309a8 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/websocket/CodeCommentChangeBroadcaster.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/websocket/CodeCommentChangeBroadcaster.java
@@ -23,7 +23,7 @@ public class CodeCommentChangeBroadcaster {
public void on(CodeCommentEvent event) {
CodeCommentChangedRegion region = new CodeCommentChangedRegion(event.getComment().getId());
PageKey sourcePageKey = WicketUtils.getPageKey();
- webSocketManager.renderAsync(region, sourcePageKey);
+ webSocketManager.render(region, sourcePageKey);
}
}
\ No newline at end of file
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/websocket/CommitIndexedBroadcaster.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/websocket/CommitIndexedBroadcaster.java
index 687d80a90b..3b7e3f49f4 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/websocket/CommitIndexedBroadcaster.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/websocket/CommitIndexedBroadcaster.java
@@ -20,7 +20,7 @@ public class CommitIndexedBroadcaster {
@Listen
public void on(CommitIndexed event) {
CommitIndexedRegion region = new CommitIndexedRegion(event.getDepot().getId(), event.getCommitId());
- webSocketManager.renderAsync(region, null);
+ webSocketManager.render(region, null);
}
}
\ No newline at end of file
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/websocket/PullRequestChangeBroadcaster.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/websocket/PullRequestChangeBroadcaster.java
index 1d37a53b30..1dc7d2bef5 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/websocket/PullRequestChangeBroadcaster.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/websocket/PullRequestChangeBroadcaster.java
@@ -24,7 +24,7 @@ public class PullRequestChangeBroadcaster {
PullRequestChangedRegion region = new PullRequestChangedRegion(event.getRequest().getId());
PageKey sourcePageKey = WicketUtils.getPageKey();
- webSocketManager.renderAsync(region, sourcePageKey);
+ webSocketManager.render(region, sourcePageKey);
}
}
\ No newline at end of file