【问题标题】:How do I make a standalone Camel application that can automatically shutdown?如何制作可以自动关闭的独立 Camel 应用程序?
【发布时间】:2012-10-16 16:01:12
【问题描述】:

我有一个作为独立 Java 应用程序运行的简单文件传输应用程序。它从 SFTP 端点获取文件并将它们移动到另一个端点。

文件在传输后会从 SFTP 端点中删除。 当没有更多文件时,最好让程序结束。但是,我还没有找到可以启动 Camel 并使其有条件地结束的解决方案(例如 SFTP 端点中没有更多文件时)。我目前的解决方法是启动 Camel,然后让主线程休眠很长时间。然后用户必须手动终止应用程序(通过 CTRL+C 或其他方式)。

有没有更好的方法让应用程序自动终止?

以下是我当前的代码:

在 CamelContext(Spring App 上下文)中:

<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");
}

【问题讨论】:

    标签: java apache-camel


    【解决方案1】:

    你可以像这样改变你的路线:

    <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();
        }
    }
    

    实际上我并没有运行这段代码,但它应该可以按需要运行。

    【讨论】:

      【解决方案2】:

      我只是想向您指出此常见问题解答 http://camel.apache.org/how-can-i-stop-a-route-from-a-route.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-03
        • 2013-02-11
        相关资源
        最近更新 更多