001    /**
002     * Copyright 2005-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.ksb.messaging;
017    
018    import org.junit.Test;
019    import org.kuali.rice.core.api.config.property.ConfigContext;
020    import org.kuali.rice.ksb.api.KsbApiServiceLocator;
021    import org.kuali.rice.ksb.messaging.remotedservices.TesetHarnessExplodingQueue;
022    import org.kuali.rice.ksb.messaging.service.KSBJavaService;
023    import org.kuali.rice.ksb.service.KSBServiceLocator;
024    import org.kuali.rice.ksb.test.KSBTestCase;
025    import org.kuali.rice.ksb.util.KSBConstants;
026    import org.kuali.rice.test.TestUtilities;
027    
028    import javax.xml.namespace.QName;
029    import java.util.List;
030    
031    import static org.junit.Assert.assertEquals;
032    import static org.junit.Assert.fail;
033    
034    
035    /**
036     * Tests exception retries in KSB messaging.
037     *
038     * @author Kuali Rice Team (rice.collab@kuali.org)
039     *
040     */
041    public class ExceptionRetryCountTest extends KSBTestCase {
042    
043    
044            private QName retryCountServiceName = new QName("KEW", "testExplodingRetryCount");
045            private TestCallback callback = new TestCallback();
046    
047            @Override
048            public void setUp() throws Exception {
049                    System.setProperty(KSBConstants.Config.ROUTE_QUEUE_TIME_INCREMENT_KEY, "500");
050            System.setProperty(KSBConstants.Config.ROUTE_QUEUE_MAX_RETRY_ATTEMPTS_KEY, "2");
051                    super.setUp();
052                    GlobalCallbackRegistry.getCallbacks().clear();
053                    GlobalCallbackRegistry.getCallbacks().add(this.callback);
054                    TestCallback.clearCallbacks();
055                    TesetHarnessExplodingQueue.NUM_CALLS = 0;
056            }
057    
058            @Override
059            public void tearDown() throws Exception {
060                try {
061                    KSBServiceLocator.getScheduler().shutdown();
062                } finally {
063                    super.tearDown();
064                }
065            }
066    
067            /**
068             * Test that a message with retry count gets retried that many times.
069             *
070             * @throws Exception
071             */
072        @Test
073        public void testRetryCount() throws Exception {
074                    //Turn the requeue up very high so the message will go through all it's requeues immediately
075    
076                    ConfigContext.getCurrentContextConfig().putProperty(KSBConstants.Config.ROUTE_QUEUE_TIME_INCREMENT_KEY, "100");
077    
078            KSBJavaService explodingQueue = (KSBJavaService) KsbApiServiceLocator.getMessageHelper().getServiceAsynchronously(
079                    this.retryCountServiceName);
080                    explodingQueue.invoke("");
081                    TestUtilities.waitForExceptionRouting();
082    
083                    this.callback.pauseUntilNumberCallbacksUsingStaticCounter(3, this.retryCountServiceName);
084    
085            //pause to let save to queue in status 'E' happen
086            int i = 0;
087            while (i++ < 30) {
088                List<PersistedMessageBO> queuedItems = KSBServiceLocator.getMessageQueueService().findAll();
089                if (queuedItems.size() != 1) {
090                    fail("test setup wrong should have a single item in the queue.");
091                }
092                PersistedMessageBO message = queuedItems.get(0);
093                if (message.getQueueStatus().equals("E")) {
094                    break;
095                }
096                System.out.println("Message not saved to queue in 'E' status.  Sleeping 1 sec.");
097                Thread.sleep(1000);
098            }
099    
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    }