Announcement Announcement Module
Collapse
No announcement yet.
How to schedule mails using quartz Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to schedule mails using quartz

    Hai All, I need to send mails at regular intervals in my spring application. I want to use the Quartz support in the spring framework for this purpose. But i am not able to understand how to do it. I had gone through the following link.
    http://www.springframework.org/docs/...cheduling.html

    Though i got some idea about it i am not able to understand completely where to start it and how to start it. Can someone give me some idea about it?
    Thx in advance....

  • #2
    You need a way to send mails first. In this example I'm using Springs JavaMail helpers.

    So first you need two beans:
    Code:
      <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> 
        <property name="host" value="your.smtp.server"/> 
        <property name="username" value="your.username"/>
        <property name="password" value="your.password"/>
        <property name="javaMailProperties">
          <props>
            <prop key="mail.smtp.auth">true</prop>
          </props>
        </property>
      </bean>
    Then you need a message:
    Code:
    <bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">
      <property name="from" value="[email protected]"/>
    </bean>
    Now you need a Job that is to be executed.
    Code:
      public class EmailJob extends QuartzJobBean {
    
        // Getter and Setter Methods left out in this example, you need to add them
        private SimpleMailMessage mailMessage;
        private MailSender mailSender;
    
        protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
          mailMessage.setSubject("subject");
          mailMessage.setText("text");
          mailMessage.setTo("[email protected]");
          mailSender.send(mailMessage);
        }
      }
    And now three more beans to declare in your XML file:
    1) A JobDetail bean
    Code:
      <bean id="emailJob" class="org.springframework.scheduling.quartz.JobDetailBean">
        <property name="jobClass" value="EmailJob"/>
        <property name="jobDataAsMap">
          <map>
            <entry key="mailMessage">
    	  <ref bean="mailMessage"/>
            </entry>
            <entry key="mailSender">
              <ref bean="mailSender"/>
            </entry>
          </map>
        </property>
      </bean>
    As you can see, the JobDetailBean is used to instantiate your EmailJob and the jobDataAsMap can be used to inject everything you need.

    2) A trigger. I'm using the SimpleTriggerBean here - CronTrigger is available too for a much more fine-grained control over the execution of your job.
    Code:
      <bean id="emailTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
        <property name="jobDetail" ref="emailJob"/>
        <property name="repeatInterval" value="30000"/>
      </bean>
    As you can see: Quite simple.
    Repeat interval is in milliseconds, so this job would execute every thirty seconds. An additional property startDelay is possible.

    3) The last thing you need is a bean to register and start your triggers:
    Code:
      <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
          <list>
            <ref bean="sendReminderTrigger" />
          </list>
        </property>
      </bean>
    That's it. You can register as many triggers as you want with that SchedulerFactoryBean.

    Remember that the QuartzJobBean has no state!
    Last edited by _dodger_; Aug 5th, 2006, 07:36 AM.

    Comment


    • #3
      Thx

      Hi Dodger,
      Thx a lot for ur reply, I shall try it out the way u mentioned and c it.

      Thx once again..............

      Comment


      • #4
        Some Doubts..

        Hi Dodger,

        I have been working onthe sample example u gave and got some doubts regarding it. As i am new to this swing and quartz frameworks i am not able to resolve them quiet quickly.

        In the EmailJob class you provided, you mentioned that we have to add the getter and setter methods. where do we need to add them and what sholuld they contain?

        In the last bean in the example, you used
        <ref bean="sendReminderTrigger" />

        what is this bean and where should we write it?

        Also we have to send different types of messages to different people. Where should we read these messages and email-id's from? where should we maintain them?
        Hope you will clear all our doubts...,
        Thanks in advance....,,

        Comment


        • #5
          Hi!

          Originally posted by jkumar
          In the EmailJob class you provided, you mentioned that we have to add the getter and setter methods. where do we need to add them and what sholuld they contain?
          Ok here is the full EmailJob class:

          Code:
            public class EmailJob extends QuartzJobBean {
          
              private SimpleMailMessage mailMessage;
              private MailSender mailSender;
          
              protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
                mailMessage.setSubject("subject");
                mailMessage.setText("text");
                mailMessage.setTo("[email protected]");
                mailSender.send(mailMessage);
              }
          
              public SimpleMailMessage getMailMessage() { return mailMessage; }
              public void setMailMessage(SimpleMailMessage newMailMessage) { mailMessage = newMailMessage; }
          
              public MailSender getMailSender() { return mailSender; }
              public void setMailSender(MailSender newMailSender) { mailSender = newMailSender; }
            }
          As these is a fundamental concept of Spring (Dependency Injection) I recommend to read the reference manual or a good book (http://www.springframework.org/documentation).

          In the last bean in the example, you used <ref bean="sendReminderTrigger" />
          That was my mistake - it shoud be:
          Code:
          <ref bean="emailTrigger"/>
          in this example, I'm sorry.

          Also we have to send different types of messages to different people. Where should we read these messages and email-id's from? where should we maintain them?
          This one I can't really answer because it is specific to your business. But you can of course extend the EmailJob class and do whatever logic you need to determine the recipient and text.

          You can inject all your objects into EmailJob and use them to decide what to do.

          I'm sorry but I can't be more specific. If you need more help don't hesitate to ask again.

          Comment

          Working...
          X