4 Replies Latest reply on Jul 21, 2005 2:37 PM by mcaughey

    How to create mbeans on runtime.

    denisius

      The task is to create several Queues from standalone application. i use next code:

      MBeanServer lServer = MBeanServerFactory.createMBeanServer();
      ObjectInstance myQ = lServer.createMBean(
       "org.jboss.mq.server.jmx.Queue", new ObjectName(
       "jboss.mq.destination:service=Queue,name=MyQ"));
      

      to craete one Queue with name "myQ" ander "jboss.mq.destination" domain. The code runs OK - without exceptions, but i don't see any new created Queues. Whats wrong?

        • 1. Re: How to create mbeans on runtime.
          denisius

          The right code is:

           private MBeanServerConnection getServer() {
           if (mBeanServer == null) {
           try {
           Properties props = new Properties();
           props.put("java.naming.factory.initial",
           "org.jnp.interfaces.NamingContextFactory");
           props.put("ava.naming.factory.url.pkgs",
           "org.jboss.naming:org.jnp.interfaces");
           props.put("java.naming.provider.url", "localhost:1199"); //jnp://
          
           InitialContext ctx = new InitialContext(props);
           mBeanServer = (MBeanServerConnection) ctx
           .lookup(jmx/invoker/RMIAdaptor);
           } catch (Exception e) {
           e.printStackTrace();
           mBeanServer = null;
           }
           }
           return mBeanServer;
           }
          
          public void createQueue(String name) {
           getServer().createMBean(
           "org.jboss.mq.server.jmx.Queue",
           new ObjectName("jboss.mq.destination:service=Queue,name=" + name));
          }
          


          • 2. Re: How to create mbeans on runtime.
            genman


            There is a method on the jboss.mq:service=DestinationManager to programmatically create a queue.

            I think your problem was, though the MBean was created, the attributes (like destination manager, etc.) were not set and "start" was not called.

            • 3. Re: How to create mbeans on runtime.
              mcaughey

              Have you been able to get a securityConf Associated with this Queue?

              I'm trying to do the same thing with a Topic. But I haven I cannot authenticate.

              -Michael

              • 4. Re: How to create mbeans on runtime.
                mcaughey

                Here's the code I'm using:

                 private void registerTopic(String topicname)
                 throws MalformedObjectNameException, Exception {
                 createDestination("org.jboss.mq.server.jmx.Topic",
                 getTopicObjectName(topicname), "topic/" + topicname);
                 }
                
                 private ObjectName getTopicObjectName(String name)
                 throws MalformedObjectNameException {
                
                 return new ObjectName("jboss.mq.destination:service=Topic,name=" + name);
                 }
                
                 protected void createDestination(String type, ObjectName name,
                 String jndiName) throws Exception {
                
                 if (logger.isDebugEnabled()) {
                 logger.debug("Attempting to create destination: " + name
                 + "; type=" + type);
                 }
                 MBeanServer server = MBeanServerLocator.locateJBoss();
                 server.createMBean(type, name);
                 server.setAttribute(name, new Attribute("DestinationManager",
                 new ObjectName("jboss.mq:service=DestinationManager")));
                
                 server.setAttribute(name, new Attribute("SecurityManager",
                 new ObjectName("jboss.mq:service=SecurityManager")));
                
                 server.setAttribute(name, new Attribute("SecurityConf", getSecurityconf()));
                 server.setAttribute(name, new Attribute("JNDIName", jndiName));
                
                 TopicMBean tmb = (TopicMBean)MBeanServerInvocationHandler.newProxyInstance(server, name,TopicMBean.class,false);
                 tmb.start();
                 }
                
                
                 private Element getSecurityconf() throws SAXException, IOException{
                 Element el = null;
                 StringBuffer securityConfStr = new StringBuffer();
                 securityConfStr.append("<security>");
                 securityConfStr.append(" <role name=\"publisher\" read=\"true\" write=\"true\" create=\"false\"/>");
                 securityConfStr.append(" <role name=\"durpublisher\" read=\"true\" write=\"true\" create=\"true\"/>");
                 securityConfStr.append("</security>");
                
                 DOMParser parser = new DOMParser();
                 parser.parse(securityConfStr.toString());
                 Document doc = parser.getDocument();
                 el = doc.getDocumentElement();
                 return el;
                 }