Spring Integration: Stop a flow gracefully and avoid MessageDispatchingException

My program have to be able to stop and start it again one or another flow. I do it with integrationFlowRegistration.stop() / .start() methods. The problem is that wenn Lifecycle components are stopped hundreds of MessageDispatchingException's are thrown. Probably because some components are stopped and another are still trying to deliver some messages. Is there some best practice to stop the flow? Some standard way to block flow.inputChannel, wait until all channels have no messages to deliver and then stop all components of the flow? Below is junit test to reproduce this behavior.

public void testStopFlowGracefully() {
    final Queue<List<Integer>> inQueue = new LinkedBlockingQueue<>();
    MessageSource<List<Integer>> inMs = new AbstractMessageSource<List<Integer>>() {
        public String getComponentType() {
            return null;

        protected Object doReceive() {
            return inQueue.poll();

    QueueChannel resultChannel = new QueueChannel();

    IntegrationFlow integrationFlow = IntegrationFlows
            .from(inMs, c->c.poller(Pollers.fixedRate(50)))
            .channel(c -> c.executor(Executors.newFixedThreadPool(5)))
            .<Integer, Integer>transform(p -> {
                try {
                } catch (Exception e) { }                   
                return p * 2;})
            .log(l -> "!!!!!!!!!!!!!!!!!!!!!!! transformed int="+l)

    IntegrationFlowRegistration registration = this.flowContext.registration(integrationFlow).register();

    inQueue.add(IntStream.rangeClosed(1, 100).boxed().collect(Collectors.toList()));
    try {
    } catch (Exception e) {

    Message<?> msg = resultChannel.receive(2000);