Reputation:
I'm still a beginner with spring boot, I'm using spring JPA to fetch the data from multiple tables in the same database and everything going fine, I used to run my application at the Main
but here I have added a Controller
class and running things there, then i used @Scheduled(fixedRate=7000)
instead of creating an infinite loop to keep check the data from db and stay live, the application working fine but as far as at the running time application executed twice instead of once at the beginning before scheduling, a is there any idea about what happened here :
Mainclass :
@SpringBootApplication
@EnableScheduling
public class AccessingDataJpaApplication {
public static void main(String[] args) throws Exception{
SpringApplication.run(AccessingDataJpaApplication.class);
}
}
Controller
class :
@Controller
@EnableScheduling
public class MainController {
private static final Logger logger = LoggerFactory.getLogger(MainController.class);
@Autowired
private CustomerRepository customerRepository;
@Autowired
private MessageRepository messageRepository;
private Set<String> camps = new HashSet<String>();
@Bean
@Scheduled(fixedRate=7000)
public void run(){
logger.info("Running");
if((customerRepository.findAllByStatusAndCampType(0, 1).size()) > 0 ){
for(Customer customer : customerRepository.findAll()){
System.out.println(customer.getCampCd());
camps.add(customer.getCampCd());
}
System.out.println("----------------------------------------");
for(MessageCampain messagecampain : messageRepository.findAllByCampCdIn(camps)) {
System.out.println(messagecampain.toString());
}
System.out.println("------------------------------------------");
for(String value : camps) {
System.out.println(value);
}
}
}
}
execution log :
[ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
[ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
[ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
[ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
[ main] c.e.accessingdatajpa.MainController : Running
[ main] o.s.s.c.ThreadPoolTaskScheduler : Initializing ExecutorService 'taskScheduler'
[ main] c.e.a.AccessingDataJpaApplication : Started AccessingDataJpaApplication in 5.467 seconds (JVM running for 6.242)
[ scheduling-1] c.e.accessingdatajpa.MainController : Running
[ scheduling-1] c.e.accessingdatajpa.MainController : Running
you can notice that at Running
word
Upvotes: 1
Views: 88
Reputation: 90447
It is because you annotate the run()
in MainController
as @Bean
, which will creates a lite
mode bean called run
. (Spring represents this bean as the type of NullBean
internally)
So , the 1st call of 'Running' in the main thread is due to spring instantiate this run
bean. The remaining calls of 'Running' in the scheduling-1 thread are due to the the effect of @Scheduled
. So please remove @Bean
from the run()
as it does not have any points to create a null bean ...
@Scheduled(fixedRate=7000)
public void run(){
}
Upvotes: 1