Results 1 to 4 of 4

Thread: some stupid question

  1. #1

    Default some stupid question

    hi

    I am newbie of Spring Batch (2.0.0 release), after find some tutorial and read through the documentation, I still have some stupid question about it :

    1) what the diff of tasklet and org.springframework.batch.item ( is that any domain name for this package ? ) like reader, writer, processor ?

    2) which scenario use tasklet and when use the item reader, writer ?

    3) Since the tasklet is run within a transaction, is it support the restart, retry, and commit-interval ?

    4) Is mail notification suit for batch job naturally ?

    I have a scenario like this :

    get user -> search the item base on user's criterion -> mail to user

    so i assume the it will be reader() -> process() -> writer(), am i right ?

    in this scenario, can it write by tasklet too ? or is should follow method above ?

    this question maybe stupid, but i think that if mail notification can send put in batch job, it can take advantages of restart or retry if the task fail in the middle.

    any idea ?

    happy hacking
    -------------
    kiwi

  2. #2
    Join Date
    Feb 2008
    Posts
    488

    Default

    A Chunk-Oriented Step (with a reader, processor, writer) is used when the step needs to iterate through and handle many similar records, i.e. from a flat file or database query. A Tasklet Step is used when the step simply needs to execute a single method call (Tasklet.execute()).

    The TaskletStep supports restart and retry. However, commit-interval doesn't apply since that is referring to the number of items to process in a tranaction, and the Tasklet doesn't deal with items.

    We may add support for email sending in the near future.

    Your scenario of the reader/processor/writer makes sense for the get/search/send. You could certainly do it in a Tasklet if you wanted, but the chunk-oriented approach fits better if you are doing the same thing for multiple users.

  3. #3

    Default

    ok.. clear now.

    thx !

    kiwi

  4. #4
    Join Date
    Jan 2008
    Location
    San Diego
    Posts
    780

    Default

    Quote Originally Posted by DHGarrette View Post
    We may add support for email sending in the near future.
    It's pretty trivial to create a tasklet that is wired up to a spring MailSender. In most cases, the implementation that is more important is WHAT to send, not how to send it. However providing an abstract implemention of an EmailTasklet that reuses the interaction with the MailSender would be more generally useful; a user can then plugin in their specific logic for generating the message content.

    Here's what mine looks like. All of the email parameters and MailSender are wired up in the spring bean config as runtime parameters:

    Code:
    **
     * A tasklet that sends an email to indicate that the batch is complete.
     *
     * @author Charles Hudak
     * @since Mar 5, 2009
     *
     */
    public class SendDeviceBatchCompleteEmailTasklet implements Tasklet, StepExecutionListener
    {
        private StepExecution stepExecution;
        
        private MailSender mailSender;
        private String[] recipients;
        private String sender;
        private String subject;
        private String message;
    
        /**
         * Set the mail sender.
         * 
         * @param mailSender the sender
         */
        public void setMailSender(MailSender mailSender)
        {
            this.mailSender = mailSender;
        }
    
        /**
         * Set the recipients for the message.
         * 
         * @param recipients the recipients
         */
        public void setRecipients(String[] recipients)
        {
            this.recipients = recipients;
        }
    
        /**
         * Set the sender for the email.
         * 
         * @param sender the sender
         */
        public void setSender(String sender)
        {
            this.sender = sender;
        }
    
        /**
         * Set the subject to be sent for the email.
         * 
         * @param subject the subject string
         */
        public void setSubject(String subject)
        {
            this.subject = subject;
        }
    
        /**
         * Set the message format style message to be sent
         * in the email body. The batch id and batch filename 
         * will be used as arguments, in that order.
         * 
         * @param message the message format style string
         */
        public void setMessage(String message)
        {
            this.message = message;
        }
    
        /* (non-Javadoc)
         * @see org.springframework.batch.core.step.tasklet.Tasklet#execute(org.springframework.batch.core.StepContribution, org.springframework.batch.core.scope.context.ChunkContext)
         */
        @Override
        public RepeatStatus execute(StepContribution stepcontribution, ChunkContext chunkcontext) throws Exception
        {
            Long batchId = this.stepExecution.getJobParameters().getLong(BatchDeviceProcessingConstants.BATCH_UPLOAD_HISTORY_ID);
            String batchFile = this.stepExecution.getJobParameters().getString(BatchDeviceProcessingConstants.BATCH_UPLOAD_FILE_NAME);
            
            String formattedMessage = MessageFormat.format(this.message, batchId, batchFile);
            
            SimpleMailMessage mailMessage = new SimpleMailMessage();
            mailMessage.setFrom(this.sender);
            mailMessage.setTo(this.recipients);
            mailMessage.setSubject(this.subject);
            mailMessage.setText(formattedMessage);
    
            mailSender.send(mailMessage);
            
            return RepeatStatus.FINISHED;
        }
    
        /* (non-Javadoc)
         * @see org.springframework.batch.core.StepExecutionListener#afterStep(org.springframework.batch.core.StepExecution)
         */
        @Override
        public ExitStatus afterStep(StepExecution execution)
        {
            return null;
        }
    
        /* (non-Javadoc)
         * @see org.springframework.batch.core.StepExecutionListener#beforeStep(org.springframework.batch.core.StepExecution)
         */
        @Override
        public void beforeStep(StepExecution execution)
        {
            this.stepExecution = execution;
        }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •