You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Chiranga Alwis <ch...@gmail.com> on 2016/02/26 12:44:45 UTC

Apache Tomcat context update listeners

Hi,

I have currently created a LifecycleListener which listens to Tomcat's
context deployment event and loads data in a custom configuration file at
that point.

I have been trying to find out whether LifecycleListeners support context
update and modification events but yet in my understanding I could not
discover an appropriate resource to use.

Most specifically, no event type which is triggered at the point of Context
modification has been provided. Or does Tomcat first destroy the modified
context instance and create a new Context instance which means it calls a
destroy event on the Context and then create a new Context instance?

The ones that have come closest to the expectations are as follows:

 - A Context WatchedResource
<https://tomcat.apache.org/tomcat-6.0-doc/config/context.html> which simply
observes defined resources
   within a context. But yet in my understanding this does not allow me
   to monitor any modification within the context.

 - Java WatchService
<https://docs.oracle.com/javase/tutorial/essential/io/notification.html>

It also has to be noted that currently I am collecting all the deployed
Contexts at deployment to a map along with their corresponding custom
configuration objects. Hence, there has to be a mechanism through which I
am to identify the Context which is modified.

Any help with this will be highly appreciated.

Re: Apache Tomcat context update listeners

Posted by Chiranga Alwis <ch...@gmail.com>.
Thanks Mark, I will go through and try to understand this. If any
clarifications are required I will post here.

By the way the goal I am pursuing (if it is not clear) is to enable the
loading of a set of custom configurations for every Context. These
configurations cannot be included within the typical web.xml (as we intend
to separate out these custom configurations from the typical web.xml
configurations) Other that that, this file plays a similar role to that
played by the web.xml.

On Sun, Feb 28, 2016 at 3:04 AM, Mark Thomas <ma...@apache.org> wrote:

> On 27/02/2016 17:38, Chiranga Alwis wrote:
> > Well, what I am trying to achieve is as follows:
> >
> > I am trying to load certain custom configurations from a file which is
> > similar to web.xml in Tomcat, for each Context. We can define it globally
> > and also override the configurations at context level. For this I have
> > already created a custom Listener and have added it to lib folder.
> >
> > I have already constructed it in a manner in which for each configuration
> > the configurations are loaded at webapp deployment.
> >
> > But now I intend to perform this task not only at the initial deployment,
> > but also if someone makes a change to the webapp. I believe Tomcat also
> > dynamically reloads a context if modified, for example a modification to
> > the web.xml or a Java class. We don't have to switch off the Tomcat
> server
> > and restart it to load the new changes.
>
> You still haven't defined change.
>
> > In my understanding, in such a situation the Context instance that was
> > initially created at server startup is destroyed and a new Context
> instance
> > is created. I got this idea as I did not come across a new event type
> under
> > Tomcat component lifecycle which defines an event of updating a Tomcat
> > component such as a Context. The ones that exist are for starting and
> > destroying of Tomcat components.
> >
> > Is my understanding correct?
>
> No.
>
> > Or is it different?
>
> Yes.
>
> > Plus what is/are the most
> > appropriate LifeCycle events which I am to use in such a situation?
>
> It appears you are using Tomcat 6 since that is the version of the
> documentation you quoted in your first post in this thread.
>
> You really need to upgrade to at least 7.0.x where the behaviour is much
> better defined. 6.0.x is likely to have some undocumented edge cases
> that might surprise you.
>
> Then you need to read this:
> http://tomcat.apache.org/tomcat-7.0-doc/config/automatic-deployment.html
>
> Pay particular attention to the differences between reload and redeploy.
>
> Then look at the Lifecyle.CONFIGURE_START_EVENT event.
>
> Mark
>
> >
> > On Sat, Feb 27, 2016 at 6:17 PM, Mark Thomas <ma...@apache.org> wrote:
> >
> >> On 26 February 2016 19:09:59 GMT+00:00, Chiranga Alwis <
> >> chirangaalwis@gmail.com> wrote:
> >>> Well, sorry if the question is not clear.
> >>>
> >>> What I want to know is for what type of event we need to listen in
> >>> order to
> >>> carry out a task when a Context is modified. It would be great if there
> >>> is
> >>> a clear explanation on how this is to be checked.
> >>
> >> Again, define modified.
> >>
> >> Again, why do you want to run a task? What are you actually trying to
> >> achieve?
> >>
> >> Mark
> >>
> >>>
> >>> On Fri, Feb 26, 2016 at 5:21 PM, Mark Thomas <ma...@apache.org> wrote:
> >>>
> >>>> On 26/02/2016 11:44, Chiranga Alwis wrote:
> >>>>> Hi,
> >>>>>
> >>>>> I have currently created a LifecycleListener which listens to
> >>> Tomcat's
> >>>>> context deployment event and loads data in a custom configuration
> >>> file at
> >>>>> that point.
> >>>>>
> >>>>> I have been trying to find out whether LifecycleListeners support
> >>> context
> >>>>> update and modification events but yet in my understanding I could
> >>> not
> >>>>> discover an appropriate resource to use.
> >>>>
> >>>> Define update.
> >>>>
> >>>> Define modification.
> >>>>
> >>>> Better still, explain what it is you actually want to achieve rather
> >>>> than ask for help implementing your, potentially flawed, solution.
> >>>>
> >>>> Mark
> >>>>
> >>>> ---------------------------------------------------------------------
> >>>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> >>>> For additional commands, e-mail: users-help@tomcat.apache.org
> >>>>
> >>>>
> >>
> >>
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> >> For additional commands, e-mail: users-help@tomcat.apache.org
> >>
> >>
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

