I have simple scheduler.
public class MailSendScheduler {
@Autowired
MailCollector mailCollector;
@Scheduled(cron = "0/10 * * * * *")
public void call() throws Exception {
log.info("Method executed " + new Date());
taskExecutor.initialize();
for (int i = 0; i < 15; i++) {
mailCollector.setText(Integer.toString(i));
taskExecutor.execute(mailCollector);
}
}
}
My config looks like this:
<bean id="mailCollector" class="ge.ddrc.school.mails.MailCollector" scope="prototype" >
<property name="mailSender" ref="mailSender" />
</bean>
Mail Controller is runnable class, and I log the integers in run() method.
@Component
public class MailCollector extends Thread {
public void run(){ log.info() }
}
What do you think what will be result? It should be from 0 to 14 is not it? But not it looks like this:
2,2,4,5,14,14,14,14,14,14,14,14,14,14,14.
It seems that mailController is Singleton. I think it is not creating new bean. But WAIT , If I write this:
ApplicationContext context = new ClassPathXmlApplicationContext("spring/mail-config.xml");
for (int i = 0; i < 15; i++) {
MailCollector mailCollector=(MailCollector) context.getBean("mailCollector");
mailCollector.setText(Integer.toString(i));
taskExecutor.execute(mailCollector);
}
the output is like this: 1,2,3,4,5,8,9,6,7,10,15,13,14,12,11.` And it is correct too! why this happens?
Aucun commentaire:
Enregistrer un commentaire