You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by syg6 <sy...@yahoo.com> on 2007/06/25 12:40:25 UTC

Programmatically stop/start context (webapp)

Hello,

I would like to be able to start and stop my 'cache' webapp from my own
'cache-admin' webapp. These two apps usually run on the same Tomcat
instance, but not always.

I am not sure if I should use ManagerServlet or HTMLManagerServlet, or
Embedded tomcat. I have found some examples on this newsgroup and onjava.com
but was unable to get them to work. Any ideas?

I would also like to somehow (Ajax perhaps) grab the contents of the log
file and display it, updating every 2-3 seconds, in my 'cache-admin' webapp
when the 'cache' webapp is started/stopped. I am using log4j for logging.

Many thanks for any help!
Bob
-- 
View this message in context: http://www.nabble.com/Programmatically-stop-start-context-%28webapp%29-tf3975317.html#a11284321
Sent from the Tomcat - User mailing list archive at Nabble.com.


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


Re: Programmatically stop/start context (webapp)

Posted by Johnny Kewl <jo...@kewlstuff.co.za>.
Syg, interesting question....

I think, that what would make it tricky, is the actually packaging of the 
solution.

In theory if you do this.... (I'm writing this blind, excuse the code)
import org.apache.catalina.ant.*;

StartTask startTask = new StartTask();
startTask.setUsername("admin");
startTask.setPassword("");
startTask.setUrl("http://localhost:8080/manager");
startTask.setPath("/ContextOfWebApp");
startTask.execute();

This is actually turned into a URL....request and sent to the manager... so 
in theory
if you want to you can just simulate the GET with the required params and 
http hdrs.

and a similar thing for stopping it.

Ok, but heres the tricky bit and if you can figure out how to do it in your 
dev environment, no problem.

The required jars are in the server main package.... so they will be missing 
from your servlet.
To develop you can just drop them in.... BUT then somehow when you actually 
package it, you have to take them out.

So... you will probably have to use "late binding".... ie Class.forname or 
something, so you can get it working in dev environment and omit the jars in 
deployment.
OR.... if you on an App server, just do it in a remote bean.

Anyway, I think its dead easy except for the working around the container 
class loader issue.

Try it in a seperate app, think it will be easy.... but when you bring it 
into another web-app, the fun will start.

Have fun

----- Original Message ----- 
From: "syg6" <sy...@yahoo.com>
To: <us...@tomcat.apache.org>
Sent: Monday, June 25, 2007 12:40 PM
Subject: Programmatically stop/start context (webapp)


>
> Hello,
>
> I would like to be able to start and stop my 'cache' webapp from my own
> 'cache-admin' webapp. These two apps usually run on the same Tomcat
> instance, but not always.
>
> I am not sure if I should use ManagerServlet or HTMLManagerServlet, or
> Embedded tomcat. I have found some examples on this newsgroup and 
> onjava.com
> but was unable to get them to work. Any ideas?
>
> I would also like to somehow (Ajax perhaps) grab the contents of the log
> file and display it, updating every 2-3 seconds, in my 'cache-admin' 
> webapp
> when the 'cache' webapp is started/stopped. I am using log4j for logging.
>
> Many thanks for any help!
> Bob
> -- 
> View this message in context: 
> http://www.nabble.com/Programmatically-stop-start-context-%28webapp%29-tf3975317.html#a11284321
> Sent from the Tomcat - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
> 


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


Re: Programmatically stop/start context (webapp)

Posted by Johnny Kewl <jo...@kewlstuff.co.za>.
Other post answers first part....

Second part of you interesting question...

What you could try do is intercept the System.out streams... and pass them 
on.
It wont be your fancy log4 formating, but you should be able to see the same 
stuff, that one see's in Netbeans.... I think thats all it does, to capture 
information.
something like.
System.setOut(new YourCustomPrintBuffer(System.out,this));

Remember you have to pass it back to the base (super) class, or else it wont 
end up in normal logs.

Alternatively... maybe instead of this heavy stuff, you can get the info you 
need from a Listener... dont know, maybe a guru can elaborate on how these 
Tomcat monitoring apps actually work.

Good luck.....


----- Original Message ----- 
From: "syg6" <sy...@yahoo.com>
To: <us...@tomcat.apache.org>
Sent: Monday, June 25, 2007 12:40 PM
Subject: Programmatically stop/start context (webapp)


>
> Hello,
>
> I would like to be able to start and stop my 'cache' webapp from my own
> 'cache-admin' webapp. These two apps usually run on the same Tomcat
> instance, but not always.
>
> I am not sure if I should use ManagerServlet or HTMLManagerServlet, or
> Embedded tomcat. I have found some examples on this newsgroup and 
> onjava.com
> but was unable to get them to work. Any ideas?
>
> I would also like to somehow (Ajax perhaps) grab the contents of the log
> file and display it, updating every 2-3 seconds, in my 'cache-admin' 
> webapp
> when the 'cache' webapp is started/stopped. I am using log4j for logging.
>
> Many thanks for any help!
> Bob
> -- 
> View this message in context: 
> http://www.nabble.com/Programmatically-stop-start-context-%28webapp%29-tf3975317.html#a11284321
> Sent from the Tomcat - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
> 


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


Re: Programmatically stop/start context (webapp)

Posted by syg6 <sy...@yahoo.com>.
In the end it turns out I was using the wrong MBean. Well, wrong in that, as
one user pointed out, shutting down a Host means shutting down every context
in that Host. In my case I only have one host but even so, after calling the
stop() method on the Host MBean, I was unable to re-start it by calling
start().

Using JConsole and MC4J (and tips from other users) I finally found the
correct MBean:

Catalina:J2EEApplication=none,J2EEServer=none,j2eeType=WebModule,name=//www.myapp.com/

I found this MBean using MC4J under MBean : Catalina : J2EEServer : J2EE
Application : WebModule. I guess in the Tomcat MBean world a WebModule is a
Context. At any rate, I am now able to call both start() and stop() on this
MBean and my webapp starts and stops accordingly.

Many thanks for everyone's patience and help!

Bob

-- 
View this message in context: http://www.nabble.com/Programmatically-stop-start-context-%28webapp%29-tf3975317.html#a11340965
Sent from the Tomcat - User mailing list archive at Nabble.com.


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


Re: Programmatically stop/start context (webapp)

Posted by syg6 <sy...@yahoo.com>.
Actually, in my case, stopping a Host should work since I only have one app
assigned to each Host:

<Server>
  <Service>
    <Engine>
      <Host name="www.myapp.com" ... >
        <Context path="" docBase="myapp" .../>
      </Host>
      <Host name="www.myAdminapp.com" ... >
        <Context path="admin" docBase="myAdminapp" ... />
      </Host>
    </Engine>
  </Service>
</Server>

With my code I successfully stop the 'myapp' 'Host', thus killing the
'myapp' Context. But calling start() don't do jack. :(

But I take your meaning, it would be better to stop the Context. The trouble
is I can't seem to find a Context MBean. I've been messing around with Lamda
Probe, which is a lot like Tomcat's Manager webapp but better. Its
shortcoming however is that it seems to only let you administer the Tomcat
instance in which it is deployed, thus it's not useful for what I want to
do, namely use JMX to manage a remote Tomcat.

I will look at MC4J ... anything to get this over with, my brain is frying
from the effort ...

Cheers,
Bob


Johnny Kewl wrote:
> 
> 
> Nice Bob.... I actually think you have cracked the JMX client stuff....
> I dont think that is the right operation, it does work, but if you 
> stop/start the whole host, every webapp on the host will stop and start.
> 
> What we actually need is the context with those operations, and there
> doesnt 
> seem to be one.
> There is another one under host, called deployer, it sounds promising but
> I 
> couldnt make sense of it.
> 
> Anyway.... it may sound like I have wriiten all the code, I havnt, just 
> wanted to tell you to get MC4J, if you havnt got it already.
> It will show you all the MBeans and let you play with the operations, and 
> properties... save you struggling with the code.
> 
> I couldnt figure out how to stop/start a web-app (context).... maybe you 
> will crack it.
> 
> Good Luck
> 
> 
> ----- Original Message ----- 
> From: "syg6" <sy...@yahoo.com>
> To: <us...@tomcat.apache.org>
> Sent: Wednesday, June 27, 2007 1:28 PM
> Subject: Re: Programmatically stop/start context (webapp)
> 
> 
>>
>> Well, I've made some progress.
>>
>> Basically, you follow all the steps to get Tomcat to load JMX support,
>> essentially editing your catalina.sh.
>>
>> Then you connect to Tomcat's JMX MBean server, choose a domain, choose an
>> MBean, choose an Operation, and execute it. The following code, mostly
>> pilfered, does all this:
>>
>> JMXServiceURL url = new
>> JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:6666/jmxrmi");
>>
>> Map map = new HashMap();
>> String[] credentials = new String[] { "monitorRole" , "QED" };
>> map.put("jmx.remote.credentials", credentials);
>>
>> JMXConnector conn = JMXConnectorFactory.connect(url, map);
>> MBeanServerConnection mbsc = conn.getMBeanServerConnection();
>> ObjectName objName = new
>> ObjectName("Catalina:host=www.myapp.com,type=Host");
>> MBeanInfo mbeanInfo = mbsc.getMBeanInfo(objName);
>>
>> Object[] params = new Object[0];
>> String[] signatures = new String[0];
>> mbsc.invoke(objName, action, params, signatures);
>>
>> conn.close();
>>
>> NOTES ON BOLD TEXT :
>>
>> 1. 6666 is the port I told Tomcat to listen on in my catalina.sh file.
>> The
>> params are the following:
>> CATALINA_OPTS="$CATALINA_OPTS ""-Dcom.sun.management.jmxremote
>> -Dcom.sun.management.jmxremote.port=6666
>> -Dcom.sun.management.jmxremote.ssl=false
>> -Dcom.sun.management.jmxremote.authenticate=false"
>>
>> 2. The host, www.myapp.com, is the name of my virtual host in my 
>> server.xml
>> file:
>> <Server>
>>  <Service>
>>    <Engine>
>>      <Host name="www.myapp.com" ...>
>>
>> If you want a list of operations exposed by an MBean just use the 
>> following
>> code:
>>
>> MBeanOperationInfo[] ops = mbeanInfo.getOperations();
>> for (int i = 0; i < ops.length; i++)
>> {
>>  MBeanOperationInfo op = ops[i];
>>  System.out.println("op: " + op.getName() + " , desc: " +
>> op.getDescription());
>> }
>>
>> The MBean I have chosen, "Catalina:host=www.myapp.com,type=Host", has
>> start(), stop(), init() and a couple other operations (methods).
>>
>> All is ok when I stop an already started Host. But when I then call 
>> start()
>> or init(), it does nothing, throws no Exception.
>>
>> What could I be doing wrong? I posted this question on the Java JMX forum 
>> as
>> well ...
>>
>> Many thanks!
>> Bob
>>
>>
>> -- 
>> View this message in context: 
>> http://www.nabble.com/Programmatically-stop-start-context-%28webapp%29-tf3975317.html#a11322642
>> Sent from the Tomcat - User mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To start a new topic, e-mail: users@tomcat.apache.org
>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>>
>> 
> 
> 
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Programmatically-stop-start-context-%28webapp%29-tf3975317.html#a11326854
Sent from the Tomcat - User mailing list archive at Nabble.com.


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


Re: Programmatically stop/start context (webapp)

Posted by Johnny Kewl <jo...@kewlstuff.co.za>.
YES.... good one.... and I found it now in MC4J as well... its under 
Domains->J2EEServer->J2EEApplication->WebModule->(host/name of 
servlet)->start property

Bob, I have no idea why the mapping is so different to JConsole.... in MC4J 
its not even under the MBean section.
Compared with Chucks find of  Catalina -> WebModule -> //[host]/[app] -> 
none -> none;

I think MC4J is more basic and probably a good bet.... well there you go, at 
least you know its possible, just got to get the code figured out.

I'm now wondering if recent logs are hidden in there somewhere as well.... 
that would blow me away ;)


