PureMVC Architects Lounge

PureMVC Manifold => MultiCore Version => Topic started by: saad on July 02, 2015, 09:27:10

Title: Java Servlets with PureMVC
Post by: saad on July 02, 2015, 09:27:10
Hi Cliff,

Trying to get my head around implementing a plumbed multicore app with Java Servlets, wondering if there's an implementation or if you can please guide for startup mechanics. I see Servlet as Front Controller/Router intercepting requests and delegating on to PureMVC apparatus via it's Mediator. I've following steps under review.

1. Define a class implementing ServletContextListener that loads with the application server, instantiate Shell and other modules inside contextInitialized method, and plumb them followed by shell and module registration with the Servlet Context so references are available to Servlets in #2

2. Servlets are lazily instantiated and are available after ServletContext is constructed. Within each servlet's init method get reference to custom ServletContext, then reference to Shell or Modules and using a method like acceptRouter along with acceptInputPipe/acceptOutputPipe while passing this so a RouterMediator can be set upon it.

Any thoughts?

Title: Re: Java Servlets with PureMVC
Post by: puremvc on July 07, 2015, 07:07:18
If the application gets large it could be an issue. Can you set the application up to run as a daemon and then connect to it from a more lightweight servlet? You could instantiate a pool of modules to handle requests, and plumb a new one when needed, then return it to the pool when complete.

Title: Re: Java Servlets with PureMVC
Post by: saad on July 30, 2015, 09:19:07
Hi Cliff,

I've worked out an implementation with some inspirations from SpringMVC with application of FrontController pattern to Servlets. https://github.com/sshams/Entitlement (Customer/Product Demo)

There are three major integration points to connect the Servlet (Router) to the PureMVC apparatus.

1. ServletContextListener on contextInitialized instantiates the ShellFacade and passes servletContextEvent to startup. StartupCommand then instantiates modules (and plumbs) and registers them with the ServletContext using attributes.

2. common/PipeAwareModule includes acceptRouter function which then sends out the notification with router in it's body. RouterMediator responds to this notification to and sets the router as it's viewComponent and sets itself as a Delegate.
   a) https://github.com/sshams/Entitlement/blob/master/src/common/PipeAwareModule.java
   b) https://github.com/sshams/Entitlement/blob/master/src/modules/entitlement/view/RouterMediator.java

3. Servlet (Router) on init method retrieves it's related Module from Servlet Content and calls acceptRouter and passes itself.

Each additional module will have it's own Servlet/Router defining it's own set of URI's and will receive the Router via acceptRouter just like above.

So far it worked out fine but I'm open to any ideas, any other strategies that can polish it further from you or the community.

Title: Re: Java Servlets with PureMVC
Post by: puremvc on July 30, 2015, 03:04:36
That looks great. Did you think about the possibility of running this as a daemon so that you don't have to load and plumb the whole app for every request?

Title: Re: Java Servlets with PureMVC
Post by: saad on July 30, 2015, 04:28:24
yes and I didn't have to since ServletContext is loaded only once for the entire life of the container, that's where the shell/ApplicationFacade and modules are instantiated and plumbed. Tested with log statements and concurrency testing through multiple machines so shell and modules instantiation and plumbing happens for once only. https://github.com/sshams/Entitlement/blob/master/src/Application.java

Each request is handled through a single instance of the servlet but concurrency is achieved by running requests on separate threads.

But things can flip around if Developer implements SingleThreadModel (not recommended)
More on http://download.oracle.com/otndocs/jcp/servlet-3.0-fr-oth-JSpec/ (Section

Then in that case there would be a pool of Servlet/Router instances (but still not shell and modules) maintained by the container. In that scenario the workaround would be to manage a counter id and register a new RouterMediator (NAME + id) for each new Servlet/Router instance in a command.

Title: Re: Java Servlets with PureMVC
Post by: puremvc on July 31, 2015, 09:25:39