You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by jb...@apache.org on 2020/05/12 07:40:54 UTC
[activemq] branch activemq-5.15.x updated: AMQ-7446 - Remove some
synchronized blocks in the MessageServlet
This is an automated email from the ASF dual-hosted git repository.
jbonofre pushed a commit to branch activemq-5.15.x
in repository https://gitbox.apache.org/repos/asf/activemq.git
The following commit(s) were added to refs/heads/activemq-5.15.x by this push:
new 9901218 AMQ-7446 - Remove some synchronized blocks in the MessageServlet
9901218 is described below
commit 9901218c5bfeed2765d7c8cc46bb956f7a8f3f9e
Author: Colm O hEigeartaigh <co...@apache.org>
AuthorDate: Thu Mar 19 17:33:03 2020 +0000
AMQ-7446 - Remove some synchronized blocks in the MessageServlet
(cherry picked from commit 8f6cfa4190a013c42dc30b261932a75605366a90)
---
.../org/apache/activemq/web/MessageServlet.java | 46 +++++++---------------
.../apache/activemq/web/MessageServletSupport.java | 2 +-
2 files changed, 15 insertions(+), 33 deletions(-)
diff --git a/activemq-web/src/main/java/org/apache/activemq/web/MessageServlet.java b/activemq-web/src/main/java/org/apache/activemq/web/MessageServlet.java
index 38546d7..2afd2ab 100644
--- a/activemq-web/src/main/java/org/apache/activemq/web/MessageServlet.java
+++ b/activemq-web/src/main/java/org/apache/activemq/web/MessageServlet.java
@@ -32,8 +32,9 @@ import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
-import java.util.HashMap;
-import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
/**
* A servlet for sending and receiving messages to/from JMS destinations using
@@ -68,8 +69,8 @@ public class MessageServlet extends MessageServletSupport {
private long requestTimeout = 1000;
private String defaultContentType;
- private final HashMap<String, WebClient> clients = new HashMap<String, WebClient>();
- private final HashSet<MessageAvailableConsumer> activeConsumers = new HashSet<MessageAvailableConsumer>();
+ private final Map<String, WebClient> clients = new ConcurrentHashMap<>();
+ private final Set<MessageAvailableConsumer> activeConsumers = ConcurrentHashMap.newKeySet();
@Override
public void init() throws ServletException {
@@ -107,7 +108,7 @@ public class MessageServlet extends MessageServletSupport {
String action = request.getParameter("action");
String clientId = request.getParameter("clientId");
- if (action != null && clientId != null && action.equals("unsubscribe")) {
+ if (clientId != null && "unsubscribe".equals(action)) {
LOG.info("Unsubscribing client " + clientId);
WebClient client = getWebClient(request);
client.close();
@@ -149,7 +150,7 @@ public class MessageServlet extends MessageServletSupport {
}
/**
- * Supports a HTTP DELETE to be equivalent of consuming a singe message
+ * Supports a HTTP DELETE to be equivalent of consuming a single message
* from a queue
*/
@Override
@@ -158,7 +159,7 @@ public class MessageServlet extends MessageServletSupport {
}
/**
- * Supports a HTTP DELETE to be equivalent of consuming a singe message
+ * Supports a HTTP DELETE to be equivalent of consuming a single message
* from a queue
*/
@Override
@@ -188,14 +189,8 @@ public class MessageServlet extends MessageServletSupport {
// Don't allow concurrent use of the consumer. Do make sure to allow
// subsequent calls on continuation to use the consumer.
- if (continuation.isInitial()) {
- synchronized (activeConsumers) {
- if (activeConsumers.contains(consumer)) {
- throw new ServletException("Concurrent access to consumer is not supported");
- } else {
- activeConsumers.add(consumer);
- }
- }
+ if (continuation.isInitial() && !activeConsumers.add(consumer)) {
+ throw new ServletException("Concurrent access to consumer is not supported");
}
Message message = null;
@@ -234,9 +229,7 @@ public class MessageServlet extends MessageServletSupport {
writeResponse(request, response, message);
closeConsumerOnOneShot(request, client, destination);
- synchronized (activeConsumers) {
- activeConsumers.remove(consumer);
- }
+ activeConsumers.remove(consumer);
}
} catch (JMSException e) {
throw new ServletException("Could not post JMS message: " + e, e);
@@ -260,9 +253,7 @@ public class MessageServlet extends MessageServletSupport {
}
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
closeConsumerOnOneShot(request, client, destination);
- synchronized (activeConsumers) {
- activeConsumers.remove(consumer);
- }
+ activeConsumers.remove(consumer);
return;
}
@@ -361,17 +352,8 @@ public class MessageServlet extends MessageServletSupport {
public WebClient getWebClient(HttpServletRequest request) {
String clientId = request.getParameter("clientId");
if (clientId != null) {
- synchronized (this) {
- LOG.debug("Getting local client [" + clientId + "]");
- WebClient client = clients.get(clientId);
- if (client == null) {
- LOG.debug("Creating new client [" + clientId + "]");
- client = new WebClient();
- clients.put(clientId, client);
- }
- return client;
- }
-
+ LOG.debug("Getting local client [" + clientId + "]");
+ return clients.computeIfAbsent(clientId, k -> new WebClient());
} else {
return WebClient.getWebClient(request);
}
diff --git a/activemq-web/src/main/java/org/apache/activemq/web/MessageServletSupport.java b/activemq-web/src/main/java/org/apache/activemq/web/MessageServletSupport.java
index 8529896..02e2b7a 100644
--- a/activemq-web/src/main/java/org/apache/activemq/web/MessageServletSupport.java
+++ b/activemq-web/src/main/java/org/apache/activemq/web/MessageServletSupport.java
@@ -345,7 +345,7 @@ public abstract class MessageServletSupport extends HttpServlet {
LOG.debug("Content-Type={}", contentType);
// lets read the message body instead
BufferedReader reader = request.getReader();
- StringBuffer buffer = new StringBuffer();
+ StringBuilder buffer = new StringBuilder();
while (true) {
String line = reader.readLine();
if (line == null) {