View Javadoc

1   /*
2    * Copyright 2007 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License"); you may not use this file except in
5    * compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.opensource.org/licenses/ecl2.php
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS
10   * IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
11   * language governing permissions and limitations under the License.
12   */
13  package org.kuali.rice.ksb.messaging;
14  
15  import java.util.List;
16  
17  import javax.xml.namespace.QName;
18  
19  import org.junit.Test;
20  import org.kuali.rice.core.config.ConfigContext;
21  import org.kuali.rice.ksb.messaging.GlobalCallbackRegistry;
22  import org.kuali.rice.ksb.messaging.PersistedMessage;
23  import org.kuali.rice.ksb.messaging.remotedservices.TesetHarnessExplodingQueue;
24  import org.kuali.rice.ksb.messaging.service.KSBJavaService;
25  import org.kuali.rice.ksb.service.KSBServiceLocator;
26  import org.kuali.rice.ksb.test.KSBTestCase;
27  import org.kuali.rice.ksb.util.KSBConstants;
28  import org.kuali.rice.test.TestUtilities;
29  
30  
31  /**
32   * This is a description of what this class does - rkirkend don't forget to fill this in.
33   *
34   * @author Kuali Rice Team (rice.collab@kuali.org)
35   *
36   */
37  public class ExceptionRetryCountTest extends KSBTestCase {
38  
39  
40  	private QName retryCountServiceName = new QName("KEW", "testExplodingRetryCount");
41  	private TestCallback callback = new TestCallback();
42  
43  	@Override
44  	public void setUp() throws Exception {
45  		System.setProperty(KSBConstants.ROUTE_QUEUE_TIME_INCREMENT_KEY, "500");
46  	System.setProperty(KSBConstants.ROUTE_QUEUE_MAX_RETRY_ATTEMPTS_KEY, "2");
47  		super.setUp();
48  		GlobalCallbackRegistry.getCallbacks().clear();
49  		GlobalCallbackRegistry.getCallbacks().add(this.callback);
50  		TestCallback.clearCallbacks();
51  		TesetHarnessExplodingQueue.NUM_CALLS = 0;
52  	}
53  
54  	@Override
55  	public void tearDown() throws Exception {
56  	    try {
57  		KSBServiceLocator.getScheduler().shutdown();
58  	    } finally {
59  		super.tearDown();
60  	    }
61  	}
62  
63  	/**
64  	 * Test that a message with retry count gets retried that many times.
65  	 *
66  	 * @throws Exception
67  	 */
68      @Test
69      public void testRetryCount() throws Exception {
70  		//Turn the requeue up very high so the message will go through all it's requeues immediately
71  
72  		ConfigContext.getCurrentContextConfig().putProperty(KSBConstants.ROUTE_QUEUE_TIME_INCREMENT_KEY, "100");
73  
74  	KSBJavaService explodingQueue = (KSBJavaService) KSBServiceLocator.getMessageHelper().getServiceAsynchronously(
75  		this.retryCountServiceName);
76  		explodingQueue.invoke("");
77  		TestUtilities.waitForExceptionRouting();
78  
79  		this.callback.pauseUntilNumberCallbacksUsingStaticCounter(3, this.retryCountServiceName);
80  
81  	//pause to let save to queue in status 'E' happen
82  	int i = 0;
83  	while (i++ < 30) {
84  	    List<PersistedMessage> queuedItems = KSBServiceLocator.getRouteQueueService().findAll();
85  	    if (queuedItems.size() != 1) {
86  		fail("test setup wrong should have a single item in the queue.");
87  	    }
88  	    PersistedMessage message = queuedItems.get(0);
89  	    if (message.getQueueStatus().equals("E")) {
90  		break;
91  	    }
92  	    System.out.println("Message not saved to queue in 'E' status.  Sleeping 1 sec.");
93  	    Thread.sleep(1000);
94  	}
95  
96  		assertEquals("Service should have been called 3 times", 3, TesetHarnessExplodingQueue.NUM_CALLS);
97  
98  	List<PersistedMessage> messagesQueued = KSBServiceLocator.getRouteQueueService().findByServiceName(
99  		this.retryCountServiceName, "invoke");
100 		PersistedMessage message = messagesQueued.get(0);
101 		assertEquals("Message should be in exception status", KSBConstants.ROUTE_QUEUE_EXCEPTION, message.getQueueStatus());
102 		assertEquals("Message retry count not what was configured", new Integer(2), message.getRetryCount());
103 	}
104 
105 }