----- Original Message ----- 
From: "Caldarale, Charles R" <Ch...@unisys.com>
To: "Tomcat Users List" <us...@tomcat.apache.org>
Sent: Wednesday, June 27, 2007 5:37 PM
Subject: RE: Programmatically stop/start context (webapp)


> From: Johnny Kewl [mailto:john@kewlstuff.co.za]
> Subject: Re: Programmatically stop/start context (webapp)
>
> What we actually need is the context with those operations,
> and there doesnt seem to be one.

Try using the MBeans tab of JConsole.  You can find the <Context>
elements under Catalina -> WebModule -> //[host]/[app] -> none -> none;
under the second "none" are attributes and operations, including stop(),
start(), and reload() methods.

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY
MATERIAL and is thus for use only by the intended recipient. If you
received this in error, please contact the sender and delete the e-mail
and its attachments from all computers.

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



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


RE: Programmatically stop/start context (webapp)

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: Johnny Kewl [mailto:john@kewlstuff.co.za] 
> Subject: Re: Programmatically stop/start context (webapp)
> 
> What we actually need is the context with those operations, 
> and there doesnt seem to be one.

Try using the MBeans tab of JConsole.  You can find the <Context>
elements under Catalina -> WebModule -> //[host]/[app] -> none -> none;
under the second "none" are attributes and operations, including stop(),
start(), and reload() methods.

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY
MATERIAL and is thus for use only by the intended recipient. If you
received this in error, please contact the sender and delete the e-mail
and its attachments from all computers.

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


