5

スタンドアロンの Java アプリケーションとして実行される単純なファイル転送アプリケーションがあります。SFTP エンドポイントからファイルを取得し、別のエンドポイントに移動します。

ファイルは、転送後に SFTP エンドポイントから削除されます。ファイルがなくなったら、プログラムを終了させるのが理想的です。ただし、Camel を起動して条件付きで終了させる (SFTP エンドポイントにファイルがなくなった場合など)解決策を見つけることができませんでした。私の回避策は現在、Camel を起動してから、メイン スレッドを非常に長い時間スリープ状態にすることです。その後、ユーザーはアプリケーションを手動で強制終了する必要があります (CTRL+C またはその他の方法で)。

自動的に終了するようにアプリケーションを終了させるより良い方法はありますか?

以下は私の現在のコードです:

CamelContext (Spring App Context) では:

<route>
    <from uri="sftp:(url)" />
    <process ref="(processor)" />
    <to uri="(endpoint)" />
</route>

main() メソッド:

public static void main(String[] args)
{
  ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml");
  CamelContext camelContext = appContext.getBean(CamelContext.class);

  try
  {
    camelContext.start();
    Thread.sleep(1000000000); // Runs the program for a long time -- is there a better way?
  }
  catch (Exception e)
  {
    e.printStackTrace();
  }

  UploadContentLogger.info("Exiting");
}
4

2 に答える 2

6

次のようにルートを変更できます。

<route>
    <from uri="sftp:(url)?sendEmptyMessageWhenIdle=true" />
    <choose>
        <when>
            <simple>${body} != null</simple>
            <process ref="(processor)" />
            <to uri="(endpoint)" />
        </when>
        <otherwise>
            <process ref="(shutdownProcessor)" />
        </otherwise>
    </choose>
</route>

使用上の注意sendEmptyMessageWhenIdle=true

そしてここにあるshutdownProcessor

public class ShutdownProcessor {
    public void stop(final Exchange exchange) {
        new Thread() {
            @Override
            public void run() {
                try {
                    exchange.getContext().stop();
                } catch (Exception e) {
                    // log error
                }
            }
        }.start();
    }
}

実際、私はこのコードを実行しませんでしたが、期待どおりに動作するはずです。

于 2012-10-16T17:39:56.090 に答える
1

このFAQも参照したかっただけです http://camel.apache.org/how-can-i-stop-a-route-from-a-route.html

于 2012-10-17T08:53:26.647 に答える