You are viewing a plain text version of this content. The canonical link for it is here.
Posted to gitbox@activemq.apache.org by GitBox <gi...@apache.org> on 2021/03/03 22:54:29 UTC

[GitHub] [activemq-artemis] tabish121 commented on a change in pull request #3475: ARTEMIS-3133 Just Encapsulating ObjectPool into a small utility

tabish121 commented on a change in pull request #3475:
URL: https://github.com/apache/activemq-artemis/pull/3475#discussion_r586853364



##########
File path: artemis-commons/src/main/java/org/apache/activemq/artemis/utils/ObjectPool.java
##########
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.activemq.artemis.utils;
+
+import java.util.Queue;
+import java.util.function.Consumer;
+import java.util.function.Supplier;
+
+import io.netty.util.internal.PlatformDependent;
+
+/**
+ * A simple encapsulation of Netty MpscQueue to provide a pool of objects.
+ * @param <T>
+ */
+public class ObjectPool<T> {
+
+   private final Queue<T> internalPool;
+
+   private final Consumer<T> cleaner;
+   private final Supplier<T> supplier;
+
+   public ObjectPool(int maxSize, Consumer<T> cleaner, Supplier<T> supplier) {
+      if (maxSize > 0) {
+         internalPool = PlatformDependent.newFixedMpscQueue(maxSize);
+      } else {
+         internalPool = null;
+      }
+      this.cleaner = cleaner;
+      this.supplier = supplier;
+   }
+
+   /** Use this when you are getting a new Object. In a sense of loaning something, or check in into a hotel */
+   public T checkIn() {

Review comment:
       The naming here for the pooling APIs is pretty much the exact opposite of what you normally see in pool APIs (the fact that you had to add a note here on what checkIn means it was already unclear to the writer I'd guess).  Most of the pooling APIs tend to use terminology like "borrowObject" for the fetch from the pool API and something along the lines of "returnObject" for the return.  Or if you prefer treat it more like a queue and use "poll" and "offer" etc.  Or just swap the two you have now because anyone using this is going to need proper API docs and will have to read them each time to realize that checkIn is actually taking something out of the pool and vice versa. 




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org