001    /**
002     * Copyright (c) 2000-present 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.DestinationNames;
021    import com.liferay.portal.kernel.messaging.Message;
022    import com.liferay.portal.kernel.messaging.MessageBus;
023    import com.liferay.portal.kernel.messaging.MessageBusException;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.kernel.uuid.PortalUUID;
026    
027    /**
028     * @author Michael C. Han
029     */
030    public class DefaultSynchronousMessageSender
031            implements SynchronousMessageSender {
032    
033            public DefaultSynchronousMessageSender() {
034            }
035    
036            /**
037             * @deprecated As of 6.1.0
038             */
039            @Deprecated
040            public DefaultSynchronousMessageSender(
041                    MessageBus messageBus, PortalUUID portalUUID, long timeout) {
042    
043                    _messageBus = messageBus;
044                    _portalUUID = portalUUID;
045                    _timeout = timeout;
046            }
047    
048            @Override
049            public Object send(String destinationName, Message message)
050                    throws MessageBusException {
051    
052                    return send(destinationName, message, _timeout);
053            }
054    
055            @Override
056            public Object send(String destinationName, Message message, long timeout)
057                    throws MessageBusException {
058    
059                    Destination destination = _messageBus.getDestination(destinationName);
060    
061                    if (destination == null) {
062                            if (_log.isInfoEnabled()) {
063                                    _log.info(
064                                            "Destination " + destinationName + " is not configured");
065                            }
066    
067                            return null;
068                    }
069    
070                    if (destination.getMessageListenerCount() == 0) {
071                            if (_log.isInfoEnabled()) {
072                                    _log.info(
073                                            "Destination " + destinationName +
074                                                    " does not have any message listeners");
075                            }
076    
077                            return null;
078                    }
079    
080                    message.setDestinationName(destinationName);
081    
082                    String responseDestinationName = message.getResponseDestinationName();
083    
084                    // Create a temporary destination if no response destination is
085                    // configured
086    
087                    if (Validator.isNull(responseDestinationName) ||
088                            !_messageBus.hasDestination(responseDestinationName)) {
089    
090                            if (_log.isDebugEnabled()) {
091                                    _log.debug(
092                                            "Response destination " + responseDestinationName +
093                                                    " is not configured");
094                            }
095    
096                            message.setResponseDestinationName(
097                                    DestinationNames.MESSAGE_BUS_DEFAULT_RESPONSE);
098                    }
099    
100                    String responseId = _portalUUID.generate();
101    
102                    message.setResponseId(responseId);
103    
104                    SynchronousMessageListener synchronousMessageListener =
105                            new SynchronousMessageListener(_messageBus, message, timeout);
106    
107                    return synchronousMessageListener.send();
108            }
109    
110            public void setMessageBus(MessageBus messageBus) {
111                    _messageBus = messageBus;
112            }
113    
114            public void setPortalUUID(PortalUUID portalUUID) {
115                    _portalUUID = portalUUID;
116            }
117    
118            public void setTimeout(long timeout) {
119                    _timeout = timeout;
120            }
121    
122            private static final Log _log = LogFactoryUtil.getLog(
123                    DefaultSynchronousMessageSender.class);
124    
125            private MessageBus _messageBus;
126            private PortalUUID _portalUUID;
127            private long _timeout;
128    
129    }