Announcement Announcement Module
Collapse
No announcement yet.
Spring scheduler Task Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring scheduler Task

    Hi,
    I use Spring Quartz scheduler(Clustered) in my application as follows. Inside the scheduler, i instantiate a thread class to spawn a new thread for each record and inside the thread class, i use reference of other bean class using wiring concept. As the thread class inside the scheduler is instantiated using the new operator, the dependency injection inside the thread class is not working. Is there any way, we can achieve this?

    From applicationContext.xml,

    <bean id="campaignNowJob" class="com.nervytech.mailer24x7.campaign.job.sched uler.CampaignNowJob"/>

    <bean id="campaignNowJobDetail" class="org.springframework.scheduling.quartz.Metho dInvokingJobDetailFactoryBean">
    <property name="targetObject" ref="campaignNowJob" />
    <property name="targetMethod" value="run" />
    <property name="concurrent" value="false" />
    </bean>

    <bean id="campaignNowTrigger" class="org.springframework.scheduling.quartz.Simpl eTriggerBean">
    <property name="jobDetail" ref="campaignNowJobDetail" />
    <property name="startDelay" value="6000" />
    <!-- repeat every 90 seconds -->
    <property name="repeatInterval" value="90000"/>
    </bean>

    <bean class="org.springframework.scheduling.quartz.Sched ulerFactoryBean">
    <property name="triggers">
    <list>
    <ref bean="campaignNowTrigger" />
    </list>
    </property>


    CampaignNow.java class,

    /**
    *
    */
    package com.nervytech.mailer24x7.campaign.job.scheduler;

    import java.io.File;
    import java.io.IOException;
    import java.util.List;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.RejectedExecutionException;
    import java.util.concurrent.ThreadPoolExecutor;

    import org.apache.commons.io.FileUtils;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autow ired;
    import org.springframework.core.task.TaskExecutor;

    import com.nervytech.mailer24x7.campaign.job.task.Campaig nTask;
    import com.nervytech.mailer24x7.client.exception.MailerEx ception;
    import com.nervytech.mailer24x7.common.enums.CampaignStat usEnum;
    import com.nervytech.mailer24x7.common.util.MailerUtil;
    import com.nervytech.mailer24x7.model.dao.CampaignDAO;
    import com.nervytech.mailer24x7.model.domains.Campaign;

    /**
    * @author bsikkaya
    *
    */
    public class CampaignNowJob {

    private static final Logger logger = LoggerFactory
    .getLogger(CampaignNowJob.class);

    @Autowired
    private CampaignDAO campaignDAO;

    public void run() {

    logger.info("CampaignNow Job started !!!!");

    try {

    ExecutorService exec = Executors
    .newFixedThreadPool(MailerUtil.THREADS_COUNT);
    ThreadPoolExecutor executor = (ThreadPoolExecutor) exec;

    List<Campaign> campaignsList = .....;

    for (Campaign cmpn : campaignsList) {

    CampaignTask task = new CampaignTask(Thread.currentThread().getName(), cmpn,
    content);

    try {
    executor .execute(task);
    } catch (RejectedExecutionException e) {
    // will be thrown if rejected execution handler
    // is not set with executor.setRejectedExecutionHandler
    e.printStackTrace();
    }

    logger.info("Campaign sent successfully for the campaignId : "+cmpn.getCampaignId());

    System.out.println("SUCCESSSSSSSSSSSSSSSSSSSSSs ");
    }

    shutDownAndWaitToFinish(executor);

    logger.info("CampaignNow Job Finished !!!!");
    System.out.println("FINISHEDDDDDDDDDDDDDDDDDDd");

    } catch (Exception e) {
    ---------------
    }
    }

    public static void shutDownAndWaitToFinish(ThreadPoolExecutor threadPool)throws Exception
    {
    // Code to shutdown .........
    }

    }


    Thread class,

    /**
    *
    */
    package com.nervytech.mailer24x7.campaign.job.task;

    import java.text.SimpleDateFormat;
    import java.util.Date;

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autow ired;

    import com.nervytech.mailer24x7.common.enums.CampaignStat usEnum;
    import com.nervytech.mailer24x7.common.util.MailerUtil;
    import com.nervytech.mailer24x7.mailgun.MailgunCampignSyn cer;
    import com.nervytech.mailer24x7.mailgun.MailgunSendUtil;
    import com.nervytech.mailer24x7.model.dao.SubscriberDAO;
    import com.nervytech.mailer24x7.model.domains.Campaign;

    /**
    * @author bsikkaya
    *
    */

    public class CampaignTask implements Runnable {

    private static final Logger logger = LoggerFactory
    .getLogger(CampaignTask.class);

    @Autowired
    private SubscriberDAO subscriberDAO;

    @Autowired
    private MailgunCampignSyncer mailgunCampignSyncer;

    private SimpleDateFormat fmt = new SimpleDateFormat(
    "yyyy-MM-dd hh:mm:ss.SSS");
    private String name;
    private Date created;
    private Campaign cmpn;
    private String content;


    public CampaignTask(String name, Campaign cmpn, String content) {
    this.name = name;
    this.cmpn = cmpn;
    this.content = content;
    this.created = new Date();
    }

    @Override
    public void run() {

    try {

    mailgunCampignSyncer.updateCampaignInMailgun(cmpn) ;

    MailgunSendUtil.sendMail(cmpn.getCampaignId(), cmpn.getSenderEmailId(), cmpn.getSubscriberListId(), cmpn.getOrgId(), cmpn.getSubject(), null, content, null);

    subscriberDAO.updateCampaignStatus(cmpn.getCampaig nId(), CampaignStatusEnum.COMPLETED.getStatus(), MailerUtil.formatter.format(new Date()));

    } catch (Exception e) {
    logger.error("Error sending campaign to :" + cmpn.getCampaignId()
    + " ", e);
    e.printStackTrace();
    }
    }

    @Override
    public String toString() {
    return name + ", created " + fmt.format(created);
    }

    }


    Thanks,
    Baskar.S
Working...
X