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.backgroundtask;
016    
017    import com.liferay.portal.kernel.messaging.Message;
018    import com.liferay.portal.kernel.messaging.sender.SingleDestinationMessageSender;
019    import com.liferay.portal.kernel.messaging.sender.SingleDestinationMessageSenderFactory;
020    import com.liferay.portal.kernel.messaging.sender.SingleDestinationMessageSenderFactoryUtil;
021    import com.liferay.registry.dependency.ServiceDependencyListener;
022    import com.liferay.registry.dependency.ServiceDependencyManager;
023    
024    /**
025     * @author Andrew Betts
026     */
027    public class BackgroundTaskStatusMessageSenderImpl
028            implements BackgroundTaskStatusMessageSender {
029    
030            public void afterPropertiesSet() {
031                    ServiceDependencyManager serviceDependencyManager =
032                            new ServiceDependencyManager();
033    
034                    serviceDependencyManager.addServiceDependencyListener(
035                            new ServiceDependencyListener() {
036    
037                                    @Override
038                                    public void dependenciesFulfilled() {
039                                            _singleDestinationMessageSender =
040                                                    SingleDestinationMessageSenderFactoryUtil.
041                                                            createSingleDestinationMessageSender(
042                                                                    _destinationName);
043                                    }
044    
045                                    @Override
046                                    public void destroy() {
047                                    }
048    
049                            });
050    
051                    serviceDependencyManager.registerDependencies(
052                            SingleDestinationMessageSenderFactory.class);
053            }
054    
055            @Override
056            public void sendBackgroundTaskStatusMessage(Message message) {
057                    if (!BackgroundTaskThreadLocal.hasBackgroundTask()) {
058                            return;
059                    }
060    
061                    _singleDestinationMessageSender.send(message);
062            }
063    
064            public void setDestinationName(String destinationName) {
065                    _destinationName = destinationName;
066            }
067    
068            private String _destinationName;
069            private SingleDestinationMessageSender _singleDestinationMessageSender;
070    
071    }