From f36792be935ed95dd4d46ccce2d007f7776400de Mon Sep 17 00:00:00 2001 From: SpeedProg Date: Fri, 8 May 2020 21:29:42 +0200 Subject: [PATCH] make jboss remoting2 work with openjdk11 --- .../main/java/mage/interfaces/MageServer.java | 11 +++---- .../src/main/java/mage/remote/Connection.java | 5 +-- .../java/mage/remote/CustomThreadPool.java | 32 +++++++++++++++++++ .../main/java/mage/remote/SessionImpl.java | 10 +++--- .../main/java/mage/server/MageServerImpl.java | 32 +++++++++---------- 5 files changed, 61 insertions(+), 29 deletions(-) create mode 100644 Mage.Common/src/main/java/mage/remote/CustomThreadPool.java diff --git a/Mage.Common/src/main/java/mage/interfaces/MageServer.java b/Mage.Common/src/main/java/mage/interfaces/MageServer.java index 52c95550c5..059f21ce63 100644 --- a/Mage.Common/src/main/java/mage/interfaces/MageServer.java +++ b/Mage.Common/src/main/java/mage/interfaces/MageServer.java @@ -16,7 +16,6 @@ import mage.utils.MageVersion; import mage.view.*; import java.util.List; -import java.util.Optional; import java.util.Set; import java.util.UUID; @@ -84,7 +83,7 @@ public interface MageServer { boolean isTableOwner(String sessionId, UUID roomId, UUID tableId) throws MageException; - Optional getTable(UUID roomId, UUID tableId) throws MageException; + TableView getTable(UUID roomId, UUID tableId) throws MageException; List getTables(UUID roomId) throws MageException; @@ -95,13 +94,13 @@ public interface MageServer { void leaveChat(UUID chatId, String sessionId) throws MageException; - Optional getTableChatId(UUID tableId) throws MageException; + UUID getTableChatId(UUID tableId) throws MageException; - Optional getGameChatId(UUID gameId) throws MageException; + UUID getGameChatId(UUID gameId) throws MageException; - Optional getRoomChatId(UUID roomId) throws MageException; + UUID getRoomChatId(UUID roomId) throws MageException; - Optional getTournamentChatId(UUID tournamentId) throws MageException; + UUID getTournamentChatId(UUID tournamentId) throws MageException; //room methods UUID getMainRoomId() throws MageException; diff --git a/Mage.Common/src/main/java/mage/remote/Connection.java b/Mage.Common/src/main/java/mage/remote/Connection.java index ecc7dd4416..f3bf4296ee 100644 --- a/Mage.Common/src/main/java/mage/remote/Connection.java +++ b/Mage.Common/src/main/java/mage/remote/Connection.java @@ -42,6 +42,7 @@ public class Connection { // private UserSkipPrioritySteps userSkipPrioritySteps; private static final String serialization = "?serializationtype=jboss"; private static final String transport = "bisocket"; + private static final String threadpool = "onewayThreadPool=mage.remote.CustomThreadPool"; private final String parameter; @@ -78,13 +79,13 @@ public class Connection { try { InetAddress inet = getLocalAddress(); if (inet != null) { - return transport + "://" + inet.getHostAddress() + ':' + port + '/' + serialization + parameter; + return transport + "://" + inet.getHostAddress() + ':' + port + '/' + serialization + "&" + threadpool + parameter; } } catch (SocketException ex) { // just use localhost if can't find local ip } } - return transport + "://" + host + ':' + port + '/' + serialization + parameter; + return transport + "://" + host + ':' + port + '/' + serialization + "&" + threadpool + parameter; } public ProxyType getProxyType() { diff --git a/Mage.Common/src/main/java/mage/remote/CustomThreadPool.java b/Mage.Common/src/main/java/mage/remote/CustomThreadPool.java new file mode 100644 index 0000000000..456f47bcd7 --- /dev/null +++ b/Mage.Common/src/main/java/mage/remote/CustomThreadPool.java @@ -0,0 +1,32 @@ +package mage.remote; + +import java.lang.reflect.Field; +import java.util.concurrent.ThreadPoolExecutor; + +import org.apache.log4j.Logger; +import org.jboss.util.threadpool.BasicThreadPool; + +public class CustomThreadPool extends BasicThreadPool { + private static final Logger logger = Logger.getLogger(SessionImpl.class); + + @Override + public void setMaximumPoolSize(int size) { + /* + * I really don't want to implement a whole new threadpool + * just to fix this and the executor is private + */ + try { + Field executorField = BasicThreadPool.class.getField("executor"); + executorField.setAccessible(true); + ThreadPoolExecutor executor = (ThreadPoolExecutor) executorField.get(this); + synchronized (executor) { + executor.setMaximumPoolSize(size); + executor.setCorePoolSize(size); + } + } catch (NoSuchFieldException | SecurityException e) { + logger.error("Failed to get field executor from BasicThreadPool", e); + } catch (IllegalArgumentException | IllegalAccessException e) { + logger.error("Failed to get executor object from BasicThreadPool", e); + } + } +} \ No newline at end of file diff --git a/Mage.Common/src/main/java/mage/remote/SessionImpl.java b/Mage.Common/src/main/java/mage/remote/SessionImpl.java index dd073af9a9..861ad752ec 100644 --- a/Mage.Common/src/main/java/mage/remote/SessionImpl.java +++ b/Mage.Common/src/main/java/mage/remote/SessionImpl.java @@ -647,7 +647,7 @@ public class SessionImpl implements Session { public Optional getRoomChatId(UUID roomId) { try { if (isConnected()) { - return server.getRoomChatId(roomId); + return Optional.of(server.getRoomChatId(roomId)); } } catch (MageException ex) { handleMageException(ex); @@ -659,7 +659,7 @@ public class SessionImpl implements Session { public Optional getTableChatId(UUID tableId) { try { if (isConnected()) { - return server.getTableChatId(tableId); + return Optional.of(server.getTableChatId(tableId)); } } catch (MageException ex) { handleMageException(ex); @@ -671,7 +671,7 @@ public class SessionImpl implements Session { public Optional getGameChatId(UUID gameId) { try { if (isConnected()) { - return server.getGameChatId(gameId); + return Optional.of(server.getGameChatId(gameId)); } } catch (MageException ex) { handleMageException(ex); @@ -685,7 +685,7 @@ public class SessionImpl implements Session { public Optional getTable(UUID roomId, UUID tableId) { try { if (isConnected()) { - return server.getTable(roomId, tableId); + return Optional.of(server.getTable(roomId, tableId)); } } catch (MageException ex) { handleMageException(ex); @@ -829,7 +829,7 @@ public class SessionImpl implements Session { public Optional getTournamentChatId(UUID tournamentId) { try { if (isConnected()) { - return server.getTournamentChatId(tournamentId); + return Optional.of(server.getTournamentChatId(tournamentId)); } } catch (MageException ex) { handleMageException(ex); diff --git a/Mage.Server/src/main/java/mage/server/MageServerImpl.java b/Mage.Server/src/main/java/mage/server/MageServerImpl.java index 386c8c2a66..9387231256 100644 --- a/Mage.Server/src/main/java/mage/server/MageServerImpl.java +++ b/Mage.Server/src/main/java/mage/server/MageServerImpl.java @@ -397,15 +397,15 @@ public class MageServerImpl implements MageServer { @Override //FIXME: why no sessionId here??? - public Optional getTable(UUID roomId, UUID tableId) throws MageException { + public TableView getTable(UUID roomId, UUID tableId) throws MageException { try { Optional room = GamesRoomManager.instance.getRoom(roomId); - return room.flatMap(r -> r.getTable(tableId)); + return room.flatMap(r -> r.getTable(tableId)).orElse(null); } catch (Exception ex) { handleException(ex); } - return Optional.empty(); + return null; } @Override @@ -536,18 +536,18 @@ public class MageServerImpl implements MageServer { @Override //FIXME: why no sessionId here??? - public Optional getRoomChatId(UUID roomId) throws MageException { + public UUID getRoomChatId(UUID roomId) throws MageException { try { Optional room = GamesRoomManager.instance.getRoom(roomId); if (!room.isPresent()) { logger.error("roomId not found : " + roomId); - return Optional.empty(); + return null; } - return Optional.of(room.get().getChatId()); + return room.get().getChatId(); } catch (Exception ex) { handleException(ex); } - return Optional.empty(); + return null; } @Override @@ -602,13 +602,13 @@ public class MageServerImpl implements MageServer { @Override //FIXME: why no sessionId here??? - public Optional getTableChatId(UUID tableId) throws MageException { + public UUID getTableChatId(UUID tableId) throws MageException { try { - return TableManager.instance.getChatId(tableId); + return TableManager.instance.getChatId(tableId).orElse(null); } catch (Exception ex) { handleException(ex); } - return Optional.empty(); + return null; } @Override @@ -646,24 +646,24 @@ public class MageServerImpl implements MageServer { @Override //FIXME: why no sessionId here??? - public Optional getGameChatId(UUID gameId) throws MageException { + public UUID getGameChatId(UUID gameId) throws MageException { try { - return GameManager.instance.getChatId(gameId); + return GameManager.instance.getChatId(gameId).orElse(null); } catch (Exception ex) { handleException(ex); } - return Optional.empty(); + return null; } @Override //FIXME: why no sessionId here??? - public Optional getTournamentChatId(UUID tournamentId) throws MageException { + public UUID getTournamentChatId(UUID tournamentId) throws MageException { try { - return TournamentManager.instance.getChatId(tournamentId); + return TournamentManager.instance.getChatId(tournamentId).orElse(null); } catch (Exception ex) { handleException(ex); } - return Optional.empty(); + return null; } @Override