如何停止文件轮询并关闭相关的应用程序上下文?

时间:2015-08-12 20:25:12

标签: spring-integration spring-batch

我正在开发一个项目,我使用FilePoller来监听目录并在接收任何文件时调用批处理作业。我通过调用Main类[来自调度程序并传入参数如“startPoller”]来调用/启动进程,并且在main方法中,我只是加载应用程序上下文,事情就像魅力一样。

我们的Web管理员希望通过再次调用相同的Main类[使用参数“stopPoller”]来杀死/停止此FilePoller。我尝试使用但没有工作,因为它无法识别/关联最初创建的应用程序上下文。

有没有办法停止FilePoller并通过外部调用关闭相应的应用程序上下文?我真的需要随时停下来启动轮询器。我在代码中遗漏了什么吗?

调用/启动进程的代码:

    public class Main {
     public static void main (String args[]){
       if(args[0].equals("startPoller")) {
          ConfigurableApplicationContext  context = new ClassPathXmlApplicationContext(
                        "poller-context.xml");
          } 

      if(args[0].equals("stopPoller")){
               //I am stuck here...I can't get reference to the above created context/Job so as to stop/close them.

           /* This did not work out since its stopping the adapter on the            newly created context [ac] and not the one running already [context].

            ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext(
                    "poller-context.xml");

            MessageChannel controlChannel = ac.getBean("inboundFileChannel", MessageChannel.class);


            Message<String> operation = MessageBuilder.withPayload("@filePoller.stop()").build();

            //controlChannel.send(new GenericMessage<String>("@filePoller.shutdown()"));

            controlChannel.send(operation);

            ac.close();

            */

         }
          }

          }

配置XML:

             <int:channel id="inboundFileChannel" />
<int:channel id="outboundJobRequestChannel" />
<int:channel id="jobLaunchReplyChannel" />

<int-file:inbound-channel-adapter id="filePoller"
    channel="inboundFileChannel" directory="file:${pollFileInboundPath}"
    filename-pattern="*.*">
    <int:poller fixed-delay="1000" />
</int-file:inbound-channel-adapter>


<int:transformer input-channel="inboundFileChannel"
    output-channel="outboundJobRequestChannel">
    <bean
        class="com.my.batch.listener.FileMessageToJobRequest">
        <property name="job" ref="processFileBatchJob" />
        <property name="fileName" value="inputFile" />
    </bean>
</int:transformer>

<batch-int:job-launching-gateway
    request-channel="outboundJobRequestChannel" reply-     channel="jobLaunchReplyChannel" />

<int:logging-channel-adapter channel="jobLaunchReplyChannel" />

<int:control-bus input-channel="inboundFileChannel"/>

1 个答案:

答案 0 :(得分:0)

你不能那样做,因为第二次执行是一个不同的JVM;您可以使用Socket进行沟通:

public static void main(final String... args) throws Exception {

    if (args.length < 2) {
        System.err.println("Usage: start/stop <port>");
        System.exit(1);
    }

    int port = Integer.parseInt(args[1]);

    if ("start".equals(args[0])) {
        ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("poller-context.xml");
        ServerSocket serverSocket = ServerSocketFactory.getDefault().createServerSocket(port);
        serverSocket.accept();
        System.out.println("stopping...");
        context.close();
        System.out.println("...stopped");
    }
    else if ("stop".equals(args[0])) {
        Socket socket = SocketFactory.getDefault().createSocket("localhost", port);
        socket.close();
    }
    else {
        System.err.println("Usage: start/stop <port>");
        System.exit(3);
    }

}

在这种情况下,只需打开一个套接字即可关闭上下文。您可以通过仅绑定到localhost(用于安全性)和/或通过套接字发送一些神秘命令来关闭上下文来使其更复杂。

然后你运行,说

start 9999

stop 9999

您必须选择服务器上当前未使用的端口。