1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.messaging;
24  
25  import com.liferay.portal.kernel.messaging.sender.MessageSender;
26  import com.liferay.portal.kernel.messaging.sender.SynchronousMessageSender;
27  
28  /**
29   * <a href="MessageBusUtil.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Michael C. Han
32   *
33   */
34  public class MessageBusUtil {
35  
36      public static void addDestination(Destination destination) {
37          _instance._addDestination(destination);
38      }
39  
40      public static Message createResponseMessage(Message requestMessage) {
41  
42          Message response = new Message();
43          response.setDestination(requestMessage.getResponseDestination());
44          response.setResponseId(requestMessage.getResponseId());
45  
46          return response;
47      }
48  
49      public static Message createResponseMessage(
50          Message requestMessage, Object payload) {
51  
52          Message response = createResponseMessage(requestMessage);
53          response.setPayload(payload);
54  
55          return response;
56      }
57  
58      public static MessageBus getMessageBus() {
59          return _instance._messageBus;
60      }
61  
62      public static MessageSender getMessageSender() {
63          return _instance._messageSender;
64      }
65  
66      public static boolean hasMessageListener(String destination) {
67          return _instance._hasMessageListener(destination);
68      }
69  
70      public static void init(
71          MessageBus messageBus, MessageSender messageSender,
72          SynchronousMessageSender synchronousMessageSender) {
73  
74          _instance._init(messageBus, messageSender, synchronousMessageSender);
75      }
76  
77      public static void registerMessageListener(
78          String destination, MessageListener listener) {
79  
80          _instance._registerMessageListener(destination, listener);
81      }
82  
83      public static void removeDestination(String destination) {
84          _instance._removeDestination(destination);
85      }
86  
87      public static void sendMessage(String destination, Message message) {
88          _instance._sendMessage(destination, message);
89      }
90  
91      public static void sendMessage(String destination, Object payload) {
92          _instance._sendMessage(destination, payload);
93      }
94  
95      public static Object sendSynchronousMessage(
96              String destination, Message message)
97          throws MessageBusException {
98  
99          return _instance._sendSynchronousMessage(destination, message);
100     }
101 
102     public static Object sendSynchronousMessage(
103             String destination, Message message, long timeout)
104         throws MessageBusException {
105 
106         return _instance._sendSynchronousMessage(destination, message, timeout);
107     }
108 
109     public static Object sendSynchronousMessage(
110             String destination, Object payload)
111         throws MessageBusException {
112 
113         return _instance._sendSynchronousMessage(destination, payload, null);
114     }
115 
116     public static Object sendSynchronousMessage(
117             String destination, Object payload, long timeout)
118         throws MessageBusException {
119 
120         return _instance._sendSynchronousMessage(
121             destination, payload, null, timeout);
122     }
123 
124     public static Object sendSynchronousMessage(
125             String destination, Object payload, String responseDestination)
126         throws MessageBusException {
127 
128         return _instance._sendSynchronousMessage(
129             destination, payload, responseDestination);
130     }
131 
132     public static Object sendSynchronousMessage(
133             String destination, Object payload, String responseDestination,
134             long timeout)
135         throws MessageBusException {
136 
137         return _instance._sendSynchronousMessage(
138             destination, payload, responseDestination, timeout);
139     }
140 
141     public static boolean unregisterMessageListener(
142         String destination, MessageListener listener) {
143 
144         return _instance._unregisterMessageListener(destination, listener);
145     }
146 
147     private MessageBusUtil() {
148     }
149 
150     private void _addDestination(Destination destination) {
151         _messageBus.addDestination(destination);
152     }
153 
154     private boolean _hasMessageListener(String destination) {
155         return _messageBus.hasMessageListener(destination);
156     }
157 
158     private void _init(
159         MessageBus messageBus, MessageSender messageSender,
160         SynchronousMessageSender synchronousMessageSender) {
161 
162         _messageBus = messageBus;
163         _messageSender = messageSender;
164         _synchronousMessageSender = synchronousMessageSender;
165     }
166 
167     private void _registerMessageListener(
168         String destination, MessageListener listener) {
169 
170         _messageBus.registerMessageListener(destination, listener);
171     }
172 
173     private void _removeDestination(String destination) {
174         _messageBus.removeDestination(destination);
175     }
176 
177     private void _sendMessage(String destination, Message message) {
178         _messageBus.sendMessage(destination, message);
179     }
180 
181     private void _sendMessage(String destination, Object payload) {
182         Message message = new Message();
183 
184         message.setPayload(payload);
185 
186         _sendMessage(destination, message);
187     }
188 
189     private Object _sendSynchronousMessage(String destination, Message message)
190         throws MessageBusException {
191 
192         return _synchronousMessageSender.sendMessage(destination, message);
193     }
194 
195     private Object _sendSynchronousMessage(
196             String destination, Message message, long timeout)
197         throws MessageBusException {
198 
199         return _synchronousMessageSender.sendMessage(
200             destination, message, timeout);
201     }
202 
203     private Object _sendSynchronousMessage(
204             String destination, Object payload, String responseDestination)
205         throws MessageBusException {
206 
207         Message message = new Message();
208 
209         message.setResponseDestination(responseDestination);
210         message.setPayload(payload);
211 
212         return _sendSynchronousMessage(destination, message);
213     }
214 
215     private Object _sendSynchronousMessage(
216             String destination, Object payload, String responseDestination,
217             long timeout)
218         throws MessageBusException {
219 
220         Message message = new Message();
221 
222         message.setResponseDestination(responseDestination);
223         message.setPayload(payload);
224 
225         return _sendSynchronousMessage(destination, message, timeout);
226     }
227 
228     private boolean _unregisterMessageListener(
229         String destination, MessageListener listener) {
230 
231         return _messageBus.unregisterMessageListener(destination, listener);
232     }
233 
234     private static MessageBusUtil _instance = new MessageBusUtil();
235 
236     private MessageBus _messageBus;
237     private MessageSender _messageSender;
238     private SynchronousMessageSender _synchronousMessageSender;
239 
240 }