1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.kernel.messaging;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.util.ConcurrentHashSet;
25  
26  import java.util.Iterator;
27  import java.util.Set;
28  import java.util.concurrent.ThreadPoolExecutor;
29  
30  /**
31   * <a href="IteratorDispatcherDestination.java.html"><b><i>View Source</i></b>
32   * </a>
33   *
34   * @author Michael C. Han
35   * @author Brian Wing Shun Chan
36   *
37   */
38  public abstract class IteratorDispatcherDestination extends BaseDestination {
39  
40      public IteratorDispatcherDestination(String name) {
41          super(name);
42      }
43  
44      public IteratorDispatcherDestination(
45          String name, int workersCoreSize, int workersMaxSize) {
46  
47          super(name, workersCoreSize, workersMaxSize);
48      }
49  
50      public void register(MessageListener listener) {
51          listener = new InvokerMessageListener(listener);
52  
53          _listeners.add(listener);
54  
55          setListenerCount(_listeners.size());
56      }
57  
58      public void send(Message message) {
59          if (_listeners.size() == 0) {
60              if (_log.isDebugEnabled()) {
61                  _log.debug("No listeners for destination " + getName());
62              }
63  
64              return;
65          }
66  
67          ThreadPoolExecutor threadPoolExecutor = getThreadPoolExecutor();
68  
69          if (threadPoolExecutor.isShutdown()) {
70              throw new IllegalStateException(
71                  "Destination " + getName() + " is shutdown and cannot " +
72                      "receive more messages");
73          }
74  
75          dispatch(_listeners.iterator(), message);
76      }
77  
78      public boolean unregister(MessageListener listener) {
79          listener = new InvokerMessageListener(listener);
80  
81          boolean value = _listeners.remove(listener);
82  
83          setListenerCount(_listeners.size());
84  
85          return value;
86      }
87  
88      protected abstract void dispatch(
89          Iterator<MessageListener> listenersItr, Message message);
90  
91      private static Log _log = LogFactoryUtil.getLog(BaseDestination.class);
92  
93      private Set<MessageListener> _listeners =
94          new ConcurrentHashSet<MessageListener>();
95  
96  }