我是Spring Boot的新手。
我有一个Spring Boot应用程序,它使用@JmsListener不断监听JMS消息,这很好用。 (下面的代码)
@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()));
}
}
这是我的SpringBootApplication:
@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的新手。
我可以在同一个Spring Boot应用程序中同时拥有JMS和计划任务吗?
如果没有,我可以共享lib和config吗?