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.
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.
Next let's see how we create a new Appointment
.
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.
We will dig further into that function next.
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.
Let's look at how we handle this trigger.
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.
Next let's look at how the SMS is sent.
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.
That's it! We've successfully set up automated appointment reminders for our customers, which will be delivered via SMS.
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!
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.