title: Spring Integration new file callback?
body: <p>I am using <code>FileWritingMessageHandler</code> with a custom <code>FileNameGenerator</code> which generates new filenames in intervals (similar to rolling file appender for <code>log4j</code>).</p>
  
  <pre><code>@Slf4j
  @Getter
  @AllArgsConstructor
  public class RolloverFileNameGenerator extends AbstractExpressionEvaluator implements FileNameGenerator {
    private final Map&lt;CachedFileEvaluation, String&gt; cache;
    private final RoundedBucketIntervalCalculator intervalCalculator;
    private final Map&lt;String, Object&gt; commonContext;
    private final Expression nameExpression;
  
    @SuppressWarnings("unchecked")
    public RolloverFileNameGenerator(final RoundedBucketIntervalCalculator intervalCalculator,
                                     final String nameExpression, final Map&lt;String, Object&gt; context, final int cacheMaxSize) {
      this(new LRUMap(cacheMaxSize), intervalCalculator, context, EXPRESSION_PARSER.parseExpression(nameExpression));
    }
  
    @SuppressWarnings("unchecked")
    public RolloverFileNameGenerator(final RoundedBucketIntervalCalculator intervalCalculator, final String nameExpression, final Map&lt;String, Object&gt; context) {
      this(null, intervalCalculator, context, EXPRESSION_PARSER.parseExpression(nameExpression));
    }
  
    @Override
    public String generateFileName(final Message&lt;?&gt; message) {
      return generateFileName(message, DateTime.now());
    }
  
    public String generateFileName(final Message&lt;?&gt; message, final DateTime dateTime) {
      final Interval interval = intervalCalculator.getInterval(dateTime);
  
      if (cache == null) {
        return evaluateFilenameFromMessage(message, interval);
      }
  
      final Optional&lt;String&gt; cached = cache.entrySet().stream()
          .filter(entry -&gt; entry.getKey().isBetweenInclusive(dateTime))
          .findFirst()
          .map(Map.Entry::getValue);
  
      return cached.orElseGet(() -&gt; {
        final String evaluatedValue = evaluateFilenameFromMessage(message, interval);
        cache.putIfAbsent(new CachedFileEvaluation(interval), evaluatedValue);
        return evaluatedValue;
      });
    }
  
    private String evaluateFilenameFromMessage(final Message&lt;?&gt; message, final Interval interval) {
      final Object evaluated = this.evaluateExpression(nameExpression, new RolloverEvaluationContext(commonContext, interval, message));
  
      if (evaluated == null) {
        throw new IllegalArgumentException("Evaluated expression is valid, but it resulted with a null");
      }
  
      if (!(evaluated instanceof String)) {
        throw new IllegalArgumentException(String.format("Evaluated expression is valid, but it didn't result in a String. Evaluated result: (%s) %s", evaluated.getClass(), evaluated));
      }
  
      return (String) evaluated;
    }
  
    @Data
    @AllArgsConstructor
    public class CachedFileEvaluation {
      private Interval interval;
  
      public boolean isBetweenInclusive(final DateTime date) {
        return interval.contains(date) || interval.getEnd().isEqual(date);
      }
    }
  
    @Getter
    @AllArgsConstructor
    private class RolloverEvaluationContext {
      private final Map&lt;String, Object&gt; ctx;
      private final Interval interval;
      private final Message&lt;?&gt; message;
    }
  }
  </code></pre>
  
  <p>From my configuration:</p>
  
  <pre><code>@Bean
  @ConditionalOnProperty(prefix = "com.github.alturkovic.rollover.writer", name = "initialize", havingValue = "true", matchIfMissing = true)
  public FileWritingMessageHandler fileWritingMessageHandler(final RolloverFileNameGenerator rolloverFileNameGenerator) {
    final Writer writer = properties.getWriter();
  
    final FileWritingMessageHandler handler = new FileWritingMessageHandler(new File(properties.getDirectory()));
    handler.setAppendNewLine(writer.isAppendNewLine());
    handler.setAutoCreateDirectory(writer.isAutoCreateDirectory());
    handler.setFileExistsMode(writer.getMode());
    handler.setFlushInterval(writer.getFlushInterval());
    handler.setFileNameGenerator(rolloverFileNameGenerator);
    handler.setOutputChannelName("nullChannel");
    return handler;
  }
  </code></pre>
  
  <p>Each time a new file has to be created, I would like to write a CSV header to it. I have looked into <code>FileWritingMessageHandler</code> and I can't figure out how to accomplish what I am trying to. </p>
  
  <p>Could someone point me in the right direction if this can be done without changing <code>FileWritingMessageHandler</code> source code?</p>