-
Notifications
You must be signed in to change notification settings - Fork 10
/
test-message-bus-sleep-10s.groovy
56 lines (48 loc) · 1.94 KB
/
test-message-bus-sleep-10s.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
*
* @author Paweł Kruszewski (@ktor on github)
*/
package s;
import com.liferay.portal.kernel.messaging.Message
import com.liferay.portal.kernel.messaging.MessageBusUtil
import com.liferay.portal.kernel.messaging.MessageListener
import com.liferay.portal.kernel.messaging.MessageListenerException
import com.liferay.portal.kernel.messaging.SynchronousDestination
public class RequestedDispatcherPOC implements MessageListener {
@Override
public void receive(Message message) throws MessageListenerException {
// payload processing
// send response
Thread.sleep(10000);
Message response = MessageBusUtil.createResponseMessage(message);
response.setPayload("some response");
MessageBusUtil.sendMessage(response.getDestinationName(), response);
}
}
class TestBusTimeout {
public static test(PrintWriter out) {
// create synchronous destination
SynchronousDestination destination = new SynchronousDestination();
destination.setName("some/address");
MessageBusUtil.addDestination(destination);
// register listener for destination
RequestedDispatcherPOC dispatcherPOC = new RequestedDispatcherPOC();
MessageBusUtil.registerMessageListener("some/address", dispatcherPOC);
send(1000, out);
send(5000, out);
send(10000, out);
}
public static send(long timeout_ms, PrintWriter out) {
Message message = new Message();
message.setResponseDestinationName("some/address" + "/answer");
long start = System.currentTimeMillis();
Object respObj = MessageBusUtil.sendSynchronousMessage("some/address", message, timeout_ms);
long stop = System.currentTimeMillis();
out.println("Timeout: " + timeout_ms + "ms. Received response to payload: " + respObj + ", after: " + Long.toString(stop - start) + "ms");
}
}
try {
TestBusTimeout.test(out);
} catch (Exception e) {
e.printStackTrace(out);
}