View Javadoc
1 /* ==================================================================== 2 * License: 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in 13 * the documentation and/or other materials provided with the 14 * distribution. 15 * 16 * 3. The end-user documentation included with the redistribution, 17 * if any, must include the following acknowledgment: 18 * "This product includes software developed by 19 * Robert Half International (http://www.rhi.com/)." 20 * Alternately, this acknowledgment may appear in the software itself, 21 * if and wherever such third-party acknowledgments normally appear. 22 * 23 * 4. The names "Parc", "RHI", and "Robert Half International" must 24 * not be used to endorse or promote products derived from this 25 * software without prior written permission. For written 26 * permission, please contact pete.mckinstry@rhi.com. 27 * 28 * 5. Products derived from this software may not be called "PARC", 29 * nor may "PARC" appear in their name, without prior written 30 * permission of Robert Half International. 31 * 32 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 33 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 34 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 35 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 36 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 37 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 38 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 39 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 40 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 41 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 42 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 43 * SUCH DAMAGE. 44 * ==================================================================== 45 * 46 */ 47 package com.rhi.architecture.parc; 48 49 import com.rhi.architecture.resource.InitializationException; 50 51 import java.util.List; 52 import java.util.ListIterator; 53 54 /*** 55 * Processing pipeline component. Responsible for running the 56 * filters and pushing the results into the output adapter. 57 * 58 * @author Pete McKinstry 59 * @copyright 2002, Robert Half Int'l., Inc. All rights reserved. 60 * 61 * @since 1.0 62 */ 63 public class SerialMTPipeline extends AbstractPipeline { 64 65 // tree of all threads that have been launched. 66 // each filter has a sub-node containing a ThreadGroup 67 // for itself. 68 private ThreadGroup root; 69 70 /*** 71 * Constructor. takes single parameter equal to the 72 * number of threads of each filter. Defaults Channel 73 * to general purpose implementation 74 * 75 * @since 1.0 76 */ 77 public SerialMTPipeline() { 78 // no op 79 } 80 81 /*** 82 * Set the default channel 83 * 84 * @param ch 85 * @throws InitializationException 86 * @since 1.1 87 */ 88 public void init(Channel ch) throws InitializationException { 89 super.init(ch); 90 } 91 92 /*** 93 * Run the filter set. 94 * 95 * @throws ProcessingException 96 * @since 1.0 97 */ 98 public void process() throws ProcessingException { 99 log().info("running serial pipeline."); 100 101 try { 102 List filters = getFilters(); 103 if (filters.size() == 0) { 104 log().debug("no filters, pipeline will pass records through."); 105 return; 106 } 107 108 ListIterator iter = filters.listIterator(); 109 root = new ThreadGroup("filters"); 110 111 // for each filter.. 112 while (iter.hasNext()) { 113 Filter f = (Filter) iter.next(); 114 f.markForDeath(); 115 // filter should shutdown as soon as it's done. 116 f.setExceptionHandler(getExceptionHandler()); 117 118 // thread group name = filter name 119 ThreadGroup grp = new ThreadGroup(root, f.getName()); 120 121 // launch threads. 122 int thread_count = (f.numThreads() > 0) ? f.numThreads() : 1; 123 for (int i = 0; i < thread_count; ++i) { 124 Thread filterTh = 125 new Thread( 126 grp, 127 f, 128 f.getName() + Integer.toString(i) /* thread name */ 129 ); 130 //filterTh.setPriority( Thread.NORM_PRIORITY ); 131 filterTh.setDaemon(true); // for fatal error handling. 132 filterTh.start(); 133 } 134 135 // wait for threads to die. 136 while ((grp.activeCount() > 0) 137 && (getExceptionHandler().hasError() == false)) { 138 try { 139 Thread.yield(); // sleep(1000); 140 } 141 //catch (InterruptedException ie) { } 142 finally { 143 // ignore. let's the try hang around in case sleep() is 144 // ever used again. 145 } 146 } 147 if (getExceptionHandler().hasError()) { 148 stopFilters(root); 149 getExceptionHandler().rethrowException(); 150 } 151 log().debug("Filter <" + f.getName() + "> complete, next... "); 152 } 153 154 if (root.activeCount() > 0) { 155 log().error("all threads should have already been shutdown."); 156 throw new ProcessingException("Bug in thread control logic."); 157 } 158 } 159 finally { 160 log().info("pipeline complete."); 161 } 162 } 163 164 /*** 165 * Cleanup in-process work and shutdown queues. 166 * 167 * @since 1.0 168 */ 169 public void flush() { 170 log().info("pipeline shutting down."); 171 172 // need to check for outstanding threads 173 while ((root != null) && (root.activeCount() > 0)) { 174 // Don't be as nice during flush() because i'm trying to exit. 175 Thread.yield(); 176 } 177 } 178 179 }

This page was automatically generated by Maven