Re: Apache Tomcat context update listeners

Posted by Mark Thomas <ma...@apache.org>.
On 27/02/2016 17:38, Chiranga Alwis wrote:
> Well, what I am trying to achieve is as follows:
> 
> I am trying to load certain custom configurations from a file which is
> similar to web.xml in Tomcat, for each Context. We can define it globally
> and also override the configurations at context level. For this I have
> already created a custom Listener and have added it to lib folder.
> 
> I have already constructed it in a manner in which for each configuration
> the configurations are loaded at webapp deployment.
> 
> But now I intend to perform this task not only at the initial deployment,
> but also if someone makes a change to the webapp. I believe Tomcat also
> dynamically reloads a context if modified, for example a modification to
> the web.xml or a Java class. We don't have to switch off the Tomcat server
> and restart it to load the new changes.

You still haven't defined change.

> In my understanding, in such a situation the Context instance that was
> initially created at server startup is destroyed and a new Context instance
> is created. I got this idea as I did not come across a new event type under
> Tomcat component lifecycle which defines an event of updating a Tomcat
> component such as a Context. The ones that exist are for starting and
> destroying of Tomcat components.
> 
> Is my understanding correct?

No.

> Or is it different?

Yes.

> Plus what is/are the most
> appropriate LifeCycle events which I am to use in such a situation?

It appears you are using Tomcat 6 since that is the version of the
documentation you quoted in your first post in this thread.

You really need to upgrade to at least 7.0.x where the behaviour is much
better defined. 6.0.x is likely to have some undocumented edge cases
that might surprise you.

Then you need to read this:
http://tomcat.apache.org/tomcat-7.0-doc/config/automatic-deployment.html

Pay particular attention to the differences between reload and redeploy.

Then look at the Lifecyle.CONFIGURE_START_EVENT event.

Mark

> 
> On Sat, Feb 27, 2016 at 6:17 PM, Mark Thomas <ma...@apache.org> wrote:
> 
>> On 26 February 2016 19:09:59 GMT+00:00, Chiranga Alwis <
>> chirangaalwis@gmail.com> wrote:
>>> Well, sorry if the question is not clear.
>>>
>>> What I want to know is for what type of event we need to listen in
>>> order to
>>> carry out a task when a Context is modified. It would be great if there
>>> is
>>> a clear explanation on how this is to be checked.
>>
>> Again, define modified.
>>
>> Again, why do you want to run a task? What are you actually trying to
>> achieve?
>>
>> Mark
>>
>>>
>>> On Fri, Feb 26, 2016 at 5:21 PM, Mark Thomas <ma...@apache.org> wrote:
>>>
>>>> On 26/02/2016 11:44, Chiranga Alwis wrote:
>>>>> Hi,
>>>>>
>>>>> I have currently created a LifecycleListener which listens to
>>> Tomcat's
>>>>> context deployment event and loads data in a custom configuration
>>> file at
>>>>> that point.
>>>>>
>>>>> I have been trying to find out whether LifecycleListeners support
>>> context
>>>>> update and modification events but yet in my understanding I could
>>> not
>>>>> discover an appropriate resource to use.
>>>>
>>>> Define update.
>>>>
>>>> Define modification.
>>>>
>>>> Better still, explain what it is you actually want to achieve rather
>>>> than ask for help implementing your, potentially flawed, solution.
>>>>
>>>> Mark
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>>>> For additional commands, e-mail: users-help@tomcat.apache.org
>>>>
>>>>
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>>
>>
> 


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


Re: Apache Tomcat context update listeners

Posted by Mark Eggers <it...@yahoo.com.INVALID>.
Chiranga,

