1
22
23 package com.liferay.portal.kernel.messaging;
24
25
32 public class StringResponseMessageListener implements MessageListener {
33
34 public StringResponseMessageListener(
35 Destination destination, Destination responseDestination,
36 String responseId, long timeout) {
37
38 _destination = destination;
39 _responseDestination = responseDestination;
40 _responseId = responseId;
41 _timeout = timeout;
42 }
43
44 public synchronized String send(String message) throws MessageBusException {
45 if (message.equals(_EMTPY_MESSAGE)) {
46 StringBuilder sb = new StringBuilder();
47
48 sb.append("{\"lfrResponseDestination\":\"");
49 sb.append(_responseDestination.getName());
50 sb.append("\",\"lfrResponseId\":\"");
51 sb.append(_responseId);
52 sb.append("\"}");
53
54 message = sb.toString();
55 }
56 else {
57 StringBuilder sb = new StringBuilder();
58
59 sb.append(message.substring(0, message.length() - 1));
60 sb.append(",\"lfrResponseDestination\":\"");
61 sb.append(_responseDestination.getName());
62 sb.append("\",\"lfrResponseId\":\"");
63 sb.append(_responseId);
64 sb.append("\"}");
65
66 message = sb.toString();
67 }
68
69 _destination.send(message);
70
71 try {
72 wait(_timeout);
73 }
74 catch (InterruptedException ie) {
75 throw new MessageBusException(
76 "Unable to receive response for request", ie);
77 }
78
79 if (_responseMessage == null) {
80 throw new MessageBusException(
81 "No reply received for request: " + message);
82 }
83
84 return _responseMessage;
85 }
86
87 public void receive(Object message) {
88 throw new UnsupportedOperationException();
89 }
90
91 public synchronized void receive(String message) {
92 if (message.indexOf(
93 "\"lfrResponseId\":\"" + _responseId + "\"") != -1) {
94
95 _responseMessage = message;
96
97 notify();
98 }
99 }
100
101 private static final String _EMTPY_MESSAGE = "{}";
102
103 private Destination _destination;
104 private Destination _responseDestination;
105 private String _responseId;
106 private long _timeout;
107 private String _responseMessage;
108
109 }