Announcement Announcement Module
Collapse
No announcement yet.
DeleteFilesTasklet with filename pattern property Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • DeleteFilesTasklet with filename pattern property

    Hi,

    I would like to implement a tasklet deleting files matching a certain pattern in 'MultiResourceReader' style. What is the best approach to teach the tasklet class to evaluate the bean property the same way as MultiResourceReader (or its calling class) does?

    I think about the following bean definition:
    Code:
    	<bean id="deleteFilesTasklet" class="com.siemens.ces.sdsync.DeleteFileTasklet"
    		scope="step">
    		<property name="resources" value="#{jobParameters['inputFileNamePattern']}"/>
    	</bean>
    Code:
    public class DeleteFileTasklet implements Tasklet, InitializingBean {
    
    	private Resource[] resources;
    
    	public RepeatStatus execute(StepContribution contribution,
    			ChunkContext chunkContext) throws Exception {
    
    		for (int i = 0; i < resources.length; i++) {
    			File file = resources[i].getFile();
    			Assert.state(file.isFile());
    			boolean deleted = file.delete();
    			if (!deleted) {
    				throw new UnexpectedJobExecutionException(
    						"Could not delete file " + file.getName());
    			}
    		}
    		return RepeatStatus.FINISHED;
    	}
    
    	public void setResources(Resource resources[]) {
    		Assert.notNull(resources, "The resources must not be null");
    		this.resources = (Resource[]) Arrays.asList(resources).toArray(
    				new Resource[resources.length]);
    	}
    
    	public void afterPropertiesSet() throws Exception {
    		Assert.notNull(resources, "files must be set");
    	}
    }
    Thanks in advance for your help,
    Elmar
    Last edited by mminella; Feb 11th, 2013, 09:08 AM. Reason: formatting

  • #2
    Hi guys,

    PathMatchingResourcePatternResolver does the trick:

    Code:
    <bean id="deleteFilesTasklet" class="com.siemens.ces.sdsync.DeleteFilesTasklet"
    scope="step">
    <property name="resources" value="#{jobParameters['inputFileNamePattern']}" />
    </bean>

    <bean id="resourcePatternResolver" class="org.springframework.core.io.support.PathMat chingResourcePatternResolver" />

    Code:
    public class DeleteFilesTasklet implements Tasklet, InitializingBean {

    private String resourcePattern;

    @Autowired
    private ResourcePatternResolver resourcePatternResolver;

    public RepeatStatus execute(StepContribution contribution,
    ChunkContext chunkContext) throws Exception {

    Resource[] resources = resourcePatternResolver.getResources(resourcePatte rn);
    for (Resource resource : resources) {
    File file = resource.getFile();
    Assert.state(file.isFile());
    boolean deleted = file.delete();
    if (!deleted) {
    throw new UnexpectedJobExecutionException(
    "Could not delete file " + file.getName());
    }
    }
    return RepeatStatus.FINISHED;
    }

    public void setResources(String resources) {
    Assert.notNull(resources, "The resources must not be null");
    this.resourcePattern = resources;
    }

    public void setResourcePatternResolver(ResourcePatternResolver resourcePatternResolver) {
    this.resourcePatternResolver = resourcePatternResolver;
    }

    public void afterPropertiesSet() throws Exception {
    Assert.notNull(resourcePattern, "files must be set");
    }
    }

    Comment


    • #3
      Hi guys,

      PathMatchingResourcePatternResolver does the trick:

      Code:
      <bean id="deleteFilesTasklet" class="com.siemens.ces.sdsync.DeleteFilesTasklet"
      scope="step">
      <property name="resources" value="#{jobParameters['inputFileNamePattern']}" />
      </bean>

      <bean id="resourcePatternResolver" class="org.springframework.core.io.support.PathMat chingResourcePatternResolver" />

      Code:
      public class DeleteFilesTasklet implements Tasklet, InitializingBean {

      private String resourcePattern;

      @Autowired
      private ResourcePatternResolver resourcePatternResolver;

      public RepeatStatus execute(StepContribution contribution,
      ChunkContext chunkContext) throws Exception {

      Resource[] resources = resourcePatternResolver.getResources(resourcePatte rn);
      for (Resource resource : resources) {
      File file = resource.getFile();
      Assert.state(file.isFile());
      boolean deleted = file.delete();
      if (!deleted) {
      throw new UnexpectedJobExecutionException(
      "Could not delete file " + file.getName());
      }
      }
      return RepeatStatus.FINISHED;
      }

      public void setResources(String resources) {
      Assert.notNull(resources, "The resources must not be null");
      this.resourcePattern = resources;
      }

      public void setResourcePatternResolver(ResourcePatternResolver resourcePatternResolver) {
      this.resourcePatternResolver = resourcePatternResolver;
      }

      public void afterPropertiesSet() throws Exception {
      Assert.notNull(resourcePattern, "files must be set");
      }
      }

      Comment

      Working...
      X