Menu

Rate this page:

Thanks for rating this page!

We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

Appointment Reminders with Java and Spark

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.

        
        
        
        
        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.

              
              
              
              
              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.

                    
                    
                    
                    
                    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.

                          
                          
                          
                          
                          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.

                                
                                
                                
                                
                                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 Agustin Camino Andrew Baker Kat King David Prothero Hector Ortega
                                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 browsing the Twilio tag on Stack Overflow.