Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is what the code looks like with @Component:</p> <p>Main class that schedules the job:</p> <pre><code>public class NotificationScheduler { private SchedulerFactory sf; private Scheduler scheduler; @PostConstruct public void initNotificationScheduler() { try { sf = new StdSchedulerFactory("spring/quartz.properties"); scheduler = sf.getScheduler(); scheduler.start(); // test out sending a notification at startup, prepare some parameters... this.scheduleImmediateNotificationJob(messageParameters, recipients); try { // wait 20 seconds to show jobs logger.info("sleeping..."); Thread.sleep(40L * 1000L); logger.info("finished sleeping"); // executing... } catch (Exception ignore) { } } catch (SchedulerException e) { e.printStackTrace(); throw new RuntimeException("NotificationScheduler failed to retrieve a Scheduler instance: ", e); } } public void scheduleImmediateNotificationJob(){ try { JobKey jobKey = new JobKey("key"); Date fireTime = DateBuilder.futureDate(delayInSeconds, IntervalUnit.SECOND); JobDetail emailJob = JobBuilder.newJob(EMailJob.class) .withIdentity(jobKey.toString(), "immediateEmailsGroup") .build(); TriggerKey triggerKey = new TriggerKey("triggerKey"); SimpleTrigger trigger = (SimpleTrigger) TriggerBuilder.newTrigger() .withIdentity(triggerKey.toString(), "immediateEmailsGroup") .startAt(fireTime) .build(); // schedule the job to run Date scheduleTime1 = scheduler.scheduleJob(emailJob, trigger); } catch (SchedulerException e) { logger.error("error scheduling job: " + e.getMessage(), e); e.printStackTrace(); } } @PreDestroy public void cleanup(){ sf = null; try { scheduler.shutdown(); } catch (SchedulerException e) { e.printStackTrace(); } } </code></pre> <p>The EmailJob is the same as in my first posting except for the @Component annotation:</p> <pre><code>@Component public class EMailJob implements Job { @Autowired private JavaMailSenderImpl mailSenderImpl; ... } </code></pre> <p>And the Spring's configuration file has:</p> <pre><code>... &lt;context:property-placeholder location="classpath:spring/*.properties" /&gt; &lt;context:spring-configured/&gt; &lt;context:component-scan base-package="com.mybasepackage"&gt; &lt;context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation" /&gt; &lt;/context:component-scan&gt; &lt;bean id="mailSenderImpl" class="org.springframework.mail.javamail.JavaMailSenderImpl"&gt; &lt;property name="host" value="${mail.host}"/&gt; &lt;property name="port" value="${mail.port}"/&gt; ... &lt;/bean&gt; &lt;bean id="notificationScheduler" class="com.mybasepackage.notifications.NotificationScheduler"&gt; &lt;/bean&gt; </code></pre> <p>Thanks for all the help!</p> <p>Marina</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload