You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "l.penet@senat.fr" <l....@senat.fr> on 2015/12/16 15:58:52 UTC

Determining parallely deployed versions of a webapp programmatically

Hi.

I have in some of my apps cron-like tasks, scheduled by libs such as quartz.

As I am also an happy user of the parallel deployment feature of tomcat, 
I was wondering whether, without special privileges such as those of the 
"admin" webapps, I could programatically determine if a webapp is the 
"top" version of a family of webapps.

So, if webapp versions :
* 1.2.1
* 1.2.2
* 1.2.3

are deployed, I should be capable to determine that 1.2.3 is the latest 
and only run the scheduled task in this last one.

Is there any API to achieve that ?

Thanks in advance,

Ludovic

|
| AVANT D'IMPRIMER, PENSEZ A L'ENVIRONNEMENT.
|


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Determining parallely deployed versions of a webapp programmatically

Posted by "l.penet@senat.fr" <l....@senat.fr>.
On 16/12/2015 15:58, l.penet@senat.fr wrote:
> Hi.
>
> I have in some of my apps cron-like tasks, scheduled by libs such as 
> quartz.
>
> As I am also an happy user of the parallel deployment feature of 
> tomcat, I was wondering whether, without special privileges such as 
> those of the "admin" webapps, I could programatically determine if a 
> webapp is the "top" version of a family of webapps.
>
> So, if webapp versions :
> * 1.2.1
> * 1.2.2
> * 1.2.3
>
> are deployed, I should be capable to determine that 1.2.3 is the 
> latest and only run the scheduled task in this last one.
>
> Is there any API to achieve that ?
I found no API and ended with the following :
1) store the ServletContext on startup, using a ServletContextListener
2) find the tomcat Context from the ServletContext, using MBeans

             MBeanServer mBeanServer = 
MBeanServerFactory.findMBeanServer(null).get(0);
             ObjectName name = new ObjectName("Catalina", "type", "Server");
             Server server = (Server) mBeanServer.getAttribute(name, 
"managedResource");
             Service service = server.findService("Catalina");
             Engine engine = (Engine) service.getContainer();
             Host host = (Host) engine.findChild(engine.getDefaultHost());
             Container[] children = host.findChildren();
             for(int i =  0 ; i < children.length ; i++) {
                 if(children[i] instanceof Context) {
                     Context ctx = (Context) children[i];
                     if(ctx.getServletContext() == sc) {
                         return ctx;
                     }
                 }
             }

3)  From the Context, go up to the host and scan contexts, finding those 
whose name starts with the same base name as ours (defined as our name 
stripped from an optionnal ##version ending)
             Host host = (Host) context.getParent();

             List<Context> ret = new ArrayList<>();

             Container[] children = host.findChildren();
             String shortName = 
StringUtils.substringBefore(context.getName(),"##");
             for(int i =  0 ; i < children.length ; i++) {
                 if(children[i] instanceof Context) {
                     Context cur = (Context) children[i];
                     if(StringUtils.startsWith(cur.getName(), shortName)) {
                         ret.add(cur);
                     }
                 }
             }
             return ret;

StringUtils is here org.apache.commons.lang3.StringUtils.

Note that you should not try to perform 2) in the ServletContextListner 
mentionned in 1), or you deadlock when calling, server.findService.

Any advice from a tomcat expert to enhance this solution welcomed. :-)

Ludovic

|
| AVANT D'IMPRIMER, PENSEZ A L'ENVIRONNEMENT.
|


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Determining parallely deployed versions of a webapp programmatically

Posted by "S.Booth" <s....@ed.ac.uk>.
On 16/12/15 14:58, l.penet@senat.fr wrote:
> I have in some of my apps cron-like tasks, scheduled by libs such as
> quartz.
> 
> As I am also an happy user of the parallel deployment feature of tomcat,
> I was wondering whether, without special privileges such as those of the
> "admin" webapps, I could programatically determine if a webapp is the
> "top" version of a family of webapps.
> 
> So, if webapp versions :
> * 1.2.1
> * 1.2.2
> * 1.2.3
> 
> are deployed, I should be capable to determine that 1.2.3 is the latest
> and only run the scheduled task in this last one.
> 
> Is there any API to achieve that ?
> 
> Thanks in advance,
> 
> Ludovic

I would expect a scheduler like Quartz to run background threads that
might give you class-loader leaks when you un-deploy the application.

I had a similar requirement to yours but compounded by having redundant
tomcat servers. I went the low-tech route of having a heartbeat servlet
pinged by an external cron job that way only a single server (and the
most up-to-date parallel deploy) runs the heartbeat code.

			Stephen


======================================================================
|epcc| Dr Stephen P Booth             Principal Architect       |epcc|
|epcc| s.booth@ed.ac.uk               Phone 0131 650 5746       |epcc|
======================================================================
-- 
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org