001    /*
002     * Copyright (c) 2009 The openGion Project.
003     *
004     * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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,
013     * either express or implied. See the License for the specific language
014     * governing permissions and limitations under the License.
015     */
016    package org.opengion.hayabusa.report2;
017    
018    import java.util.ArrayList;
019    import java.util.Collections;
020    import java.util.List;
021    
022    import org.opengion.fukurou.util.StringUtil;
023    import org.opengion.hayabusa.common.HybsSystem;
024    
025    /**
026     * 帳票è¦�求スレãƒ?ƒ‰ã�®æœ¬ä½“ã�§ã�™ã?
027     * 外部ã�‹ã‚‰ã‚¹ã‚¿ãƒ?‚¯ã�•れã�Ÿã‚­ãƒ¥ãƒ¼ã‚’å?入れå?出ã�—ã?é ?•ªã�«å‡¦ç�?�—ã�¾ã�™ã?
028     *
029     * ã�‚るキューã�«å¯¾ã�—ã�¦ã‚¨ãƒ©ãƒ¼ã�Œç™ºç”Ÿã�™ã‚‹ã�¨ã€�シスãƒ?ƒ ãƒªã‚½ãƒ¼ã‚¹ã�®RETRY_COUNTã�§è¨­å®šã�•れã�Ÿå›žæ•°å†�å?ç�?‚’試ã�¿ã�¾ã�™ã?
030     * ã�“ã?回数åˆ?‚¨ãƒ©ãƒ¼ã�Œç™ºç”Ÿã�—ã�Ÿå?å�ˆã?ã€�ã��ã�®ã‚­ãƒ¥ãƒ¼ã�®ã�¿ã�Œã‚¢ãƒ—リエラーã�¨ã�ªã‚Šã�¾ã�™ã?
031     *
032     * ã�“ã?スレãƒ?ƒ‰ã�¯ä¸?º¦ç”Ÿæ?ã�•れるã�¨ã€�外部ã�‹ã‚‰æ˜Žç¤ºçš?�«çµ‚äº??è¦�求を起ã�“ã�•ã�ªã�?™�り生存ã�—ç¶šã�‘ã�¾ã�™ã?
033     * 終äº?�™ã‚‹ã�«ã�¯ã€�finish()メソãƒ?ƒ‰ã‚’呼ã�³å‡ºã�—ã�¾ã�™ã?
034     * ã�“ã?メソãƒ?ƒ‰ã�Œå‘¼ã�°ã‚Œã‚‹ã�¨ã€�å?部ã�§ã‚¹ã‚¿ãƒ?‚¯ã�—ã�¦ã�?‚‹ã‚­ãƒ¥ãƒ¼ã�¯å…¨ã�¦ã‚¯ãƒªã‚¢ã�•れるã�Ÿã‚�ã?ã��ã?時点ã�§
035     * 処ç�?�•れã�¦ã�?‚‹ã‚­ãƒ¥ãƒ¼ã�®å‡¦ç�?�Œå®Œäº?�—ã�Ÿæ™‚点ã�§ã€�スレãƒ?ƒ‰ã�Œçµ‚äº?�—ã�¾ã�™ã?
036     *
037     * @og.group 帳票シスãƒ?ƒ 
038     *
039     * @version  4.0
040     * @author   Hiroki.Nakamura
041     * @since    JDK1.6
042     */
043    public class ExecThread extends Thread {
044    
045            private static enum Status { EXECUTE, WAIT };
046            private Status state = Status.EXECUTE;
047    
048            private static final int RETRY_COUNT = HybsSystem.sysInt( "REPORT_RETRY_COUNT" );
049    
050            private final List<ExecQueue> queues = Collections.synchronizedList( new ArrayList<ExecQueue>() );
051    
052            private long threadStart        = 0;
053            private long execStart          = 0;
054            private long execEnd            = 0;
055            private final boolean debug;    // 4.3.0.0 (2008/07/15) ãƒ?ƒ�ãƒ?‚°ã�®è¿½åŠ?
056    
057            /**
058             * コンストラクタ
059             * OOoã�¸ã�®æŽ¥ç¶šã‚’生æ?ã�—ã�¾ã�™ã?
060             *
061             * @param       id      スレãƒ?ƒ‰ID
062             */
063            public ExecThread( final String id ) {
064                    // threadStart = System.currentTimeMillis();
065                    // setName( id ); // スタãƒ?‚¯ãƒˆãƒ¬ãƒ¼ã‚¹æ™‚ã�«ã‚¹ãƒ¬ãƒ?ƒ‰IDã‚’å?ã�™ã�Ÿã‚�ã�«ã‚»ãƒ?ƒˆ
066                    this ( id , false );
067            }
068    
069            /**
070             * コンストラクタ
071             * OOoã�¸ã�®æŽ¥ç¶šã‚’生æ?ã�—ã�¾ã�™ã?
072             *
073             * @og.rev 4.3.0.0 (2008/07/15) ãƒ?ƒ�ãƒ?‚°ãƒ•ラグを追åŠ?�—ã�¾ã�™ã?
074             * @param       id                      スレãƒ?ƒ‰ID
075             * @param       debugFlag       ãƒ?ƒ�ãƒ?‚°ãƒ•ラグ[true/false]
076             */
077            public ExecThread( final String id , final boolean debugFlag ) {
078                    threadStart = System.currentTimeMillis();
079                    setName( id ); // スタãƒ?‚¯ãƒˆãƒ¬ãƒ¼ã‚¹æ™‚ã�«ã‚¹ãƒ¬ãƒ?ƒ‰IDã‚’å?ã�™ã�Ÿã‚�ã�«ã‚»ãƒ?ƒˆ
080                    debug = debugFlag; // 4.2.5.0 (2008/06/26) ãƒ?ƒ�ãƒ?‚°å‡¦ç�??追åŠ?
081            }
082    
083            /**
084             * キューをスタãƒ?‚¯ã�—ã�¾ã�™ã?
085             *
086             * @og.rev 4.3.0.0 (2008/07/15) debug追�
087             * @param       queue   ExecQueueオブジェク�
088             *
089             * @return      スタãƒ?‚¯ã�Œå�—ã�‘付ã�‘られã�Ÿã�‹ã�©ã�?�‹
090             */
091            public boolean stackQueue( final ExecQueue queue ) {
092                    queue.addMsg( "[INFO]QUEUE STACK:THREAD-ID=" + queue.getThreadId() + ",YKNO=" + queue.getYkno() + HybsSystem.CR );
093    
094                    queues.add( queue );
095    
096                    queue.setExecute();
097                    if( debug ) { queue.addMsg( "[INFO]QUEUE STACKED" + HybsSystem.CR ); }
098    
099                    synchronized( this ) {
100                            if( state == Status.WAIT ) {
101                                    this.interrupt();
102                                    if( debug ) { queue.addMsg( "[INFO]INTERRUPT" + HybsSystem.CR ); }
103                            }
104                    }
105                    return true;
106            }
107    
108            /**
109             * スレãƒ?ƒ‰æœ¬ä½?
110             * スタãƒ?‚¯ã�•れã�Ÿã‚­ãƒ¥ãƒ¼ã‚’é?番ã�«å�–り出ã�—å?ç�?‚’行ã�„ã�¾ã�™ã?
111             */
112            @Override
113            public void run() {
114    
115                    while( true ) {
116    
117                            synchronized( this ) {
118                                    while( queues.isEmpty() ) {
119                                            try {
120                                                    state = Status.WAIT;
121                                                    wait();
122                                            }
123                                            catch( InterruptedException ex ) {
124                                                    state = Status.EXECUTE;
125                                            }
126                                    }
127                            }
128    
129                            ExecQueue queue = popQueue();
130                            if( queue != null ) {
131                                    if( "_FINALIZE".equals( queue.getYkno() ) ) {
132                                            if( debug ) { queue.addMsg( "[INFO]END" + HybsSystem.CR ); }
133                                            break;
134                                    }
135                                    else {
136                                            if( debug ) { queue.addMsg( "[INFO]QUEUE START" + HybsSystem.CR ); }
137                                            exec( queue );
138    
139                                            // System.out.println( queue.getMsg() );
140                                            System.out.print( queue.getMsg() ); // 4.3.0.0 (2008/07/15)
141                                    }
142                            }
143                    }
144            }
145    
146            /**
147             * スレãƒ?ƒ‰ã‚’終äº?�•ã�›ã‚‹ã�Ÿã‚�ã�®ã‚­ãƒ¥ãƒ¼ã‚’追åŠ?�—ã�¾ã�™ã?
148             *
149             * ã�“ã?メソãƒ?ƒ‰ã�Œå‘¼ã�°ã‚Œã‚‹ã�¨ã€�å?部ã�«ã‚¹ã‚¿ãƒ?‚¯ã�—ã�¦ã�?‚‹ã‚­ãƒ¥ãƒ¼ã�¯å…¨ã�¦ã‚¯ãƒªã‚¢ã�•れã�¾ã�™ã?
150             */
151            public void finish() {
152                    queues.clear();
153    
154                    ExecQueue qu = new ExecQueue();
155                    qu.setYkno( "_FINALIZE" );
156                    stackQueue( qu );
157            }
158    
159            /**
160             * スレãƒ?ƒ‰ã‚’終äº?�•ã�›ã‚‹ã�Ÿã‚�ã�®ã‚­ãƒ¥ãƒ¼ã‚’追åŠ?�—ã�¾ã�™ã?
161             *
162             * ã�“ã?メソãƒ?ƒ‰ã�§ã�¯ã€�æ—¢ã�«ã‚¹ã‚¿ãƒ?‚¯ã�•れã�¦ã�?‚‹ã‚­ãƒ¥ãƒ¼ã�¯ã‚¯ãƒªã‚¢ã�•れã�šã?å…¨ã�¦å‡¦ç�?�•れã�Ÿå¾Œã�§ã€?
163             * スレãƒ?ƒ‰ã‚’終äº?�—ã�¾ã�™ã?
164             *
165             * @og.rev 5.1.6.0 (2010/05/01) æ–°è¦�作æ?
166             */
167            public void finishAfterExec() {
168                    ExecQueue qu = new ExecQueue();
169                    qu.setYkno( "_FINALIZE" );
170                    stackQueue( qu );
171            }
172    
173    //      /**
174    //       * ç�¾åœ¨å‡¦ç�?�—ã�¦ã�?‚‹ã‚­ãƒ¥ãƒ¼ã�®å‡¦ç�?™‚é–“ã‚’è¿”ã�—ã�¾ã�™ã?
175    //       * スレãƒ?ƒ‰ã�ŒWAIT状態ã?å ´å�ˆã?ã€?ã‚’è¿”ã�—ã�¾ã�™ã?
176    //       *
177    //       * @return 処ç�?™‚é–?
178    //       */
179    //      public int getExecTime() {
180    //              return ( execStart > execEnd ? (int)(System.currentTimeMillis() - execStart) : 0 );
181    //      }
182    
183            /**
184             * 帳票処ç�?‚’行ã�„ã�¾ã�™ã?
185             *
186             * @og.rev 5.1.2.0 (2010/01/01) 256シートをè¶?�ˆã�Ÿå?å�ˆã�§ã‚‚ã?æ­£ã�—ã��処ç�?�§ã��るよã�†ã�«å¯¾å¿?
187             *
188             * @param       queue   ExecQueueオブジェク�
189             */
190            private void exec( final ExecQueue queue ) {
191                    execStart = System.currentTimeMillis();
192    
193                    ExecProcess oep = new ExecProcess( queue, debug );
194                    for( int i = 0; i <= RETRY_COUNT; i++ ) {
195                            try {
196                                    // 5.1.2.0 (2010/01/01) ãƒ??ã‚¿ã�Œçµ‚ã‚�ã‚‹ã�¾ã�§å‡¦ç�?‚’継続ã�™ã‚‹ã?
197                                    while( !queue.isEnd() ) {
198                                            oep.process();
199                                    }
200                                    queue.setComplete();
201                                    break;
202                            }
203                            catch( Throwable th ) {
204                                    queue.addMsg( "[ERROR]ERROR OCCURRED!" + HybsSystem.CR );
205                                    queue.addMsg( StringUtil.stringStackTrace( th ) );
206    
207                                    if( i == RETRY_COUNT ) {
208                                            queue.addMsg( "[ERROR]UPTO RETRY COUNT!" + HybsSystem.CR );
209                                            queue.setError();
210                                    }
211                            }
212                    }
213    
214                    execEnd = System.currentTimeMillis();
215            }
216    
217            /**
218             * キューをå�–りå?ã�—ã�¾ã�™ã?
219             *
220             * @return キュー
221             */
222            private ExecQueue popQueue() {
223                    return queues.remove( 0 );
224            }
225    
226            /**
227             * ã�“ã?クラスã�®æ–?­—å?表ç�¾ã‚’è¿”ã�—ã�¾ã�™ã?
228             *
229             * @og.rev 4.3.0.0 (2008/07/15) debugを追�
230             *
231             * @return æ–?­—å?表ç�¾
232             */
233            @Override
234            public String toString() {
235                    StringBuilder sb = new StringBuilder();
236                    sb.append( "STATE=" ).append( state.toString() );
237                    sb.append( ", START=" ).append( HybsSystem.getDate( threadStart ) );
238                    sb.append( ", POOL=" ).append( queues.size() );
239                    sb.append( ", EXEC-START=" ).append( HybsSystem.getDate( execStart ) );
240                    sb.append( ", EXEC-END=" ).append( HybsSystem.getDate( execEnd ) );
241                    sb.append( ", DEBUG=" ).append( debug ); // 4.3.0.0 (2008/07/15) ãƒ?ƒ�ãƒ?‚°ã�®è¿½åŠ?
242    
243                    return sb.toString();
244            }
245    }