Re: Programmatically stop/start context (webapp)

Posted by Johnny Kewl <jo...@kewlstuff.co.za>.
Nice Bob.... I actually think you have cracked the JMX client stuff....
I dont think that is the right operation, it does work, but if you 
stop/start the whole host, every webapp on the host will stop and start.

What we actually need is the context with those operations, and there doesnt 
seem to be one.
There is another one under host, called deployer, it sounds promising but I 
couldnt make sense of it.

Anyway.... it may sound like I have wriiten all the code, I havnt, just 
wanted to tell you to get MC4J, if you havnt got it already.
It will show you all the MBeans and let you play with the operations, and 
properties... save you struggling with the code.

I couldnt figure out how to stop/start a web-app (context).... maybe you 
will crack it.

Good Luck


----- Original Message ----- 
From: "syg6" <sy...@yahoo.com>
To: <us...@tomcat.apache.org>
Sent: Wednesday, June 27, 2007 1:28 PM
Subject: Re: Programmatically stop/start context (webapp)


>
> Well, I've made some progress.
>
> Basically, you follow all the steps to get Tomcat to load JMX support,
> essentially editing your catalina.sh.
>
> Then you connect to Tomcat's JMX MBean server, choose a domain, choose an
> MBean, choose an Operation, and execute it. The following code, mostly
> pilfered, does all this:
>
> JMXServiceURL url = new
> JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:6666/jmxrmi");
>
> Map map = new HashMap();
> String[] credentials = new String[] { "monitorRole" , "QED" };
> map.put("jmx.remote.credentials", credentials);
>
> JMXConnector conn = JMXConnectorFactory.connect(url, map);
> MBeanServerConnection mbsc = conn.getMBeanServerConnection();
> ObjectName objName = new
> ObjectName("Catalina:host=www.myapp.com,type=Host");
> MBeanInfo mbeanInfo = mbsc.getMBeanInfo(objName);
>
> Object[] params = new Object[0];
> String[] signatures = new String[0];
> mbsc.invoke(objName, action, params, signatures);
>
> conn.close();
>
> NOTES ON BOLD TEXT :
>
> 1. 6666 is the port I told Tomcat to listen on in my catalina.sh file. The
> params are the following:
> CATALINA_OPTS="$CATALINA_OPTS ""-Dcom.sun.management.jmxremote
> -Dcom.sun.management.jmxremote.port=6666
> -Dcom.sun.management.jmxremote.ssl=false
> -Dcom.sun.management.jmxremote.authenticate=false"
>
> 2. The host, www.myapp.com, is the name of my virtual host in my 
> server.xml
> file:
> <Server>
>  <Service>
>    <Engine>
>      <Host name="www.myapp.com" ...>
>
> If you want a list of operations exposed by an MBean just use the 
> following
> code:
>
> MBeanOperationInfo[] ops = mbeanInfo.getOperations();
> for (int i = 0; i < ops.length; i++)
> {
>  MBeanOperationInfo op = ops[i];
>  System.out.println("op: " + op.getName() + " , desc: " +
> op.getDescription());
> }
>
> The MBean I have chosen, "Catalina:host=www.myapp.com,type=Host", has
> start(), stop(), init() and a couple other operations (methods).
>
> All is ok when I stop an already started Host. But when I then call 
> start()
> or init(), it does nothing, throws no Exception.
>
> What could I be doing wrong? I posted this question on the Java JMX forum 
> as
> well ...
>
> Many thanks!
> Bob
>
>
> -- 
> View this message in context: 
> http://www.nabble.com/Programmatically-stop-start-context-%28webapp%29-tf3975317.html#a11322642
> Sent from the Tomcat - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
> 


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


