001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.messaging;
016    
017    import com.liferay.portal.kernel.util.ThreadLocalRegistry;
018    
019    import java.util.Set;
020    import java.util.concurrent.ThreadPoolExecutor;
021    
022    /**
023     * <p>
024     * Destination that delivers a message to a list of message listeners one at a
025     * time.
026     * </p>
027     *
028     * @author Michael C. Han
029     */
030    public class SerialDestination extends BaseDestination {
031    
032            public SerialDestination() {
033            }
034    
035            /**
036             * @deprecated
037             */
038            public SerialDestination(String name) {
039                    super(name, _WORKERS_CORE_SIZE, _WORKERS_MAX_SIZE);
040            }
041    
042            protected void dispatch(
043                    final Set<MessageListener> messageListeners, final Message message) {
044    
045                    ThreadPoolExecutor threadPoolExecutor = getThreadPoolExecutor();
046    
047                    Runnable runnable = new Runnable() {
048    
049                            public void run() {
050                                    try {
051                                            for (MessageListener messageListener : messageListeners) {
052                                                    messageListener.receive(message);
053                                            }
054                                    }
055                                    finally {
056                                            ThreadLocalRegistry.resetThreadLocals();
057                                    }
058                            }
059    
060                    };
061    
062                    threadPoolExecutor.execute(runnable);
063            }
064    
065            private static final int _WORKERS_CORE_SIZE = 1;
066    
067            private static final int _WORKERS_MAX_SIZE = 1;
068    
069    }