On Wed, Dec 21, 2011 at 12:09 AM, Pid * <p...@pidster.com> wrote:

> On 20 Dec 2011, at 15:47, S B <sbl...@gmail.com> wrote:
>
> > On Tue, Dec 20, 2011 at 7:00 PM, Pid <p...@pidster.com> wrote:
> >
> >> On 20/12/2011 11:57, S B wrote:
> >>> MBeanImpl code is packaged as a war and deployed in tomcat along with
> >> it's
> >>> mbeans-descriptor.xml
> >>> I am not using ServletContextListener in my code. (mbean registration
> is
> >>> done inside constructor).
> >>
> >> Right, but what is calling the constructor?
> >>
> >
> > constructor is run during server startup hook. MBean is registered during
> > that time .
>
> This is like pulling teeth. What exactly is the "server startup hook"?
>
>
> > Mbean is registered using below code (in constructor):
> >
> >            MBeanServer server = getServer();
> >            server.registerMBean(*this*, new
> > ObjectName(StoreServerSyncAndReloadConstants.MBEAN_NAME)
> >
> > When we invoke reload() from jconsole, it causes class to load and call
>
> How can it do that if it needs to be loaded already for the MBean to work?
>
> How are you unloading it?
>
> > it's public constructor and create instance before calling reload() on
> that
> > instance.
>
> Right. I can't make any sense out of the above.
>
>
> > During reload() constructor is called again and registration code runs
> > again.
>
> So you re-register the Mbean that you're using to load the same MBean?
>
>
> p
>

Sorry about the confusion. I'll try to put in a more clear way. Here is the
complete flow from beginning.

There is a *ConfiguratorLoadingServlet* which loads and initializes on
tomcat startup. *<load-on-startup>* in *web.xml* causes that to happen.

In *init()* method of *ConfiguratorLoadingServlet* it does new *MBeanImpl()
*which causes mbean registration code inside its constructor to run.
After that in same *init() *method it calls *DBManager.reload()* which
causes config properties to load from db during startup.

DBManager.reload() code is like this:
{
         InitialContext initContext = new InitialContext();
   Context context = (Context) initContext.lookup("java:comp/env");
   BasicDataSource ds = (BasicDataSource) context.lookup("jdbc/" +
dataSourceName);

   // use ds to connect to db and get records.
}

MBeanImpl constructor code is like this:

public MBeanImpl() {

        try {
            MBeanServer server = getServer();
            server.registerMBean(this, new
ObjectName(StoreServerSyncAndReloadConstants.MBEAN_NAME));
        } catch (Exception e) {
            logger.error("Error in Rgistering the Configurator MBean",e);
            logger.warn("Configurator is NOT Available through JMX");
        }


    }

in destroy() method of *ConfiguratorLoadingServlet* it does the
unregisterMBean.

*Above flow works fine during tomcat startup. records are loaded
successfully. *


*Problem shows up when I invoke reload() from jconsole gui.*
Here is the flow that fails.

click reload() on jconsole gui.
it causes constructor of MBeanImpl to run. (verified from logs.)
calls reload() defined in MBeanImpl.
calls DBManager.reload()
inside DBManager.reload() it fails at InitialContext initContext = new
InitialContext();
the exception is  -  javax.naming.NoInitialContextException: Cannot
instantiate class: org.apache.naming.java.javaURLContextFactory [Root
exception is java.lang.ClassNotFoundException:
org.apache.naming.java.javaURLContextFactory]


*Workaround : *

I modified MBeanImpl() constructor like this:

private Context envContext = null;
public MBeanImpl() {

        InitialContext initialContext;
        try {
            initialContext = new InitialContext();
            envContext = (Context) initialContext.lookup("java:/comp/env");
        } catch (NamingException e1) {
            e1.printStackTrace();
        }

        try {
            MBeanServer server = getServer();
            server.registerMBean(this, new
ObjectName(StoreServerSyncAndReloadConstants.MBEAN_NAME));
        } catch (Exception e) {
            logger.error("Error in Rgistering the Configurator MBean",e);
            logger.warn("Configurator is NOT Available through JMX");
        }


    }

Passed envContext as a parameter to DBManager(envContext)
Modified DBManager code looks like this :

DBManager.reload(Context envContext)
{
         BasicDataSource ds = (BasicDataSource) envContext.lookup("jdbc/" +
dataSourceName);

   // use ds to connect to db and get records.
}

Now  if I invoke reload() from jconsole it works fine. The flow goes as
follows:
click reload() on jconsole gui
causes constructor of MBeanImpl to run where it does new InitialContext()
invokes reload() where it passes envContext object as parameter to
DBManager.reload(envContext)
DBManager uses passed in object to lookup datasource
flow completes with success.


*My question is : when I invoked reload() from jconsole , why it failed
when it tried to create new InitialContext() in DBManager. But when same
reload() is invoked during tomcat startup it succeeds.

Another question: Is there a better workaround to this problem?*



>
> >
> >
> >
> >
> >
> >
> >>
> >>> (I am using gmail and hitting reply button on your messages. Please let
> >> me
> >>> know if my messages are not being put in order as mailing list
> expects.)
> >>
> >> That shouldn't prevent you from scrolling down and entering your replies
> >> at the appropriate points in the text.  Just like I am.
> >>
> >>
> >> p
> >>
> >>> On Tue, Dec 20, 2011 at 5:18 PM, Pid <p...@pidster.com> wrote:
> >>>
> >>>> On 20/12/2011 11:46, S B wrote:
> >>>>> Code Sample that fails with exception(I am invoking reload() from
> >>>> jconsole
> >>>>> ):
> >>>>>
> >>>>> class MBeanImpl implements MBeanInterface{
> >>>>>
> >>>>>         public MBeanImpl{
> >>>>>            // code to register this bean.
> >>>>>         }
> >>>>>
> >>>>>
> >>>>>  @Override
> >>>>>    public void reload() {
> >>>>>
> >>>>>        DBManager.loadFromDB();
> >>>>>
> >>>>>    }
> >>>>> }
> >>>>>
> >>>>>
> >>>>> class DBManager{
> >>>>>
> >>>>>            public static void loadFromDB(){
> >>>>>
> >>>>>                 InitialContext initContext = new
> >>>>> InitialContext();                         // here it fails with
> >> exception
> >>>>> avax.naming.NoInitialContextException: Cannot instantiate class:
> >>>>> org.apache.naming.java.javaURLContextFactory [Root
> >>>>>
> >>>>> // exception is java.lang.ClassNotFoundException:
> >>>>> org.apache.naming.java.javaURLContextFactory]
> >>>>>                Context context = (Context)
> >>>>> initContext.lookup("java:comp/env");
> >>>>>                logger.info("context found " + context);
> >>>>>                BasicDataSource ds = (BasicDataSource)
> >>>>> context.lookup("jdbc/" + dataSourceName);
> >>>>>               ...............
> >>>>>               ...............
> >>>>>            }
> >>>>>
> >>>>> }
> >>>>>
> >>>>>
> >>>>> ---------------------------------------------
> >>>>>
> >>>>> Code Sample that works:
> >>>>>
> >>>>> class MBeanImpl implements MBeanInterface{
> >>>>>          private Context envContext = null;
> >>>>>
> >>>>>         public MBeanImpl{
> >>>>>           InitialContext initialContext = new InitialContext();
> >>>>>            envContext = (Context)
> >>>> initialContext.lookup("java:/comp/env");
> >>>>>            // code to register this bean.
> >>>>>         }
> >>>>>
> >>>>>
> >>>>>  @Override
> >>>>>    public void reload() {
> >>>>>
> >>>>>        DBManager.loadFromDB(envContext);     // passed context object
> >>>>> obtained in constructor
> >>>>>
> >>>>>    }
> >>>>> }
> >>>>>
> >>>>>
> >>>>> class DBManager{
> >>>>>
> >>>>>            public static void loadFromDB(Context context){
> >>>>>
> >>>>>                 BasicDataSource ds = (BasicDataSource)
> >>>>> context.lookup("jdbc/" + dataSourceName);  // works fine as I am not
> >>>>> creating new InitialContext here.
> >>>>>               ...............
> >>>>>               ...............
> >>>>>            }
> >>>>>
> >>>>> }
> >>>>
> >>>> Where does the MBeanImpl code run?  In a ServletContextListener, or
> >>>> somewhere else?
> >>>>
> >>>>
> >>>> p
> >>>>
> >>>>
> >>>>> On Tue, Dec 20, 2011 at 4:59 PM, S B <sbl...@gmail.com> wrote:
> >>>>>
> >>>>>> Hi,
> >>>>>> I am using :
> >>>>>>
> >>>>>> Server version: Apache Tomcat/6.0.32
> >>>>>> OS Name:        Mac OS X
> >>>>>> OS Version:     10.6.8
> >>>>>> Architecture:   x86_64
> >>>>>> JVM Version:    1.6.0_29-b11-402-10M3527
> >>>>>>
> >>>>>> Thanks,
> >>>>>> Ravi
> >>>>>>
> >>>>>>
> >>>>>> On Tue, Dec 20, 2011 at 4:52 PM, Pid * <p...@pidster.com> wrote:
> >>>>>>
> >>>>>>> On 20 Dec 2011, at 11:02, S B <sbl...@gmail.com> wrote:
> >>>>>>>
> >>>>>>>> Hi,
> >>>>>>>>
> >>>>>>>> I created and deployed an MBean in my tomcat. It uses datasource
> to
> >>>>>>> connect
> >>>>>>>> to DB.
> >>>>>>>
> >>>>>>> Which version of Java? Tomcat?
> >>>>>>>
> >>>>>>>
> >>>>>>>> My questions is:
> >>>>>>>>
> >>>>>>>> When I create InitialContext() inside MBean's constructor and pass
> >> the
> >>>>>>>> envContext to DBManager class to lookup datasource it works fine.
> >>>>>>> However
> >>>>>>>> when I create InitialContext() in DBManager  class, it fails.
> >>>>>>>>
> >>>>>>>> Is it necessary to create InitialContext() during loading of MBean
> >>>>>>> (either
> >>>>>>>> in ContextListener class  or inside MBean constructor).
> >>>>>>>
> >>>>>>> It depends on where you run the code. You haven't told us this.
> >>>>>>>
> >>>>>>> Can you post a code example.
> >>>>>>>
> >>>>>>>
> >>>>>>> p
> >>>>>>>
> >>>>>>>
> >>>>>>>> When I did new InitialContext() in  my DBManager class which is
> >> called
> >>>>>>> from
> >>>>>>>> hello method of MBean it failed with below exception:
> >>>>>>>>
> >>>>>>>> avax.naming.NoInitialContextException: Cannot instantiate class:
> >>>>>>>> org.apache.naming.java.javaURLContextFactory [Root exception is
> >>>>>>>> java.lang.ClassNotFoundException:
> >>>>>>>> org.apache.naming.java.javaURLContextFactory]
> >>>>>>>>
> >>>>>>>> I invoked hello method of MBean from JConsole.
> >>>>>>>>
> >>>>>>>>
> >>>>>>>> Please let me know is it Tomcat specific? or same behavior is seen
> >>>>>>> across
> >>>>>>>> all app servers. Also, What difference does it make if I create
> >>>>>>>> InitialContext inside MBean's constructor or in some other class
> at
> >> a
> >>>>>>> later
> >>>>>>>> point of time.
> >>>>>>>>
> >>>>>>>> Thanks
> >>>>>>>> Ravi
> >>>>>>>
> >>>>>>>
> ---------------------------------------------------------------------
> >>>>>>> To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
> >>>>>>> For additional commands, e-mail: users-h...@tomcat.apache.org
> >>>>>>>
> >>>>>>>
> >>>>>>
> >>>>>
> >>>>
> >>>>
> >>>> --
> >>>>
> >>>> [key:62590808]
> >>>>
> >>>>
> >>>
> >>
> >>
> >> --
> >>
> >> [key:62590808]
> >>
> >>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
> For additional commands, e-mail: users-h...@tomcat.apache.org
>
>

Reply via email to