How to change the database causes updates to Web client views


Environment: Java EE 7, Glassfish 4, MySQL 5.7 on Linux on an ARM7 server with 512MB ram (app must be memory frugal!) Application: A database-driven Java web app served to multiple Android tablet clients running Android 4.0 with the default browser.

The clients will have two basic user types: Producers and Consumers. Producers generate Tasks that are displayed on Consumers' browsers. When a Producer creates a new Task, it should immediately show up on all logged-in Consumers' browsers if they have the TaskList (viewTaskList.jsp) displayed with a "PENDING" status. Consumers select Tasks from the TaskList and process them (doTask.jsp). When as Task is selected by a Consumer, it should be flagged as "WORKING" on all other TaskList displays and not be selectable. If the Consumer who has a Task open clicks a Cancel button, the Task status should be changed to "PENDING" on all TaskLists. When a Task is completed, it is flagged as such, and should disappear from any Consumers' browsers that are displaying the TaskList view.

Sample TaskList view

Task entity class: (fragment)

public class Task {
   private int taskId;
   private String taskDescription;
   private boolean isComplete;
   private String completerId; // the userId of the Consumer who marked this as Complete (foreign key);
   private boolean isLocked;
   private String lockId;  // the userId of a Consumer who has this task open;


I am learning to do Java EE programming, and not proficient in JPA and JSF yet, but understand the basic concepts. From what I've seen here and elsewhere, I think Comet and/or JQuery/AJAX may help me solve this, but I don't know how to use either of those. I need basic guidance in how to have the database updates cause changes to the Consumers' displays, without the Consumers requesting the changes. Is part of the solution adding one or more triggers to the MySQL database?

You need to implement a polling model here. On GUI implement a script that should make a server call every n seconds bring the latest data and show it on GUI, obviously on server side you need code that can bring data and generate response.

To implement polling you can set a function to be invoked after x time.

window.setInterval(dbCallFunction, x);

Once function invokes, make a server call, return results, you can set again function interval.

There are lot of javascript libraries that can solve this for you, I would recommend using JQuery, here is the link

Cheers !!