Re: Programmatically stop/start context (webapp)

Posted by syg6 <sy...@yahoo.com>.
Well, I've made some progress.

Basically, you follow all the steps to get Tomcat to load JMX support,
essentially editing your catalina.sh.

Then you connect to Tomcat's JMX MBean server, choose a domain, choose an
MBean, choose an Operation, and execute it. The following code, mostly
pilfered, does all this:

JMXServiceURL url = new
JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:6666/jmxrmi");

Map map = new HashMap();
String[] credentials = new String[] { "monitorRole" , "QED" }; 
map.put("jmx.remote.credentials", credentials);
  	
JMXConnector conn = JMXConnectorFactory.connect(url, map);
MBeanServerConnection mbsc = conn.getMBeanServerConnection();
ObjectName objName = new
ObjectName("Catalina:host=www.myapp.com,type=Host");
MBeanInfo mbeanInfo = mbsc.getMBeanInfo(objName);

Object[] params = new Object[0];
String[] signatures = new String[0];
mbsc.invoke(objName, action, params, signatures);
  	
conn.close();

NOTES ON BOLD TEXT : 

1. 6666 is the port I told Tomcat to listen on in my catalina.sh file. The
params are the following:
CATALINA_OPTS="$CATALINA_OPTS ""-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=6666
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false"

2. The host, www.myapp.com, is the name of my virtual host in my server.xml
file:
<Server>
  <Service>
    <Engine>
      <Host name="www.myapp.com" ...>

If you want a list of operations exposed by an MBean just use the following
code:
  
MBeanOperationInfo[] ops = mbeanInfo.getOperations();
for (int i = 0; i < ops.length; i++)
{
  MBeanOperationInfo op = ops[i];
  System.out.println("op: " + op.getName() + " , desc: " +
op.getDescription());
}

The MBean I have chosen, "Catalina:host=www.myapp.com,type=Host", has
start(), stop(), init() and a couple other operations (methods). 

All is ok when I stop an already started Host. But when I then call start()
or init(), it does nothing, throws no Exception.

What could I be doing wrong? I posted this question on the Java JMX forum as
well ...

Many thanks!
Bob


-- 
View this message in context: http://www.nabble.com/Programmatically-stop-start-context-%28webapp%29-tf3975317.html#a11322642
Sent from the Tomcat - User mailing list archive at Nabble.com.


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