1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.ksb.messaging;
17
18 import org.junit.Test;
19 import org.kuali.rice.core.api.config.property.ConfigContext;
20 import org.kuali.rice.ksb.api.KsbApiServiceLocator;
21 import org.kuali.rice.ksb.messaging.remotedservices.TestHarnessExplodingQueue;
22 import org.kuali.rice.ksb.messaging.service.KSBJavaService;
23 import org.kuali.rice.ksb.service.KSBServiceLocator;
24 import org.kuali.rice.ksb.test.KSBTestCase;
25 import org.kuali.rice.ksb.util.KSBConstants;
26 import org.kuali.rice.test.TestUtilities;
27
28 import javax.xml.namespace.QName;
29 import java.util.List;
30
31 import static org.junit.Assert.assertEquals;
32 import static org.junit.Assert.fail;
33
34
35
36
37
38
39 public class ExceptionRetryCountTest extends KSBTestCase {
40
41 private QName retryCountServiceName = new QName("KEW", "testExplodingRetryCount");
42 private TestCallback callback = new TestCallback();
43
44 @Override
45 public void setUp() throws Exception {
46 System.setProperty(KSBConstants.Config.ROUTE_QUEUE_TIME_INCREMENT_KEY, "500");
47 System.setProperty(KSBConstants.Config.ROUTE_QUEUE_MAX_RETRY_ATTEMPTS_KEY, "2");
48 super.setUp();
49 GlobalCallbackRegistry.getCallbacks().clear();
50 GlobalCallbackRegistry.getCallbacks().add(this.callback);
51 TestCallback.clearCallbacks();
52 TestHarnessExplodingQueue.NUM_CALLS = 0;
53 }
54
55 @Override
56 public void tearDown() throws Exception {
57 try {
58 KSBServiceLocator.getScheduler().shutdown();
59 } finally {
60 super.tearDown();
61 }
62 }
63
64
65
66
67
68
69 @Test
70 public void testRetryCount() throws Exception {
71
72
73 ConfigContext.getCurrentContextConfig().putProperty(KSBConstants.Config.ROUTE_QUEUE_TIME_INCREMENT_KEY, "10000");
74
75 KSBJavaService explodingQueue = KsbApiServiceLocator.getMessageHelper().getServiceAsynchronously(
76 this.retryCountServiceName);
77 explodingQueue.invoke("");
78 TestUtilities.waitForExceptionRouting();
79
80 this.callback.pauseUntilNumberCallbacksUsingStaticCounter(3, this.retryCountServiceName);
81
82
83 int i = 0;
84 while (i++ < 30) {
85 List<PersistedMessageBO> queuedItems = KSBServiceLocator.getMessageQueueService().findAll();
86 if (queuedItems.size() != 1) {
87 fail("test setup wrong should have a single item in the queue but instead had "
88 + queuedItems.size()
89 + ".");
90 }
91 PersistedMessageBO message = queuedItems.get(0);
92 if (message.getQueueStatus().equals("E")) {
93 break;
94 }
95 System.out.println("Message not saved to queue in 'E' status. Sleeping 1 sec.");
96 Thread.sleep(1000);
97 }
98
99 assertEquals("Service should have been called 3 times", 3, TestHarnessExplodingQueue.NUM_CALLS);
100
101 List<PersistedMessageBO> messagesQueued = KSBServiceLocator.getMessageQueueService().findByServiceName(
102 this.retryCountServiceName, "invoke");
103 PersistedMessageBO message = messagesQueued.get(0);
104 assertEquals("Message should be in exception status", KSBConstants.ROUTE_QUEUE_EXCEPTION,
105 message.getQueueStatus());
106 assertEquals("Message retry count not what was configured", new Integer(2), message.getRetryCount());
107 }
108
109 }