View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Tests exception retries in KSB messaging.
36   *
37   * @author Kuali Rice Team (rice.collab@kuali.org)
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       * Test that a message with retry count gets retried that many times.
66       *
67       * @throws Exception
68       */
69      @Test
70      public void testRetryCount() throws Exception {
71          //Turn the requeue up very high so the message will go through all it's requeues immediately
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          //pause to let save to queue in status 'E' happen
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 }