On 2/27/2016 9:38 AM, Chiranga Alwis wrote:
> Well, what I am trying to achieve is as follows:
> 
> I am trying to load certain custom configurations from a file which
> is similar to web.xml in Tomcat, for each Context. We can define it
> globally and also override the configurations at context level. For
> this I have already created a custom Listener and have added it to
> lib folder.
> 
> I have already constructed it in a manner in which for each
> configuration the configurations are loaded at webapp deployment.
> 
> But now I intend to perform this task not only at the initial
> deployment, but also if someone makes a change to the webapp. I
> believe Tomcat also dynamically reloads a context if modified, for
> example a modification to the web.xml or a Java class. We don't have
> to switch off the Tomcat server and restart it to load the new
> changes.
> 
> In my understanding, in such a situation the Context instance that
> was initially created at server startup is destroyed and a new
> Context instance is created. I got this idea as I did not come across
> a new event type under Tomcat component lifecycle which defines an
> event of updating a Tomcat component such as a Context. The ones that
> exist are for starting and destroying of Tomcat components.
> 
> Is my understanding correct? Or is it different? Plus what is/are the
> most appropriate LifeCycle events which I am to use in such a
> situation?
> 
> On Sat, Feb 27, 2016 at 6:17 PM, Mark Thomas <ma...@apache.org>
> wrote:
> 
>> On 26 February 2016 19:09:59 GMT+00:00, Chiranga Alwis < 
>> chirangaalwis@gmail.com> wrote:
>>> Well, sorry if the question is not clear.
>>> 
>>> What I want to know is for what type of event we need to listen
>>> in order to carry out a task when a Context is modified. It would
>>> be great if there is a clear explanation on how this is to be
>>> checked.
>> 
>> Again, define modified.
>> 
>> Again, why do you want to run a task? What are you actually trying
>> to achieve?
>> 
>> Mark
>> 
>>> 
>>> On Fri, Feb 26, 2016 at 5:21 PM, Mark Thomas <ma...@apache.org>
>>> wrote:
>>> 
>>>> On 26/02/2016 11:44, Chiranga Alwis wrote:
>>>>> Hi,
>>>>> 
>>>>> I have currently created a LifecycleListener which listens
>>>>> to
>>> Tomcat's
>>>>> context deployment event and loads data in a custom
>>>>> configuration
>>> file at
>>>>> that point.
>>>>> 
>>>>> I have been trying to find out whether LifecycleListeners
>>>>> support
>>> context
>>>>> update and modification events but yet in my understanding I
>>>>> could
>>> not
>>>>> discover an appropriate resource to use.
>>>> 
>>>> Define update.
>>>> 
>>>> Define modification.
>>>> 
>>>> Better still, explain what it is you actually want to achieve
>>>> rather than ask for help implementing your, potentially flawed,
>>>> solution.
>>>> 
>>>> Mark

My thoughts on this from an operations / sanity viewpoint.

I am always leery of changing web application configurations on the fly.
It's easy to get source code repositories not agreeing with production
code, not agreeing with Maven repositories (if you use Maven), etc.,
etc. Then when you go to debug a production problem, figuring out what
you're working with becomes challenging.

I much prefer using parallel deployment, and changing the web
application every time a configuration changes. Tomcat handles this
gracefully. We've found that Jenkins / Nexus / Maven / parallel
deployment makes it easy to manage test, UAT, and production releases.

The Jenkins job sends all interested parties email, which contains the
version of the product, who did the release, and where the release can
be found. There's also a record in the Jenkins logs, so we can look at a
product release history fairly easily.

That being said, if you absolutely want to change configurations on the
fly, I'd look at something like the following (please note that I am a
systems architect and NOT a Java developer):

1. Store your configuration in a Singleton
   Maybe use a ConcurrentHashMap

2. Populate the Singelton via a ServletContextListener on start up

3. Create a protected administrative interface
   Look at using the Remote Address Filter to provide protection

4. Reload the configuration using the protected administrative interface

I think this is ugly, and you should strongly consider releasing point
updates when the configuration changes.

. . . just my two cents
/mde/


Re: Apache Tomcat context update listeners

Posted by Chiranga Alwis <ch...@gmail.com>.
Well, what I am trying to achieve is as follows:

I am trying to load certain custom configurations from a file which is
similar to web.xml in Tomcat, for each Context. We can define it globally
and also override the configurations at context level. For this I have
already created a custom Listener and have added it to lib folder.

I have already constructed it in a manner in which for each configuration
the configurations are loaded at webapp deployment.

But now I intend to perform this task not only at the initial deployment,
but also if someone makes a change to the webapp. I believe Tomcat also
dynamically reloads a context if modified, for example a modification to
the web.xml or a Java class. We don't have to switch off the Tomcat server
and restart it to load the new changes.

