There are files:
- UserRepository.java
- UserService.java
- Thread.java
- Task.java
When the program starts, an error is displayed every minute (logical), that in the Task
class in the line List<User> users = userServices.findAllUsers();
I have nothing "accepted", i.e. Hibernate does not want to fulfill my request. A typical error is java.lang.NullPointerException
.
Question: why Hibernate does not want to take data from the users
table and output data in the stream, although during normal loading of the page (in the method, @getmapping) findAllUsers()
works?
Here are my files:
Application.properties:
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.username=USER spring.datasource.password=PASS spring.datasource.url=jdbc:mysql://LOCALHOST ## Hibernate Properties # The SQL dialect makes Hibernate generate better SQL for the chosen database spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect spring.jpa.show-sql=true spring.jpa.generate-ddl=true # Hibernate DDL auto (create, create-drop, validate, update) spring.jpa.hibernate.ddl-auto = update spring.datasource.initialization-mode=always
UserRepository.java:
@Repository public interface UserRepository extends CrudRepository<User, Long> { List<User> findAll(); }
UserService.java:
@Service public class UserServices { private UserRepository userRepository; @Autowired public UserServices(UserRepository userRepository) { this.userRepository = userRepository; } public List<User> findAllUsers(){ return userRepository.findAll(); } }
Threads.java:
public class Threads { public void start() { Task task = new Task(); ScheduledExecutorService executorService = Executors.newScheduledThreadPool(3); executorService.scheduleAtFixedRate(task::downloadCalendars, 0, 1, TimeUnit.MINUTES); } }
Task.java:
public class Task { private Cutter cutter = new Cutter(); @Autowired private UserServices userServices; void downloadCalendars(){ try { LocalTime time = LocalTime.now(); if(time.getHour() == 0 && time.getMinute() == 0) { List<User> users = userServices.findAllUsers(); for (User user : users) { new Thread(() -> { try { cutter.updateCalendar(user); } catch (Exception e) { e.printStackTrace(); if (user != null) { System.out.println("проблема в user " + user.getId()); } } }).start(); } } } catch (Exception e){ e.printStackTrace(); } } }