On 16/12/2015 15:58, l.pe...@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-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to