1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portal.kernel.messaging;
16  
17  import com.liferay.portal.kernel.util.ThreadLocalRegistry;
18  
19  import java.util.Set;
20  import java.util.concurrent.ThreadPoolExecutor;
21  
22  /**
23   * <a href="SerialDestination.java.html"><b><i>View Source</i></b></a>
24   *
25   * <p>
26   * Destination that delivers a message to a list of message listeners one at a
27   * time.
28   * </p>
29   *
30   * @author Michael C. Han
31   */
32  public class SerialDestination extends BaseDestination {
33  
34      public SerialDestination() {
35          super();
36  
37          setWorkersCoreSize(_WORKERS_CORE_SIZE);
38          setWorkersMaxSize(_WORKERS_MAX_SIZE);
39      }
40  
41      /**
42       * @deprecated
43       */
44      public SerialDestination(String name) {
45          super(name, _WORKERS_CORE_SIZE, _WORKERS_MAX_SIZE);
46      }
47  
48      protected void dispatch(
49          final Set<MessageListener> messageListeners, final Message message) {
50  
51          ThreadPoolExecutor threadPoolExecutor = getThreadPoolExecutor();
52  
53          Runnable runnable = new MessageRunnable(message) {
54  
55              public void run() {
56                  try {
57                      for (MessageListener messageListener : messageListeners) {
58                          messageListener.receive(message);
59                      }
60                  }
61                  finally {
62                      ThreadLocalRegistry.resetThreadLocals();
63                  }
64              }
65  
66          };
67  
68          threadPoolExecutor.execute(runnable);
69      }
70  
71      private static final int _WORKERS_CORE_SIZE = 1;
72  
73      private static final int _WORKERS_MAX_SIZE = 1;
74  
75  }