In my understanding, in such a situation the Context instance that was
initially created at server startup is destroyed and a new Context instance
is created. I got this idea as I did not come across a new event type under
Tomcat component lifecycle which defines an event of updating a Tomcat
component such as a Context. The ones that exist are for starting and
destroying of Tomcat components.

Is my understanding correct? Or is it different? Plus what is/are the most
appropriate LifeCycle events which I am to use in such a situation?

On Sat, Feb 27, 2016 at 6:17 PM, Mark Thomas <ma...@apache.org> wrote:

> On 26 February 2016 19:09:59 GMT+00:00, Chiranga Alwis <
> chirangaalwis@gmail.com> wrote:
> >Well, sorry if the question is not clear.
> >
> >What I want to know is for what type of event we need to listen in
> >order to
> >carry out a task when a Context is modified. It would be great if there
> >is
> >a clear explanation on how this is to be checked.
>
> Again, define modified.
>
> Again, why do you want to run a task? What are you actually trying to
> achieve?
>
> Mark
>
> >
> >On Fri, Feb 26, 2016 at 5:21 PM, Mark Thomas <ma...@apache.org> wrote:
> >
> >> On 26/02/2016 11:44, Chiranga Alwis wrote:
> >> > Hi,
> >> >
> >> > I have currently created a LifecycleListener which listens to
> >Tomcat's
> >> > context deployment event and loads data in a custom configuration
> >file at
> >> > that point.
> >> >
> >> > I have been trying to find out whether LifecycleListeners support
> >context
> >> > update and modification events but yet in my understanding I could
> >not
> >> > discover an appropriate resource to use.
> >>
> >> Define update.
> >>
> >> Define modification.
> >>
> >> Better still, explain what it is you actually want to achieve rather
> >> than ask for help implementing your, potentially flawed, solution.
> >>
> >> Mark
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> >> For additional commands, e-mail: users-help@tomcat.apache.org
> >>
> >>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

Re: Apache Tomcat context update listeners

Posted by Mark Thomas <ma...@apache.org>.
On 26 February 2016 19:09:59 GMT+00:00, Chiranga Alwis <ch...@gmail.com> wrote:
>Well, sorry if the question is not clear.
>
>What I want to know is for what type of event we need to listen in
>order to
>carry out a task when a Context is modified. It would be great if there
>is
>a clear explanation on how this is to be checked.

Again, define modified.

Again, why do you want to run a task? What are you actually trying to achieve?

Mark

>
>On Fri, Feb 26, 2016 at 5:21 PM, Mark Thomas <ma...@apache.org> wrote:
>
>> On 26/02/2016 11:44, Chiranga Alwis wrote:
>> > Hi,
>> >
>> > I have currently created a LifecycleListener which listens to
>Tomcat's
>> > context deployment event and loads data in a custom configuration
>file at
>> > that point.
>> >
>> > I have been trying to find out whether LifecycleListeners support
>context
>> > update and modification events but yet in my understanding I could
>not
>> > discover an appropriate resource to use.
>>
>> Define update.
>>
>> Define modification.
>>
>> Better still, explain what it is you actually want to achieve rather
>> than ask for help implementing your, potentially flawed, solution.
>>
>> Mark
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>>
>>



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


Re: Apache Tomcat context update listeners

Posted by Chiranga Alwis <ch...@gmail.com>.
Well, sorry if the question is not clear.

What I want to know is for what type of event we need to listen in order to
carry out a task when a Context is modified. It would be great if there is
a clear explanation on how this is to be checked.

On Fri, Feb 26, 2016 at 5:21 PM, Mark Thomas <ma...@apache.org> wrote:

> On 26/02/2016 11:44, Chiranga Alwis wrote:
> > Hi,
> >
> > I have currently created a LifecycleListener which listens to Tomcat's
> > context deployment event and loads data in a custom configuration file at
> > that point.
> >
> > I have been trying to find out whether LifecycleListeners support context
> > update and modification events but yet in my understanding I could not
> > discover an appropriate resource to use.
>
> Define update.
>
> Define modification.
>
> Better still, explain what it is you actually want to achieve rather
> than ask for help implementing your, potentially flawed, solution.
>
> Mark
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

Re: Apache Tomcat context update listeners

Posted by Mark Thomas <ma...@apache.org>.
On 26/02/2016 11:44, Chiranga Alwis wrote:
> Hi,
> 
> I have currently created a LifecycleListener which listens to Tomcat's
> context deployment event and loads data in a custom configuration file at
> that point.
> 
> I have been trying to find out whether LifecycleListeners support context
> update and modification events but yet in my understanding I could not
> discover an appropriate resource to use.

Define update.

Define modification.

Better still, explain what it is you actually want to achieve rather
than ask for help implementing your, potentially flawed, solution.

Mark

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