In this session, we will go step by step through the creation of a Google App Engine (GAE) application using the Java Runtime. This will be a beginner level session with respect to Google App Engine, but, a good understanding of how to build web applications in Java will be assumed. The code of the twitter bot sample application is available online.
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
SVCC Google App Engine: Java Runtime
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
Notas del editor
How many people are familiar with twitter? Explain concepts: followers, friends, tweets, and direct messages. Kritter makes all your followers friends automatically. It responds to certain commands sent from friends as direct messages and sends back responses to friends also as direct messages.
Can I have a volunteer? Anyone on twitter right now? If not, MrVanShizzle will have to fill in. =) Note: the long term vision is third party applications generating the KrillionKritter commands on behalf of users. For now, this is an easy way to test the functionality.
This has been quite the learning opportunity for me as a long time IntelliJ IDEA user. If the plugin for IDEA had not displayed passwords in the clear, I would have probably gone that route instead for this presentation. I highly recommend Fred Sauer's Sunday 9:15am GWT Session for those interested in GWT.
Due to sessions being shortened from 75 to 60 minutes, we won't be able to take time now to get folks setup. Sorry! Java getting started docs are excellent and work well for the Eclipse development approach. Twitter4J has worked flawlessly. Even using the HttpClient class from this project for my Krillion web service calls.
Mention logging setup in appengine.xml. Good idea to set INFO as default logging level for new projects. Nothing special in web.xml. Define my own custom task queues. Cron can be shut off by deploying an empty <cronentries/> element.
In my Eclipse setup, jdoconfig.xml was generated for me with reasonable defaults right out of the box. kritter.properties is where you configure the bot to work with the twitter account of your choosing. Checked in values are placeholders that you must change to some valid twitter account credentials.
Say more about datastore later. Focus on the task queue usage in this servlet: 1. Get named queue from factory 2. Add url to the queue 3. Uses builder pattern for adding parameters to the url. Note: Output a plain text response in parallel to logged information. This was really helpful during initial manual testing phase.
Each worker request provides new follower's ID as request parameter. Use this to verify existing friendship and establish new friendships as necessary. Send twitter direct message welcoming new friends. Persist new friend records in our datastore. You need to be careful with task handlers. If they fail, the task queue management logic will keep retrying them. They can also run more than once. Can you say ... Idempotence? =)
Say more about datastore later. Focus on the task queue usage in this servlet: 1. Get named queue from factory 2. Add url to the queue 3. Uses builder pattern for adding parameters to the url. Note: Output a plain text response in parallel to logged information. This was really helpful during initial manual testing phase.
Each worker request provides message text and the sender as request parameters. Any messages that match our command syntax are handled. Note: We update Friend record early on and add new Reply record at the end. Important to do single close of the PersistenceManager at the very end.
If you go the JDO route, the google documentation on JDO linked to here is a must read. Very well documented. In fact, my implementation of PMF is a copy/paste from the google documentation. This is highly recommended due to overhead associated with PersistenceManager initialization. My datastore may not be very complicated, but, I was extremely happy with how easy persistence was to implement for this application.
By retrieving latest FollowerStat in cron job processing, I can efficiently process only the new followers during each scheduled execution of the /followers job. By retrieving latest MessageStat in cron job processing, I can efficiently process only the new messages during each scheduled execution of the /messages job.
Friend entities are pretty straighforward. It will get more interesting when I add support to store multiple products or multiple zip code preferences per Friend. Reply entities capture the incoming command and the generated answer. Storing the parsed UPC code and Zip code as separate fields for possible future use. For example, looking up in the Reply table all the unique UPC codes searched for in a particular Zip code area.
Demonstrate how simple deployment is from within Eclipse IDE. I confess. I did most of my debugging with the logs and plain text responses during the initial manual testing. In theory, you deploy locally to debug. Get to know the App Engine web dashboard for your application. Deployed version management, log viewing, cron job status, viewing your datastore data, and quota stats are all there. It may not be sexy, but, there is a lot of useful functionality in it.
The kritter cron jobs spawn idempotent tasks, but, it is still a good idea to secure the URLs involved. This is something I will be adding soon I would like to add a dashboard to the webapp that uses Ajax to show a live stream of new followers and new replies. This is a perfect task for introducing GWT to this project. My current RegEx approach will not scale. Kritter code sorely needs to be properly documented. My bad.
First resource, is a Google I/O video that comes highly recommended. I'm a big fan of Rich Hightower's stuff. This is a three part tutorial that looks promising. Gaelyk looked interesting to me. At this point, there is also a Grails App Engine plugin. Of course, we can't leave Scala/Lift out of the equation either. =)
If you are still hungry for more, you can attend Jason Cooper's App Engine 101 session on Sunday at 1:00pm.