12 Replies Latest reply on Feb 27, 2014 9:23 AM by Justin Bertram

    jboss 7.1 hornetq creating a queue at runtime using JMX

    Alex Don Newbie

      I want to create a Queue on run time. I have code below for the Server. After the Client sends a message to the Server, MDB takes the message from the  "queue/MyQueue" and  it shold be created a new Queue. I'm trying do it using JMX. The Problem ist with ObjectName. I tried a lot of possibilities, two of them I listed here, the one is commented in (//ObjectName on = ObjectNameBuilder.DEFAULT.getJMSServerObjectName(); ).  in \jboss-as-7.1.1.Final\standalone\configuration\standalone.xml I've put hopefully settings.

       

       

      @MessageDriven(name = "MessageMDBSample", activationConfig = {

      @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),

      @ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/MyQueue"),

      @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge") })

       

       

      public class QueueListenerMDB implements MessageListener {

      ...

      public void onMessage(Message message) {

           ....

      createQueueUsingJMX();

       

      }

       

      private void createQueueUsingJMX() throws Exception {

       

       

      MBeanServer mBeanServer = (MBeanServer) javax.management.MBeanServerFactory.findMBeanServer(null).iterator().next();

      ObjectName on = new ObjectName("jboss.as:subsystem=messaging,hornetq-server=default");

      //ObjectName on = ObjectNameBuilder.DEFAULT.getJMSServerObjectName();

        MBeanInfo mbi = mBeanServer.getMBeanInfo(on);

        

        mBeanServer.invoke(on, "createQueue", new Object[] { "queueName" },

          new String[] { "java.lang.String" });

         

      }

       

      }

       

      with different ObjectName I got the following errors:

       

      ERROR by using ObjectName on = new ObjectName("jboss.as:subsystem=messaging,hornetq-server=default");  >>>>>  [stderr] (Thread-362 (HornetQ-client-global-threads-1330642)) javax.management.MBeanException: JBAS011343: No operation called 'createQueue'

       

      ERROR by using     ObjectName on = ObjectNameBuilder.DEFAULT.getJMSServerObjectName(); >>>>>>>     javax.management.InstanceNotFoundException: org.hornetq:module=JMS,type=Server

       

      I checked in the JConsole under "MBeans" all MBeans.  I can find only : jboss.jta, jboss.modules, jboss.msc, jboss.ws. I would expect there smth like this org.hornetq:module=JMS.  So I think that maybe hornetq is not write configured with Jboss or what is wrong?

       

      Thanks in advance.

        • 1. Re: jboss 7.1 hornetq creating a queue at runtime using JMX
          Justin Bertram Master

          Try adding this to your config:

           

          <jmx-management-enabled>true</jmx-management-enabled>
          

           

          The documentation discusses this - Chapter 30. Management.

          1 of 1 people found this helpful
          • 3. Re: Re: jboss 7.1 hornetq creating a queue at runtime using JMX
            Alex Don Newbie

            I read it too and added the line. As I understood, since Jboss 7.* the settings from file hornetq-configuration.xml are moved into jboss-as-7.1.1.Final\standalone\configuration\standalone.xml and standalone-full, so the file  hornetq-configuration.xml doesn't exist more in Jboss 7. Is it right? (Question 1)

             

            SoI tried to put the line <jmx-management-enabled>true</jmx-management-enabled> into standalone.xml file, but after launching the jboss server the line is deleted atomatically. So I putted this line in standalone-full. Is it right?   (Question 2)

             

            Question 3: Can you have a look please in both attached files, maybe you will see some other missing settings or give some other hint?

             

            I would be grateful for your help, as I'm hanging with this problem for a long time.

            Alex

            • 4. Re: Re: jboss 7.1 hornetq creating a queue at runtime using JMX
              Justin Bertram Master

              As I understood, since Jboss 7.* the settings from file hornetq-configuration.xml are moved into jboss-as-7.1.1.Final\standalone\configuration\standalone.xml and standalone-full, so the file  hornetq-configuration.xml doesn't exist more in Jboss 7. Is it right? (Question 1)

              To be clear, the configuration elements (at least most of them) from hornetq-configuration.xml can be specified in JBoss AS 7.x in the <hornetq-server> element of the messaging subsystem. By default, only standalone-full.xml and standalone-full-ha.xml have this subsystem defined (although it can be added to any profile).

               

              SoI tried to put the line <jmx-management-enabled>true</jmx-management-enabled> into standalone.xml file, but after launching the jboss server the line is deleted atomatically. So I putted this line in standalone-full. Is it right?   (Question 2)

              It depends on where you put it.  By default, the standalone.xml doesn't have the messaging subsystem so it wouldn't be right to put it in there unless you added the messaging subsystem first.  I'd stick with standalone-full.xml for simplicity.

               

              Question 3: Can you have a look please in both attached files, maybe you will see some other missing settings or give some other hint?

              The attached standalone.xml does have the messaging subsystem, but there is no mention of <jmx-management-enabled>true</jmx-management-enabled>.

               

              The attached standalone-full.xml does appear to be correctly configured with <jmx-management-enabled>true</jmx-management-enabled>.

               

              As a simple test I unzipped a fresh copy of JBoss AS 7.1.1 and started it.  Then I started JConsole and attached to JBoss AS.  I didn't see any HornetQ MBeans.  Then I stopped JBoss AS and added <jmx-management-enabled>true</jmx-management-enabled> to the <hornetq-server> and restarted.  When I attached JConsole this time I could see all the relevant HornetQ MBeans.

              1 of 1 people found this helpful
              • 5. Re: Re: Re: jboss 7.1 hornetq creating a queue at runtime using JMX
                Alex Don Newbie

                Thanks a lot for your help ! I'm now a little bit further as before .

                 

                I decided to download a fresh Jboss 7.1.1 and tried, how you explained. It seems to be better. At least I can see in MBeans jboss.as , subsystem=messaging and there hornetq-ra. Is it the same, what you can see in your JConsole? (Question 1) I would expext org.hornetq, as you can see on the  attached picture(I downloaded it from internet).

                 

                Question2: How do you start your server, with command : standalone -server-config=standalone-full.xml or only :standalone.bat?

                 

                Question 3: Do I starting the JConsole right, when I choose the unzipped Jboss from the List : "Local Process ", as my JBoss runs local on my computer.

                 

                Cheers,

                Alex

                • 6. Re: Re: Re: Re: jboss 7.1 hornetq creating a queue at runtime using JMX
                  Justin Bertram Master

                  At least I can see in MBeans jboss.as , subsystem=messaging and there hornetq-ra. Is it the same, what you can see in your JConsole? (Question 1)

                  If I start a completely unmodified instance of JBoss AS 7.1.1 I can see the jboss.as:subsystem=messaging MBeans and all of its children.  Note, these are MBeans from the JBoss AS messaging subsystem.  They are not the same as HornetQ's built-in MBeans (although they overlap functionally in some ways).  To see HornetQ's built-in MBeans you have to specify <jmx-management-enabled>true</jmx-management-enabled> as we've already discussed.

                   

                  Question2: How do you start your server, with command : standalone -server-config=standalone-full.xml or only :standalone.bat?

                  I use this command:

                   

                  ./standalone.sh -c standalone-full.xml
                  

                   

                  Question 3: Do I starting the JConsole right, when I choose the unzipped Jboss from the List : "Local Process ", as my JBoss runs local on my computer.

                  I start JConsole using this command:

                   

                  jconsole
                  

                   

                  Then I choose to connect to the local JBoss AS process because, like you, everything is running on my local computer.

                  • 7. Re: Re: Re: jboss 7.1 hornetq creating a queue at runtime using JMX
                    Alex Don Newbie

                    Thanks, It works! It works with the command ./standalone.sh -c standalone-full.xml, as we determine that the exactly this xml file is used.

                     

                    Now I'm trying to connect with a client to my server, which I launched with hornetq: the Code is here:

                     

                    SERVER:

                    @MessageDriven(name = "MessageMDBSample", activationConfig = {

                      @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),

                      @ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/test"),

                      @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge") })

                     

                    public class QueueListenerMDB implements MessageListener {

                      public QueueListenerMDB() {

                      System.out.println("Constructor");

                      }

                      public void onMessage(Message message) {

                     

                      try {

                      if (message instanceof TextMessage) {

                      System.out.println("Queue: I received a TextMessage at "

                      + new Date());

                      TextMessage msg = (TextMessage) message;

                      System.out.println("Message is : " + msg.getText());

                     

                      }

                     

                      } catch (JMSException e) {

                      e.printStackTrace();

                      } catch (Exception e) {

                      // TODO Auto-generated catch block

                      e.printStackTrace();

                      }

                      }

                     

                    I see my Queues in AdminConsole on Jboss Server, so the Server runs.

                     

                    I cannot establish connection from the Client to the server, it tried a lot of possibility, maybe can you say, which of possibility is more or less in the right direction:

                     

                    Variant1 :

                     

                    public static void main(String[] args) throws Exception {

                     

                     

                      new JMSApplicationClient().firstConnection();

                     

                     

                      }

                     

                     

                      private void firstConnection() throws JMSException {

                      // Step 1. Directly instantiate the JMS Queue object.

                     

                      Queue queue = HornetQJMSClient.createQueue("testQueue");

                      //Queue replyQueue = HornetQJMSClient.createQueue("replyQueue");

                      // Step 2. Instantiate the TransportConfiguration object

                      // which

                      // contains the knowledge of what transport to use,

                      // The server port etc.

                      Map connectionParams = new HashMap();

                      connectionParams

                      .put(org.hornetq.core.remoting.impl.netty.TransportConstants.PORT_PROP_NAME,

                      5445);

                     

                    // connectionParams.put(Context.SECURITY_PRINCIPAL, "guest");

                    // connectionParams.put(Context.SECURITY_CREDENTIALS, "pass");

                     

                      TransportConfiguration transportConfiguration = new TransportConfiguration(

                      NettyConnectorFactory.class.getName(), connectionParams);

                      // Step 3 Directly instantiate the JMS ConnectionFactory

                      // object

                      // using that TransportConfiguration

                      ConnectionFactory cfactory = (ConnectionFactory) HornetQJMSClient

                      .createConnectionFactoryWithoutHA(JMSFactoryType.CF,

                      transportConfiguration);

                      System.out.println("cfactory");

                      Connection connection;

                      try {

                      // Step 4.Create a JMS Connection

                      System.out.println("createConnection>");

                      connection = cfactory.createConnection();

                      System.out.println("createConnection");

                      // Step 5. Create a JMS Session

                      Session session = connection.createSession(false,

                      Session.AUTO_ACKNOWLEDGE);

                      System.out.println("createSession");

                      // Step 6. Create a JMS Message Producer

                      MessageProducer producer = session.createProducer(queue);

                      MessageConsumer consumer = session.createConsumer(queue);

                     

                      //consumer.setMessageListener(this);

                      connection.start();

                     

                      //  Create a Text Message

                      TextMessage message = session

                      .createTextMessage("This is a text message hello");

                     

                      System.out.println("Sent message: " + message.getText()

                      + " JMStype: " + message.getJMSType());

                     

                     

                      // Step 8. Send the Message

                      producer.send(message);

                     

                     

                      //session.close();

                      //session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

                     

                     

                     

                    ERROR: Unable to validate user: null

                     

                    When change the line connection = cfactory.createConnection();       to             this one connection = cfactory.createConnection("guest", "pass");

                     

                    ERROR: Unable to validate user: guest.

                     

                     

                    VARIANT 2:

                     

                    public void sendMessagetoJMS() {

                     

                     

                      Hashtable<String, String> ht = new Hashtable<String, String>();

                      ht.put(Context.INITIAL_CONTEXT_FACTORY,

                      "org.jnp.interfaces.NamingContextFactory");

                      ht.put(Context.PROVIDER_URL, "127.0.0.1");

                    // ht.put(Context.PROVIDER_URL, "jnp://localhost:1099");

                      ht.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");

                     

                      try {

                      lContext = new InitialContext(ht);

                      Hconnection = (ConnectionFactory) lContext

                      .lookup("ConnectionFactory");

                      q = (Queue) lContext.lookup("queue/test");

                      Hconn = (Connection) Hconnection.createConnection("guest", "guest");

                      session = Hconn.createSession(false, Session.AUTO_ACKNOWLEDGE);

                      messageProducer = session.createProducer(q);

                      /*

                      * Insert into Database

                      */

                     

                     

                      Hconn.start();

                     

                     

                      TextMessage message = session

                      .createTextMessage("This is a text message hello");

                      message.setJMSType("hello"); // the first message from Client

                     

                     

                      System.out.println("Sent message: " + message.getText()

                      + " JMStype: " + message.getJMSType());

                     

                     

                      // Step 8. Send the Message

                      messageProducer.send(message);

                      System.out.println("Message sent ");

                      Hconn.close();

                     

                     

                      } catch (Exception ex) {

                      ex.printStackTrace();

                      }

                      }

                     

                    ERROR: Cannot instantiate class: org.jnp.interfaces.NamingContextFactory

                     

                    When I chage the line ht.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");     to this one : ht.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");

                     

                    ERROR : 

                    Feb 25, 2014 6:23:31 PM org.xnio.Xnio <clinit>

                    INFO: XNIO Version 3.0.3.GA

                    Feb 25, 2014 6:23:31 PM org.xnio.nio.NioXnio <clinit>

                    INFO: XNIO NIO Implementation Version 3.0.3.GA

                    Feb 25, 2014 6:23:32 PM org.jboss.remoting3.EndpointImpl <clinit>

                    INFO: JBoss Remoting version 3.2.3.GA

                    javax.naming.NamingException: Failed to create remoting connection  

                     

                    Caused by: org.jboss.remoting3.UnknownURISchemeException: No connection provider for URI scheme "null" is installed

                     

                    I hope it post belongs to the same Forum.

                     

                    Thanks in advance.

                    Alex

                    • 8. Re: Re: Re: jboss 7.1 hornetq creating a queue at runtime using JMX
                      Justin Bertram Master

                      ERROR: Unable to validate user: null

                       

                      When change the line connection = cfactory.createConnection();       to             this one connection = cfactory.createConnection("guest", "pass");

                       

                      ERROR: Unable to validate user: guest.

                      This indicates you don't have the proper user created on the server.  I recommend you use the "add-user.sh" (or "add-user.bat") script to add an "Application User" to the server so your client can connect properly.  The server doesn't allow unauthenticated access by default so that nefarious individuals can't exploit your server when you first boot it.

                       

                      ERROR: Cannot instantiate class: org.jnp.interfaces.NamingContextFactory

                       

                      When I chage the line ht.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");     to this one : ht.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");

                       

                      ERROR : 

                      Feb 25, 2014 6:23:31 PM org.xnio.Xnio <clinit>

                      INFO: XNIO Version 3.0.3.GA

                      Feb 25, 2014 6:23:31 PM org.xnio.nio.NioXnio <clinit>

                      INFO: XNIO NIO Implementation Version 3.0.3.GA

                      Feb 25, 2014 6:23:32 PM org.jboss.remoting3.EndpointImpl <clinit>

                      INFO: JBoss Remoting version 3.2.3.GA

                      javax.naming.NamingException: Failed to create remoting connection  

                       

                      Caused by: org.jboss.remoting3.UnknownURISchemeException: No connection provider for URI scheme "null" is installed

                      In this case your JNDI look-up is incorrect.  See this bit of code from the JBoss AS 7 quickstarts to see an example of how the JNDI look-up should be performed: https://github.com/jboss-developer/jboss-eap-quickstarts/blob/jdf-2.1.7.Final/helloworld-jms/src/main/java/org/jboss/as/quickstarts/jms/HelloWorldJMSClient.java.

                      • 9. Re: Re: Re: jboss 7.1 hornetq creating a queue at runtime using JMX
                        Alex Don Newbie

                        Thanks a lot, Justin!

                        You helped me very much. It works...

                         

                        I'm new to this topic : JBoss, hornetq etc. with the documentation is it not every think clear.

                         

                        Here is the code:

                         

                        private void sendReceive() throws JMSException {

                               // Step 1. Directly instantiate the JMS Queue object.

                               Queue queue = HornetQJMSClient.createQueue("testQueue");

                               // Step 2. Instantiate the TransportConfiguration object which contains the knowledge of what transport to use, The server port etc.

                               Map connectionParams = new HashMap();

                               connectionParams.put(org.hornetq.core.remoting.impl.netty.TransportConstants.PORT_PROP_NAME,5445);

                               TransportConfiguration transportConfiguration = new TransportConfiguration(NettyConnectorFactory.class.getName(), connectionParams);

                               // Step 3 Directly instantiate the JMS ConnectionFactory object using that TransportConfiguration

                               ConnectionFactory cfactory = (ConnectionFactory) HornetQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF,transportConfiguration);

                                Connection connection;

                               try {

                                    // Step 4.Create a JMS Connection

                                    connection = cfactory.createConnection("guest1", "pass");

                                    // Step 5. Create a JMS Session

                                    Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);

                                    // Step 6. Create a JMS Message Producer

                                    MessageProducer producer = session.createProducer(queue);

                                    connection.start();

                                    //  Create a Text Message

                                    TextMessage message = session.createTextMessage("It is a  hello Messedge");

                                    message.setJMSType("hello"); // the first message from Client

                         

                                    System.out.println("Sent message: " + message.getText() + " JMStype: " + message.getJMSType());

                                    // Step 8. Send the Message

                                    producer.send(message);

                                    session.close();

                                 

                               } catch (JMSException e) {

                                    e.printStackTrace();

                               }

                        }

                         

                        Cheers,

                        Alex

                        • 10. Re: Re: Re: jboss 7.1 hornetq creating a queue at runtime using JMX
                          Justin Bertram Master

                          I'm not sure what you mean about the documentation not being clear.  Can you be more specific?

                           

                          The code you've pasted looks fine to me.  In fact, it looks like a modified version of the instantiate-connection-factory example that ships with HornetQ.  Are you receiving an error with this?  If so, what is it?

                          • 11. Re: Re: Re: jboss 7.1 hornetq creating a queue at runtime using JMX
                            Alex Don Newbie

                            The posted code in my last post is correct and it works. I just posted the code here, as final result. It is heplful for other too, who will be conserned. I know it from experience, that is helpful, when the final result is posted. It happened to me,as I started to deal with Jboss and Hornetq.

                             

                            About documentation: I will explain it, how I started and where I had trouble in order to show the problems, which faced by begginers

                             

                            I started to read this documentation : http://docs.jboss.org/hornetq/2.2.2.Final/user-manual/en/html_single/#using-jms . I seen a lot of examples, but I couldn't start a simple programm.  It was the same to me with availabel examples \hornetq-HornetQ_2_4_0_Final\examples\jms . This tutorial helped me very well to get in.

                            I believe, that for somebody, who work before with similar topics (at least Jboss or some other Server configuration), the documentation and examples would be enough.

                             

                            As well now, I found the following example, which show, how a MDB can reply to a msg and send the answer to the Queue back http://docs.jboss.org/jbossas/docs/Server_Configuration_Guide/4/html/JMS_Examples-A_Point_To_Point_With_MDB_Example.html

                             

                            I receive the following Error and I don't know, why It doesn't work, as shown in example. The error occurs in this line: Queue dest = (Queue) msg.getJMSReplyTo();

                             

                            18:38:10,052 ERROR [stderr] (Thread-333 (HornetQ-client-global-threads-23337920)) java.lang.NullPointerException

                            18:38:10,052 ERROR [stderr] (Thread-333 (HornetQ-client-global-threads-23337920))       at com.theopentutorials.mdb.QueueListenerMDB.sendReply(QueueListenerMDB.java:115)

                            18:38:10,053 ERROR [stderr] (Thread-333 (HornetQ-client-global-threads-23337920))       at com.theopentutorials.mdb.QueueListenerMDB.onMessage(QueueListenerMDB.java:81)

                            18:38:10,053 ERROR [stderr] (Thread-333 (HornetQ-client-global-threads-23337920))       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

                             

                            Actually, what I need, is a out-Queue, that I could send a message from MDB to the client.

                            • 12. Re: jboss 7.1 hornetq creating a queue at runtime using JMX
                              Justin Bertram Master

                              I started to read this documentation : http://docs.jboss.org/hornetq/2.2.2.Final/user-manual/en/html_single/#using-jms . I seen a lot of examples, but I couldn't start a simple programm.

                              That documentation is for HornetQ 2.2.2.Final which is quite old at this point.  You want to be using the latest release and the latest documentation which is 2.4.0.Final right now.  See:

                              Furthermore, all of the examples contain a readme.html file that explains the example and describes how to run it (typically 'mvn verify').

                               

                              If you combine old documentation with a current release that is a recipe for confusion.

                               

                              As well now, I found the following example, which show, how a MDB can reply to a msg and send the answer to the Queue back http://docs.jboss.org/jbossas/docs/Server_Configuration_Guide/4/html/JMS_Examples-A_Point_To_Point_With_MDB_Example.html

                              That bit of documentation is for JBoss AS 4 which is ancient history now.  It doesn't even use EJB3.  If you want a simple MDB example I recommend you take a look at the JBoss EAP quickstarts, specifically https://github.com/jboss-developer/jboss-eap-quickstarts/tree/jdf-2.1.7.Final/helloworld-mdb.