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.cluster.messaging;
016    
017    import com.liferay.portal.kernel.cluster.Address;
018    import com.liferay.portal.kernel.cluster.ClusterInvokeThreadLocal;
019    import com.liferay.portal.kernel.cluster.ClusterLinkUtil;
020    import com.liferay.portal.kernel.cluster.Priority;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.kernel.messaging.BaseMessageListener;
024    import com.liferay.portal.kernel.messaging.Message;
025    
026    /**
027     * @author Shuyang Zhou
028     */
029    public class ClusterBridgeMessageListener extends BaseMessageListener {
030    
031            public void setPriority(Priority priority) {
032                    _priority = priority;
033            }
034    
035            @Override
036            protected void doReceive(Message message) throws Exception {
037                    if (!ClusterInvokeThreadLocal.isEnabled()) {
038                            return;
039                    }
040    
041                    Address address = ClusterLinkUtil.getAddress(message);
042    
043                    if (address == null) {
044                            if (_log.isInfoEnabled()) {
045                                    _log.info("Bridging cluster link multicast message " + message);
046                            }
047    
048                            ClusterLinkUtil.sendMulticastMessage(message, _priority);
049                    }
050                    else {
051                            if (_log.isInfoEnabled()) {
052                                    _log.info(
053                                            "Bridging cluster link unicast message " + message +
054                                                    " to " + address);
055                            }
056    
057                            ClusterLinkUtil.sendUnicastMessage(address, message, _priority);
058                    }
059            }
060    
061            private static final Log _log = LogFactoryUtil.getLog(
062                    ClusterBridgeMessageListener.class);
063    
064            private Priority _priority;
065    
066    }