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