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  
56      public void send(Message message) {
57          if (_listeners.size() == 0) {
58              if (_log.isDebugEnabled()) {
59                  _log.debug("No listeners for destination " + getName());
60              }
61  
62              return;
63          }
64  
65          ThreadPoolExecutor threadPoolExecutor = getThreadPoolExecutor();
66  
67          if (threadPoolExecutor.isShutdown()) {
68              throw new IllegalStateException(
69                  "Destination " + getName() + " is shutdown and cannot " +
70                      "receive more messages");
71          }
72  
73          dispatch(_listeners.iterator(), message);
74      }
75  
76      public boolean unregister(MessageListener listener) {
77          listener = new InvokerMessageListener(listener);
78  
79          return _listeners.remove(listener);
80      }
81  
82      public int getListenerCount() {
83          return _listeners.size();
84      }
85  
86      protected abstract void dispatch(
87          Iterator<MessageListener> listenersItr, Message message);
88  
89      private static Log _log =
90          LogFactoryUtil.getLog(IteratorDispatcherDestination.class);
91  
92      private Set<MessageListener> _listeners =
93          new ConcurrentHashSet<MessageListener>();
94  
95  }