Futurescale, Inc. PureMVC Home

The PureMVC Framework Code at the Speed of Thought


Welcome, Guest. Please login or register.
July 28, 2017, 08:01:49 AM
Home Help Search Login Register
News: Please DM @puremvc on Twitter to gain forum access. Spambots are why we can't have nice things.
Pages: [1]
Print
Author Topic: Java Servlets with PureMVC  (Read 8621 times)
saad
Sr. Member
****
Posts: 65


View Profile
« on: July 02, 2015, 09:27:10 AM »

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?
« Last Edit: July 02, 2015, 11:15:23 PM by saad » Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2870



View Profile WWW
« Reply #1 on: July 07, 2015, 07:07:18 AM »

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.
Logged
saad
Sr. Member
****
Posts: 65


View Profile
« Reply #2 on: July 30, 2015, 09:19:07 AM »

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.
https://github.com/sshams/Entitlement/blob/master/src/Application.java

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.
https://github.com/sshams/Entitlement/blob/master/src/modules/entitlement/view/components/Router.java

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.
« Last Edit: July 30, 2015, 10:29:27 AM by saad » Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2870



View Profile WWW
« Reply #3 on: July 30, 2015, 03:04:36 PM »

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?
Logged
saad
Sr. Member
****
Posts: 65


View Profile
« Reply #4 on: July 30, 2015, 04:28:24 PM »

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 2.3.3.1).

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.
« Last Edit: July 30, 2015, 04:33:51 PM by saad » Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2870



View Profile WWW
« Reply #5 on: July 31, 2015, 09:25:39 AM »

Nice.
Logged
Pages: [1]
Print
Jump to: