You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Mateu Yabar <ma...@foodreg.com> on 2011/05/19 11:30:46 UTC

getting current context

Hi to all,

I need to obtain the number of active sessions on the current application.
If written the following (import org.apache.catalina.Context, import
org.apache.catalina.Manager):
public int getActiveSession(){
Context context=null;
Manager manager=context.getManager();
return manager.getActiveSessions();
}

However I don't know who to get the current context. Is there any static
method from which I can get it?

THanks in advance!

Re: getting current context

Posted by Mateu Yabar <ma...@foodreg.com>.
Sorry! It is working.
I was just looking to another webbap path.

So this is the solution:
ObjectName name=new
ObjectName("Catalina:type=Manager,path=/NAME_OF_APP,host=localhost");
ManagementFactory.getPlatformMBeanServer().getAttribute(name,
"activeSessions");

However, is there any way to know which is the current webbap name (path)?
The only place I know I can find it is in the context, but as said, I don't
know how to get the context from an static method. Any help there?

On 19 May 2011 16:27, Mark Thomas <ma...@apache.org> wrote:

> On 19/05/2011 15:24, Mateu Yabar wrote:
> > What I have for now, after enableing JMX as descrived in
> > http://tomcat.apache.org/tomcat-5.5-doc/monitoring.html
> >
> > I have the following code:
> > ObjectName name=new
> > ObjectName("Catalina:type=Manager,path=/test,host=localhost");
> > ManagementFactory.getPlatformMBeanServer().invoke(name, "listSessionIds",
> > null, null);
> >
> > But the invocation return an emtpy string.
>
> What happens when you try the same thing via JConsole? JConsole will
> also confirm you are using the right object name (it looks right to me).
>
> Mark
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

Re: getting current context

Posted by Mark Thomas <ma...@apache.org>.
On 19/05/2011 15:24, Mateu Yabar wrote:
> What I have for now, after enableing JMX as descrived in
> http://tomcat.apache.org/tomcat-5.5-doc/monitoring.html
> 
> I have the following code:
> ObjectName name=new
> ObjectName("Catalina:type=Manager,path=/test,host=localhost");
> ManagementFactory.getPlatformMBeanServer().invoke(name, "listSessionIds",
> null, null);
> 
> But the invocation return an emtpy string.

What happens when you try the same thing via JConsole? JConsole will
also confirm you are using the right object name (it looks right to me).

Mark



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


Re: getting current context

Posted by Mateu Yabar <ma...@foodreg.com>.
What I have for now, after enableing JMX as descrived in
http://tomcat.apache.org/tomcat-5.5-doc/monitoring.html

I have the following code:
ObjectName name=new
ObjectName("Catalina:type=Manager,path=/test,host=localhost");
ManagementFactory.getPlatformMBeanServer().invoke(name, "listSessionIds",
null, null);

But the invocation return an emtpy string.


On 19 May 2011 15:45, chris derham <ch...@derham.me.uk> wrote:

> On Thu, May 19, 2011 at 10:33 AM, Mateu Yabar <mateu.yabar@foodreg.com
> >wrote:
>
> > After all day working on this, I'm still unable to use JMX to obtain the
> > context.
> > Please, can anyone gime me a hand?
> >
>
> Obviously if you have spent all day on it, you must have a lot of code.
> Just
> send the latest piece, with a description of how far along it gets and how
> it hasn't worked. Then we may be able to help you fix it
>
> Chris
>

Re: getting current context

Posted by chris derham <ch...@derham.me.uk>.
On Thu, May 19, 2011 at 10:33 AM, Mateu Yabar <ma...@foodreg.com>wrote:

> After all day working on this, I'm still unable to use JMX to obtain the
> context.
> Please, can anyone gime me a hand?
>

Obviously if you have spent all day on it, you must have a lot of code. Just
send the latest piece, with a description of how far along it gets and how
it hasn't worked. Then we may be able to help you fix it

Chris

Re: getting current context

Posted by Mateu Yabar <ma...@foodreg.com>.
After all day working on this, I'm still unable to use JMX to obtain the
context.
Please, can anyone gime me a hand?

Thanks!

On 19 May 2011 13:12, Mateu Yabar <ma...@foodreg.com> wrote:

> >
> > 2. The OP wants current sessions so a ServletContextListener that only
> > runs at context start and stop isn't going to help.
> >
>
> In ServletContextListener, arg0.getServletContext(), only gives a
> ApplicationContextFacade, but I can access apache context from there, so I
> can't get the active sessions.
>
> Any help with JMX?
>
> On 19 May 2011 12:24, Mark Thomas <ma...@apache.org> wrote:
>
>> On 19/05/2011 11:17, chris derham wrote:
>> >>
>> >>
>> >> 2. The OP wants current sessions so a ServletContextListener that only
>> >> runs at context start and stop isn't going to help.
>> >>
>> >
>> > I don't understand this - could you explain a little further, or point
>> me to
>> > some docs? I thought when a context starts, sessions can start against
>> it.
>> > How is this not going to help?
>>
>> Assuming the OP needed to access the ServletContext to get the current
>> sesison count (which they don't - it won't help at all) then the OP will
>> want to access the current session count from a servlet. Being able to
>> access the ServletContext from a ServletContextListener is zero help
>> when you are in a Servlet.
>>
>> Mark
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>>
>>
>

Re: getting current context

Posted by Mateu Yabar <ma...@foodreg.com>.
>
> 2. The OP wants current sessions so a ServletContextListener that only
> runs at context start and stop isn't going to help.
>

In ServletContextListener, arg0.getServletContext(), only gives a
ApplicationContextFacade, but I can access apache context from there, so I
can't get the active sessions.

Any help with JMX?

On 19 May 2011 12:24, Mark Thomas <ma...@apache.org> wrote:

> On 19/05/2011 11:17, chris derham wrote:
> >>
> >>
> >> 2. The OP wants current sessions so a ServletContextListener that only
> >> runs at context start and stop isn't going to help.
> >>
> >
> > I don't understand this - could you explain a little further, or point me
> to
> > some docs? I thought when a context starts, sessions can start against
> it.
> > How is this not going to help?
>
> Assuming the OP needed to access the ServletContext to get the current
> sesison count (which they don't - it won't help at all) then the OP will
> want to access the current session count from a servlet. Being able to
> access the ServletContext from a ServletContextListener is zero help
> when you are in a Servlet.
>
> Mark
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

Re: getting current context

Posted by Mark Thomas <ma...@apache.org>.
On 19/05/2011 11:17, chris derham wrote:
>>
>>
>> 2. The OP wants current sessions so a ServletContextListener that only
>> runs at context start and stop isn't going to help.
>>
> 
> I don't understand this - could you explain a little further, or point me to
> some docs? I thought when a context starts, sessions can start against it.
> How is this not going to help?

Assuming the OP needed to access the ServletContext to get the current
sesison count (which they don't - it won't help at all) then the OP will
want to access the current session count from a servlet. Being able to
access the ServletContext from a ServletContextListener is zero help
when you are in a Servlet.

Mark



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


Re: getting current context

Posted by chris derham <ch...@derham.me.uk>.
>
>
> 2. The OP wants current sessions so a ServletContextListener that only
> runs at context start and stop isn't going to help.
>

I don't understand this - could you explain a little further, or point me to
some docs? I thought when a context starts, sessions can start against it.
How is this not going to help?

Thanks

Chris

Re: getting current context

Posted by Mateu Yabar <ma...@foodreg.com>.
Thanks!

I'm looking through JMX (I don't nothing about it), but meanwhile, can
anyone tell me how to access the Context through JMX?

On 19 May 2011 11:41, Mark Thomas <ma...@apache.org> wrote:

> On 19/05/2011 10:36, Micka wrote:
> > Hi,
> >
> > You should implents ServletContextListener to get the context () :
> >
> > http://www.roseindia.net/servlets/ServletContextListener-example.shtml
> >
> >     @Override
> >     public void contextInitialized(ServletContextEvent arg0) {
> >         if (context == null) {
> >             context = arg0.getServletContext();
> >         }
> >     }
> >
> >
> >
> > It works well in my case ;)
>
> Nope. Won't work.
>
> 1. The OP wants Tomcat's Context object.
> org.apache.catalina.Context != javax.servlet.ServletContext
>
> 2. The OP wants current sessions so a ServletContextListener that only
> runs at context start and stop isn't going to help.
>
> 3. If the OP did want the ServletContext,
> ServletRequest.getServletContext() is the way to go.
>
> Mark
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

Re: getting current context

Posted by Mark Thomas <ma...@apache.org>.
On 19/05/2011 10:36, Micka wrote:
> Hi,
> 
> You should implents ServletContextListener to get the context () :
> 
> http://www.roseindia.net/servlets/ServletContextListener-example.shtml
> 
>     @Override
>     public void contextInitialized(ServletContextEvent arg0) {
>         if (context == null) {
>             context = arg0.getServletContext();
>         }
>     }
> 
> 
> 
> It works well in my case ;)

Nope. Won't work.

1. The OP wants Tomcat's Context object.
org.apache.catalina.Context != javax.servlet.ServletContext

2. The OP wants current sessions so a ServletContextListener that only
runs at context start and stop isn't going to help.

3. If the OP did want the ServletContext,
ServletRequest.getServletContext() is the way to go.

Mark



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


Re: getting current context

Posted by Micka <mi...@gmail.com>.
Hi,

You should implents ServletContextListener to get the context () :

http://www.roseindia.net/servlets/ServletContextListener-example.shtml

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        if (context == null) {
            context = arg0.getServletContext();
        }
    }



It works well in my case ;)



On Thu, May 19, 2011 at 11:30 AM, Mateu Yabar <ma...@foodreg.com>wrote:

> Hi to all,
>
> I need to obtain the number of active sessions on the current application.
> If written the following (import org.apache.catalina.Context, import
> org.apache.catalina.Manager):
> public int getActiveSession(){
> Context context=null;
> Manager manager=context.getManager();
> return manager.getActiveSessions();
> }
>
> However I don't know who to get the current context. Is there any static
> method from which I can get it?
>
> THanks in advance!
>

Re: getting current context

Posted by Mark Thomas <ma...@apache.org>.
On 19/05/2011 10:30, Mateu Yabar wrote:
> Hi to all,
> 
> I need to obtain the number of active sessions on the current application.
> If written the following (import org.apache.catalina.Context, import
> org.apache.catalina.Manager):
> public int getActiveSession(){
> Context context=null;
> Manager manager=context.getManager();
> return manager.getActiveSessions();
> }
> 
> However I don't know who to get the current context. Is there any static
> method from which I can get it?

There is no static method.

You have two options:
- implement org.apache.catalina.ContainerServlet
- use JMX

I'd go the JMX route since ContainerServlets can't be packaged with web
applications.

Mark



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