9 Replies Latest reply on Jan 30, 2013 10:47 AM by N J

    HornetQ support for REST API in JBoss 7 Embedded mode

    N J Newbie

      Based on the standalone document , I have created a w.ar file to enable the RestEasy interfaces for Embedded HornetQ in Jboss 7.1.2. 

       

      When the JBoss is started, I don't any error message. However the following code does not create a message in the queue and fails with "Null pointer execption". Am I missing anything ?

       

       

      public class RestSend {

       

                public static void main(String[] args) {

                               try {

                                              String msgStr = "<Orders>

                                                   <order_no>123</order_no>

                                                   <total_amount>99.99</total_amount>

                                             </Orders>";

                                              ClientRequest request = new ClientRequest("http://localhost:8080/queues/queue.test");

                  ClientResponse res = request.head();

                  Link create = res.getHeaderAsLink("msg-create");

                  System.out.println("Send xml...");

                  res = create.request().body("application/xml", msgStr).post();

                  if (res.getStatus() != 201)

                        throw new RuntimeException("Failed to post");

                              } catch (Exception e) {

                  e.printStackTrace();

            }

                }

       

      }

        • 3. Re: HornetQ support for REST API in JBoss 7 Embedded mode
          N J Newbie

          This is what I did

           

          1. Creates web.xml per documentation and deployed in JBoss 7.1.2

                            <?xml version="1.0" encoding="UTF-8"?>

                            <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee       http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"     version="2.4">

                            <display-name>Hornetq RestEasy Webservice</display-name>

                             <listener>

                                       <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>

                             </listener>

                             <listener>

                                  <listener-class>org.hornetq.rest.integration.RestMessagingBootstrapListener</listener-class>

                             </listener>

                            <filter>

                                      <filter-name>Rest-Messaging</filter-name>

                                      <filter-class>org.jboss.resteasy.plugins.server.servlet.FilterDispatcher</filter-class>

                             </filter>

                       <filter-mapping>

                       <filter-name>Rest-Messaging</filter-name>

                               <url-pattern>/*</url-pattern>

                        </filter-mapping>  

                    </web-app>

           

          2. JBoss 7.1.2 Embedded HornetQ has this entry to start the queue

                              <jms-queue name="testQueue">

                                  <entry name="queue/test"/>

                                  <entry name="java:jboss/exported/jms/queue/test"/>

                              </jms-queue>

           

          3. Wrote Simple JAva class to send a meesage to the "test" queue. (Please see the above  code for the URI ClientRequest request = new ClientRequest("http://localhost:8080/queues/queue.test");

           

          4. NPE stack trace is

                         log4j:WARN No appenders could be found for logger (org.jboss.resteasy.plugins.providers.DocumentProvider).

                         log4j:WARN Please initialize the log4j system properly.

                         log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

                         java.lang.NullPointerException

                         Send xml...

                                        at com.example.com.hornetq71.test.RestSend.main(RestSend.java:29)

           

           

          Is My understanding/approach  correct ?.

          • 5. Re: HornetQ support for REST API in JBoss 7 Embedded mode
            Justin Bertram Master

            It looks to me like you're simply using the wrong URL. 

             

            First of all, unless you've configured this WAR to be at the root context then the URL will have the WAR's context.  For example, if the WAR was named "hornetq-rest.war" then the URL would be http://localhost:8080/hornetq-rest/queues/{name}.  That's basic Java EE web application practice. 

             

            Also, the queue name you're using isn't correct.  If you deploy a jms-queue with a name of "testQueue" then the name of the queue you would use is "jms.queue.testQueue".  In the end your URL would look like this:

             

              http://localhost:8080/hornetq-rest/queues/jms.queue.testQueue

             

            I'll try to update the documentation so this is more clear.

            • 6. Re: HornetQ support for REST API in JBoss 7 Embedded mode
              N J Newbie

              Thanks for the clarification. The code executed with no error

                   try {

                       String msgStr = "<Orders> " +

                          "                              <order_no>123</order_no> " +

                          "                              <total_amount>99.99</total_amount> " +

                          "                    </Orders>";

                       ClientRequest request = new ClientRequest("http://localhost:8080/hornetq_web_service/queues/jms.queue.testQueue");

                       request.body("application/xml", msgStr);

                       ClientResponse res = request.head();

                       Link create = res.getHeaderAsLink("msg-create");

               

                       res = create.request().body("application/xml", msgStr).post();

                       if (res.getStatus() != 201)

                           throw new RuntimeException("Failed to post");

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

                   } catch (Exception e) {

                       // TODO Auto-generated catch block

                       e.printStackTrace();

                   }

               

               

              But when I try to consume the message through JMS code, I did not get anything.

               

              Also, When I tried to consume the message through below code

                 {

                    ClientRequest request = new ClientRequest("http://localhost:8080/hornetq_web_service/queues/jms.queue.testQueue");

                    MediaType mediaType=MediaType.APPLICATION_XML_TYPE;

                    request.accept(mediaType);

                   

                    ClientResponse res = request.head();

                    Link pullConsumers = res.getHeaderAsLink("msg-pull-consumers");

                    res = pullConsumers.request().formParameter("autoAck", "false").post();

                    Link ackNext = res.getHeaderAsLink("msg-acknowledge-next");

                    while (true)

                    {

                       System.out.println("Waiting...");

                       res = ackNext.request()

                               .header("Accept-Wait", "10")

                               .header("Accept", "application/xml")

                               .post();

                       if (res.getStatus() == 503)

                       {

                          System.out.println("Timeout...");

                          ackNext = res.getHeaderAsLink("msg-acknowledge-next");

                       }

                       else if (res.getStatus() == 200)

                       {

                          Link ack = res.getHeaderAsLink("msg-acknowledgement");

                          res = ack.request().formParameter("acknowledge", "true").post();

                          ackNext = res.getHeaderAsLink("msg-acknowledge-next");

                       }

                       else

                       {

                          throw new RuntimeException("Failure! " + res.getStatus());

                       }

                    }

                 }

               

              any idea where am I making mistake ?

              • 7. Re: HornetQ support for REST API in JBoss 7 Embedded mode
                N J Newbie

                Justin - Can you please provide some insight into the issue ?

                 

                Thanks

                • 8. Re: HornetQ support for REST API in JBoss 7 Embedded mode
                  Justin Bertram Master

                  I don't know exactly why your client is failing, but this simple app works for me:

                   

                  package com.example.AS7RestHornetQClient; 
                  
                  import org.jboss.resteasy.client.ClientRequest;
                  import org.jboss.resteasy.client.ClientResponse;
                  import org.jboss.resteasy.spi.Link; 
                  
                  public class SimpleRESTClient { 
                      public static final String MSG_ACKNOWLEDGE_NEXT = "msg-acknowledge-next";
                      public static final String MSG_PULL_CONSUMERS = "msg-pull-consumers";
                      public static final String MSG_ACKNOWLEDGEMENT = "msg-acknowledgement";
                      public static final String MSG_CREATE = "msg-create"; 
                  
                      public static void main(String[] args) throws Exception {
                          ClientRequest request = new ClientRequest("http://localhost:8080/queues/jms.queue.testQueue");
                          ClientResponse res = request.head();
                          res.releaseConnection(); 
                  
                          Link msg_create = res.getHeaderAsLink(MSG_CREATE);
                          Link msg_pull_consumers = res.getHeaderAsLink(MSG_PULL_CONSUMERS);
                          System.out.println(MSG_CREATE + ": " + res.getStatus());
                          res = msg_create.request().body("application/xml", "abc").post();
                          res.releaseConnection();
                          if (res.getResponseStatus().getStatusCode() != 201) {
                              throw new RuntimeException("Failed to post");
                          } 
                  
                          Link location = null; 
                  
                          try {
                              res = msg_pull_consumers.request().formParameter("autoAck", "false").post();
                              res.releaseConnection();
                              System.out.println(MSG_PULL_CONSUMERS + ": " + res.getStatus());
                              location = res.getLocation(); 
                  
                              Link msg_acknowledge_next = res.getHeaderAsLink(MSG_ACKNOWLEDGE_NEXT);
                              res = msg_acknowledge_next.request().header("Accept-Wait", "30").header("Accept", "application/xml").post();
                              res.releaseConnection();
                              System.out.println(MSG_ACKNOWLEDGE_NEXT + ": " + res.getStatus()); 
                  
                              Link ack = res.getHeaderAsLink(MSG_ACKNOWLEDGEMENT);
                              res = ack.request().formParameter("acknowledge", "true").post();
                              res.releaseConnection();
                              System.out.println(MSG_ACKNOWLEDGEMENT + ": " + res.getStatus());
                          } finally {
                              if (location != null) {
                                  res = location.request().delete();
                                  res.releaseConnection();
                                  System.out.println("Delete consumer: " + res.getStatus());
                              }
                          }
                      }
                  }
                  

                   

                  I know for sure that your client isn't cleaning up its consumers which is a big no-no.

                  • 9. Re: HornetQ support for REST API in JBoss 7 Embedded mode
                    N J Newbie

                    Thanks Justin. This helped me a lot to clear some of misundrstanding on the concept. This worked for me. Thanks again.