3 Replies Latest reply on Dec 12, 2001 8:05 PM by wujimx

    Is there a way to make sure that only one instance is runnin

    wujimx Newbie

      Hi, everyone, I am kind of new to this mbean world, could someone please guide me with the following:
      How to make sure that one and only one instance of a mbean will be created by JbossMX? I was thinking about using singleton pattern here but seems no way to do that.

      Thanks a lot.
      Jimmy.

        • 1. Re: Is there a way to make sure that only one instance is ru
          Juha Lindfors Master

          There are no instance pools of MBeans created, so you are in control of how many instances you register to the MBean server.

          • 2. Re: Is there a way to make sure that only one instance is ru
            wujimx Newbie

            Thanks for your respond.

            But what confuse me is the following:
            createMBean of MBeanServer will instantiate and register an MBean in the MBean server, that makes sence.
            queryMBeans of MBeanServer will get MBeans controlled by the MBean server.
            I coded a small program to test this queryMBeans, I only use this queryMBeans in the code(NEVER use createMBean), and I can still get the objectInstance and invoke the method from it. Does that mean queryMBeans will instantiate and register an MBean in the MBean server if there is not one in the server?

            My another question is: is there any limitation in coding MBean? I means, can I use mutilple threads in MBean, static variable? those limitation for EJB?

            Thanks.
            Jimmy.

            • 3. Re: Is there a way to make sure that only one instance is ru
              wujimx Newbie

              Thanks you very much Juha.
              Please be patient with me and see my question in italics:

              > > I coded a small program to test this queryMBeans,
              > I
              > > only use this queryMBeans in the code(NEVER use
              > > createMBean), and I can still get the
              > objectInstance
              > > and invoke the method from it. Does that mean
              > > queryMBeans will instantiate and register an MBean
              > in
              > > the MBean server if there is not one in the
              > server?
              >
              > No, that doesn't happen.

              I did code a small program to do that using inmemory/maps/MapTest in the documentation, I simply add a public function getString() to return a String into that MBean, and code a simple session bean with the following function:
              ...
              public HashMap getHashMap() throws
              {
              HashMap aback = null;
              try
              {
              MBeanServer lServer = (MBeanServer)MBeanServerFactory.findMBeanServer(null).get(0);
              Iterator i = lServer.queryMBeans(null, null).iterator();
              while (i.hasNext()) {
              ObjectInstance io = (ObjectInstance) i.next();
              String aname = io.getObjectName().toString();
              System.out.println("Object Name: " + aname);
              if (aname.compareToIgnoreCase("DefaultDomain:service=JNDIMap,jndiName=inmemory/maps/MapTest")== 0)
              {
              String astr = (String)lServer.invoke(
              io.getObjectName(),
              "getString",
              new Object[]{},
              new String[]{});
              System.out.println("Here is the string: " + astr);

              aback = (HashMap)lServer.invoke(
              io.getObjectName(),
              "getHashMap",
              new Object[]{},
              new String[]{});
              System.out.println(aback);

              }
              }
              }
              catch(Exception e)
              {
              System.out.println("test: " + e.toString());
              e.printStackTrace(System.err);
              }

              return aback;
              }
              ...

              Then I code my client to call this session bean and I was able to get the String from MBean, note that I only use queryMBeans in my code.

              ????



              > > My another question is: is there any limitation in
              > > coding MBean? I means, can I use mutilple threads
              > in
              > > MBean, static variable? those limitation for EJB?
              >
              > There aren't limitations to MBeans (similar to what
              > is in the EJB spec). You can both create threads and
              > use static variables.
              >
              >

              Are you sure? I remembers that I saw the limitation of EJB in somewhere saying that one better don't use static variables or create threads in EJB, etc.