View Javadoc

1   /**
2    * Copyright 2005-2013 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.TesetHarnessExplodingQueue;
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   * Tests exception retries in KSB messaging.
37   *
38   * @author Kuali Rice Team (rice.collab@kuali.org)
39   *
40   */
41  public class ExceptionRetryCountTest extends KSBTestCase {
42  
43  
44  	private QName retryCountServiceName = new QName("KEW", "testExplodingRetryCount");
45  	private TestCallback callback = new TestCallback();
46  
47  	@Override
48  	public void setUp() throws Exception {
49  		System.setProperty(KSBConstants.Config.ROUTE_QUEUE_TIME_INCREMENT_KEY, "500");
50  	System.setProperty(KSBConstants.Config.ROUTE_QUEUE_MAX_RETRY_ATTEMPTS_KEY, "2");
51  		super.setUp();
52  		GlobalCallbackRegistry.getCallbacks().clear();
53  		GlobalCallbackRegistry.getCallbacks().add(this.callback);
54  		TestCallback.clearCallbacks();
55  		TesetHarnessExplodingQueue.NUM_CALLS = 0;
56  	}
57  
58  	@Override
59  	public void tearDown() throws Exception {
60  	    try {
61  		KSBServiceLocator.getScheduler().shutdown();
62  	    } finally {
63  		super.tearDown();
64  	    }
65  	}
66  
67  	/**
68  	 * Test that a message with retry count gets retried that many times.
69  	 *
70  	 * @throws Exception
71  	 */
72      @Test
73      public void testRetryCount() throws Exception {
74  		//Turn the requeue up very high so the message will go through all it's requeues immediately
75  
76  		ConfigContext.getCurrentContextConfig().putProperty(KSBConstants.Config.ROUTE_QUEUE_TIME_INCREMENT_KEY, "100");
77  
78  	KSBJavaService explodingQueue = (KSBJavaService) KsbApiServiceLocator.getMessageHelper().getServiceAsynchronously(
79  		this.retryCountServiceName);
80  		explodingQueue.invoke("");
81  		TestUtilities.waitForExceptionRouting();
82  
83  		this.callback.pauseUntilNumberCallbacksUsingStaticCounter(3, this.retryCountServiceName);
84  
85  	//pause to let save to queue in status 'E' happen
86  	int i = 0;
87  	while (i++ < 30) {
88  	    List<PersistedMessageBO> queuedItems = KSBServiceLocator.getMessageQueueService().findAll();
89  	    if (queuedItems.size() != 1) {
90  		fail("test setup wrong should have a single item in the queue.");
91  	    }
92  	    PersistedMessageBO message = queuedItems.get(0);
93  	    if (message.getQueueStatus().equals("E")) {
94  		break;
95  	    }
96  	    System.out.println("Message not saved to queue in 'E' status.  Sleeping 1 sec.");
97  	    Thread.sleep(1000);
98  	}
99  
100 		assertEquals("Service should have been called 3 times", 3, TesetHarnessExplodingQueue.NUM_CALLS);
101 
102 	    List<PersistedMessageBO> messagesQueued = KSBServiceLocator.getMessageQueueService().findByServiceName(
103 		this.retryCountServiceName, "invoke");
104 		PersistedMessageBO message = messagesQueued.get(0);
105 		assertEquals("Message should be in exception status", KSBConstants.ROUTE_QUEUE_EXCEPTION, message.getQueueStatus());
106 		assertEquals("Message retry count not what was configured", new Integer(2), message.getRetryCount());
107 	}
108 
109 }