Я довольно новичок в Spring Boot.
У меня есть приложение с начальной загрузкой, которое постоянно прослушивает сообщения JMS, используя @JmsListener, это прекрасно работает. (код ниже)
@Component
@Slf4j
public class DRGMessageReceiver {
private static final Logger log = LoggerFactory.getLogger(DRGMessageReceiver.class);
@Autowired
private Environment env;
@JmsListener(destination = "${ibm.mq.request.queueName}", containerFactory = "myFactory")
public void receiveMessage(String message, @Header(JmsHeaders.MESSAGE_ID) String messageId) throws Exception {
....
Я хотел бы добавить отчет по электронной почте, который отправляется ежедневно в полночь.
Я пытался добавить запланированный компонент, но он не работает. Я пытался создать запланированный компонент для регистрации каждые 5 секунд - он тоже не работает.
@Component
@Slf4j
public class DRGScheduler {
private static final Logger log = LoggerFactory.getLogger(com.drg.DRGScheduler.class);
@Autowired
private Environment env;
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
@Scheduled(cron = "0/5 * * * * *?")
public void every5seconds(){
log.info("every5seconds: time is {}", dateFormat.format(new Date()));
}
@Scheduled(cron = "0 0 0 0 1 ?") //send mails daily at 1am
public void sendDailyMails(){
log.info("sendDailyMails: time is {}", dateFormat.format(new Date()));
}
}
Вот мое приложение SpringBoot:
@EnableJms
@EnableScheduling
@SpringBootApplication
@Configuration
@EnableConfigurationProperties(MailConfigProperties.class)
class DRGSpringBootApplication {
@Autowired
EmailSender emailSender
static void main(String[] args) {
ConfigurableApplicationContext context =SpringApplication.run(DRGSpringBootApplication.class, args)
}
//If I add the following, the JMS Listener doesn't fire when I pus a message on the queue
@Bean
public TaskScheduler taskScheduler() {
return new ConcurrentTaskScheduler();
}
@Bean
JmsListenerContainerFactory<?> myFactory(ConnectionFactory connectionFactory,
DefaultJmsListenerContainerFactoryConfigurer configurer) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory()
factory.setMaxMessagesPerTask(1)
factory.setConcurrency("5-10")
factory.setSessionTransacted(true)
factory.setErrorHandler(new DefaultErrorHandler())
configurer.configure(factory, connectionFactory)
return factory
}
}
Любая помощь будет принята с благодарностью. Как я уже сказал, я новичок в Spring Boot.
Могу ли я иметь задачи JMS и запланированные в одном приложении Spring Boot?
Если нет, могу ли я поделиться lib и config?