Menu

Expand
Rate this page:

Appointment Reminders with Java and Spark

Ahoy! We now recommend you build your SMS appointment reminders with Twilio's built in Message Scheduling functionality. Head on over to the Message Resource documentation to learn more about scheduling SMS messages!

This is a Java 8 web application written using Spark that demonstrates how to send appointment reminders to your customers with Twilio SMS.

Check out this application on GitHub to download the code and read instructions on how to run it yourself. In this tutorial, we'll show you the key bits of code necessary to drive this use case.

Check out how Yelp uses SMS to confirm restaurant reservations for diners.

Let's get started! Click the button below to move on to the next step of the tutorial.

Let's get started!

Create the Quartz job scheduler

The Quartz scheduler is instantiated in the main method of our web application, before we set up the routes. We pass a reference to this scheduler to the controller so it can schedule jobs to send out appointment reminders. Note that by default, Quartz temporarily stores jobs in memory, but in production you can configure Quartz to store jobs in a data store of your choice.

Loading Code Sample...
        
        
        src/main/java/com/twilio/appointmentreminders/Server.java

        Create the Quartz job scheduler

        src/main/java/com/twilio/appointmentreminders/Server.java

        Next let's see how we create a new Appointment.

        Let's create some appointments

        Create an appointment

        Once validations pass and the appointment is persisted to the database.

        With scheduleJob a notification is scheduled based on the time of the appointment.

        Loading Code Sample...
              
              
              src/main/java/com/twilio/appointmentreminders/controllers/AppointmentController.java

              Create an appointment

              src/main/java/com/twilio/appointmentreminders/controllers/AppointmentController.java

              We will dig further into that function next.

              Schedule the reminder job

              Schedule the reminder job

              The controller uses the injected scheduler to set up a notification. The AppointmentScheduler class is used here to actually send out the notification via SMS through a Quartz trigger.

              Loading Code Sample...
                    
                    
                    src/main/java/com/twilio/appointmentreminders/controllers/AppointmentController.java

                    Schedule the reminder job

                    src/main/java/com/twilio/appointmentreminders/controllers/AppointmentController.java

                    Let's look at how we handle this trigger.

                    Configure the application to send SMS messages

                    Configure the application to send SMS messages

                    Every time a scheduled job is triggered by Quartz, an instance of the AppointmentScheduler class is created to handle the job. When the class is loaded, we create a RestClient to interact with the Twilio API using our account credentials.

                    Loading Code Sample...
                          
                          
                          src/main/java/com/twilio/appointmentreminders/util/AppointmentScheduler.java

                          Configure the application to send SMS messages

                          src/main/java/com/twilio/appointmentreminders/util/AppointmentScheduler.java

                          Next let's look at how the SMS is sent.

                          Send an SMS message from a background job

                          Send an SMS message from a background job

                          When the execute method is called on an AppointmentScheduler instance, we use the Twilio REST API client to actually send a formatted reminder message to our customer via SMS.

                          Loading Code Sample...
                                
                                
                                src/main/java/com/twilio/appointmentreminders/util/AppointmentScheduler.java

                                Scheduled task to send SMS messages

                                src/main/java/com/twilio/appointmentreminders/util/AppointmentScheduler.java

                                That's it! We've successfully set up automated appointment reminders for our customers, which will be delivered via SMS.

                                Where to next?

                                Where to next?

                                If you haven't already, be sure to check out the JavaDoc for the Twilio helper library and our guides for SMS and voice.

                                Did this help?

                                Thanks for checking out this tutorial! If you have any feedback to share with us, please reach out on Twitter... we'd love to hear your thoughts, and know what you're building!

                                Mario Celi Maylon Pedroso Hector Ortega David Prothero Kat King Andrew Baker Stephanie Marchante
                                Rate this page:

                                Need some help?

                                We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.

                                Loading Code Sample...
                                      
                                      
                                      

                                      Thank you for your feedback!

                                      Please select the reason(s) for your feedback. The additional information you provide helps us improve our documentation:

                                      Sending your feedback...
                                      🎉 Thank you for your feedback!
                                      Something went wrong. Please try again.

                                      Thanks for your feedback!

                                      thanks-feedback-gif