mardi 2 janvier 2018

Is it good design to loop through many users? - SpringBoot

I am looking into good design principles and would just like some feedback.

I want to go through every user in my application that is a patient, check their scheduled activities, see if there is one upcoming and send them an email. It looks a little like this:

 List<Patient> listOfAllPatients = patientRepositoryJPA.findAll();

    if (listOfAllPatients.size() != 0) {

        for (Patient patient : listOfAllPatients) {

            user = userRepositoryJPA.findByUsername(patient.getUsername());

            Timestamp currentTimeAndDate = new Timestamp(System.currentTimeMillis());
            Long closestDate = Integer.toUnsignedLong(31);;

            List<Activity> activities = activityRepositoryJPA.findByPatientAndStartDateTimeAfter(patient, currentTimeAndDate);

            for (Activity activity : activities) {

                Timestamp activityDateAndTime = activity.getStartDateTime();

                Long difference = getTimeDifferenceByMinutes(currentTimeAndDate, activityDateAndTime);

                if (difference < closestDate) {

                    LocalDateTime upcomingDate = activity.getStartDateTime().toLocalDateTime();

                    emailHandler.sendEmail(javaMailSender, "Upcoming Activity Reminder", "The activity, " + activity.getName() + " is starting soon! (" +
                            upcomingDate.getHour() + ":" + upcomingDate.getMinute() + ")", user);
                }

            }
        }

Now here I loop through every patient in the application and then through each of their activities. If there were many user (millions) surely there would be some bottlenecks or something here.

Does anybody have any advice on how big companies would handle data like this?

Or is what I am doing fine? Thanks.

Aucun commentaire:

Enregistrer un commentaire