001    /**
002     * Copyright (c) 2000-2013 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.sender;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.messaging.Destination;
020    import com.liferay.portal.kernel.messaging.Message;
021    import com.liferay.portal.kernel.messaging.MessageBus;
022    import com.liferay.portal.kernel.messaging.MessageBusException;
023    import com.liferay.portal.kernel.messaging.MessageListener;
024    import com.liferay.portal.kernel.messaging.MessageListenerException;
025    import com.liferay.portal.kernel.messaging.SynchronousDestination;
026    
027    import java.util.Set;
028    
029    /**
030     * @author Shuyang Zhou
031     */
032    public class DirectSynchronousMessageSender
033            implements SynchronousMessageSender {
034    
035            @Override
036            public Object send(String destinationName, Message message)
037                    throws MessageBusException {
038    
039                    Destination destination = _messageBus.getDestination(destinationName);
040    
041                    if (destination == null) {
042                            if (_log.isInfoEnabled()) {
043                                    _log.info(
044                                            "Destination " + destinationName + " is not configured");
045                            }
046    
047                            return null;
048                    }
049    
050                    if (destination.getMessageListenerCount() == 0) {
051                            if (_log.isInfoEnabled()) {
052                                    _log.info(
053                                            "Destination " + destinationName +
054                                                    " does not have any message listeners");
055                            }
056    
057                            return null;
058                    }
059    
060                    if (destination instanceof SynchronousDestination) {
061                            destination.send(message);
062                    }
063                    else {
064                            Set<MessageListener> messageListeners =
065                                    destination.getMessageListeners();
066    
067                            for (MessageListener messageListener : messageListeners) {
068                                    try {
069                                            messageListener.receive(message);
070                                    }
071                                    catch (MessageListenerException mle) {
072                                            throw new MessageBusException(mle);
073                                    }
074                            }
075                    }
076    
077                    return message.getResponse();
078            }
079    
080            @Override
081            public Object send(String destinationName, Message message, long timeout)
082                    throws MessageBusException {
083    
084                    if (_log.isWarnEnabled()) {
085                            _log.warn(
086                                    DirectSynchronousMessageSender.class.getName() +
087                                            " does not support timeout");
088                    }
089    
090                    return send(destinationName, message);
091            }
092    
093            public void setMessageBus(MessageBus messageBus) {
094                    _messageBus = messageBus;
095            }
096    
097            private static Log _log = LogFactoryUtil.getLog(
098                    DirectSynchronousMessageSender.class);
099    
100            private MessageBus _messageBus;
101    
102    }