001/* 002 * Copyright (c) 2017 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 */ 016package org.opengion.fukurou.fileexec; 017 018import java.util.Arrays; 019import java.util.List; 020import java.util.concurrent.ConcurrentMap; 021import java.util.concurrent.ConcurrentHashMap; 022 023import static org.opengion.fukurou.fileexec.CommandLine.GE70; // enum を簡素化して使用するための定義 024 025/** 026 * MainProcess は、単独で使用する ファイル取込システムのメインクラスです。 027 * 028 *<pre> 029 * このクラスのmainメソッドから起動します。 030 * コマンドラインを処理することで、各種処理を実行します。 031 * 032 *</pre> 033 * 034 * @og.rev 7.0.0.0 (2017/07/07) 新規作成 035 * 036 * @version 7.0 037 * @author Kazuhiko Hasegawa 038 * @since JDK1.8, 039 */ 040public final class MainProcess { 041 private static final XLogger LOGGER= XLogger.getLogger( MainProcess.class.getName() ); // ログ出力 042 043 private final ConcurrentMap<Integer,FileExec> execMap = new ConcurrentHashMap<>(); 044 045 /** 046 * デフォルトコンストラクター 047 */ 048 public MainProcess() { super(); } // 必要ないが、とりあえず。 049 050 /** 051 * 時間起動のタスクオブジェクトを起動します。 052 * 053 * コマンドリストは、予約番号,種別,号機指定,雛形ファイル,開始日時,実行間隔,終了日時,経過終了間隔,パラメータMAP を 054 * 文字列として順番に持っています。 055 * リストの数が、少ない場合は、それぞれ、初期値が使用されます。 056 * 最低、コマンド種別は、必要です。 057 * 058 * @param cmndLine CommandLineオブジェクト 059 */ 060 public void startTask( final CommandLine cmndLine ) { 061 // 同一予約番号の過去の依頼は停止しておきます。 062 final int yoyakuNo = Integer.parseInt( cmndLine.getValue( GE70.RSRV_NO ) ); 063 stopTask( yoyakuNo ); // 一旦、予約を解除します。 064 065 // ※ 取込予約フラグ(FGYKAN)は、DB検索時に、1:登録 4:停止 しか、来ません。 066 final String fgkan = cmndLine.getValue( GE70.FGYKAN ); // 取込予約フラグ 1:登録 2:済 3:実行中 4:停止 067 if( "1".equals( fgkan ) || "3".equals( fgkan ) ) { // 取込予約フラグ(FGYKAN)が、1:登録 3:実行中 以外は、過去の依頼を停止した時点で、終了します。 068 final FileExec fExec = new FileExec( cmndLine ); 069 070 LOGGER.info( () -> "startTask: yoyakuNo=[" + yoyakuNo + "]" ); 071 072 fExec.watchStart(); 073 execMap.put( yoyakuNo,fExec ); 074 } 075 else { 076 LOGGER.warning( () -> "【WARNING】startTask: yoyakuNo=[" + yoyakuNo + "] , fgkan=[" + fgkan + "]" ); // 6.8.1.5 (2017/09/08) 077 } 078 } 079 080 /** 081 * 時間起動のタスクオブジェクトをキャンセルします。 082 * 083 * @param yoyakuNo コマンド予約番号 084 */ 085 private void stopTask( final int yoyakuNo ) { 086 final FileExec fExec = execMap.remove( yoyakuNo ); // 取り消しなので、Mapから削除します。 087 if( fExec != null ) { // 完了(正常終了、例外、取り消し)以外は、キャンセルします。 088 fExec.watchStop(); 089 } 090 091 LOGGER.info( () -> "stopTask: yoyakuNo=[" + yoyakuNo + "]" ); 092 } 093 094 /** 095 * すべての成形機のセッションフォルダの監視を終了します。 096 * 097 */ 098 public void watchStop() { 099 execMap.forEach( (no,fExec) -> fExec.watchStop() ); 100 } 101 102 /** main メソッドから呼ばれる ヘルプメッセージです。 {@value} */ 103 public static final String USAGE = "Usage: java org.opengion.fukurou.fileexec.MainProcess [-LOOP n(秒)] [-help]" ; 104 105 /** 106 * Euromapのミドルウエア(成形条件管理システム)のメインクラスを起動します。 107 * 108 * ベースフォルダを、指定しない場合は、アプリケーションの起動フォルダになります。(./) 109 * 110 * {@value #USAGE} 111 * 112 * @param args 引数配列 113 */ 114 public static void main( final String[] args ) { 115 // ********** 【整合性チェック】 ********** 116 117 // ********** 【引数定義】 ********** 118 int loopTime = 10; // スキャンするインターバル(秒) 119 120 // ********** 【引数処理】 ********** 121 for( int i=0; i<args.length; i++ ) { 122 final String arg = args[i]; 123 124 if( "-help" .equalsIgnoreCase( arg ) ) { System.out.println( USAGE ); return ; } 125 else if( "-LOOP" .equalsIgnoreCase( arg ) ) { 126 loopTime = Integer.parseInt( args[++i] ); // ひとつ進める 127 } 128 else { 129 // MSG2011 = コマンドリストの操作に失敗ました。形式をご確認ください。[{0}] 130 throw MsgUtil.throwException( "MSG2011" , Arrays.toString( args ) ); 131 } 132 } 133 134 // ********** 【本体処理】 ********** 135 final MainProcess mainPrcs = new MainProcess(); 136 137 // ほぼ、暫定対応。無限ループで、DBを監視します。 138 boolean isFirst = true; // 最初だけ、FGYKAN=3:実行中 を検索する必要がある。 139 int cnt = 0; 140 while( true ) { 141 try { 142 final List<CommandLine> cmndList = CommandLine.dbCommand( isFirst ); 143 cmndList.forEach( cmndLine -> mainPrcs.startTask( cmndLine ) ); 144 try{ Thread.sleep( loopTime * 1000L ); } catch( final InterruptedException ex ){} 145 isFirst = false; 146 System.out.println( StringUtil.getTimeFormat( "yyyyMMdd HH:mm:ss [" + (cnt++) + "]" ) ); // 6.8.1.5 (2017/09/08) 147 } 148 catch( final Throwable th ) { 149 // MSG0021 = 予期せぬエラーが発生しました。\n\tメッセージ=[{0}] 150 MsgUtil.errPrintln( th , "MSG0021" , th.getMessage() ); 151 152 mainPrcs.watchStop(); 153 } 154 } 155 } 156}