Class MessageServlet

  • All Implemented Interfaces:
    Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig

    public class MessageServlet
    extends MessageServletSupport
    A servlet for sending and receiving messages to/from JMS destinations using HTTP POST for sending and HTTP GET for receiving.

    You can specify the destination and whether it is a topic or queue via configuration details on the servlet or as request parameters.

    For reading messages you can specify a readTimeout parameter to determine how long the servlet should block for. One thing to keep in mind with this solution - due to the nature of REST, there will always be a chance of losing messages. Consider what happens when a message is retrieved from the broker but the web call is interrupted before the client receives the message in the response - the message is lost.

    See Also:
    Serialized Form
    • Constructor Detail

      • MessageServlet

        public MessageServlet()
    • Method Detail

      • init

        public void init()
                  throws javax.servlet.ServletException
        Overrides:
        init in class javax.servlet.GenericServlet
        Throws:
        javax.servlet.ServletException
      • doPost

        protected void doPost​(javax.servlet.http.HttpServletRequest request,
                              javax.servlet.http.HttpServletResponse response)
                       throws javax.servlet.ServletException,
                              IOException
        Sends a message to a destination
        Overrides:
        doPost in class javax.servlet.http.HttpServlet
        Parameters:
        request -
        response -
        Throws:
        javax.servlet.ServletException
        IOException
      • doDelete

        protected void doDelete​(javax.servlet.http.HttpServletRequest request,
                                javax.servlet.http.HttpServletResponse response)
                         throws javax.servlet.ServletException,
                                IOException
        Supports a HTTP DELETE to be equivalent of consuming a single message from a queue
        Overrides:
        doDelete in class javax.servlet.http.HttpServlet
        Throws:
        javax.servlet.ServletException
        IOException
      • doGet

        protected void doGet​(javax.servlet.http.HttpServletRequest request,
                             javax.servlet.http.HttpServletResponse response)
                      throws javax.servlet.ServletException,
                             IOException
        Supports a HTTP DELETE to be equivalent of consuming a single message from a queue
        Overrides:
        doGet in class javax.servlet.http.HttpServlet
        Throws:
        javax.servlet.ServletException
        IOException
      • doMessages

        protected void doMessages​(javax.servlet.http.HttpServletRequest request,
                                  javax.servlet.http.HttpServletResponse response)
                           throws javax.servlet.ServletException,
                                  IOException
        Reads a message from a destination up to some specific timeout period
        Parameters:
        request -
        response -
        Throws:
        javax.servlet.ServletException
        IOException
      • handleAsyncRequest

        protected void handleAsyncRequest​(javax.servlet.http.HttpServletRequest request,
                                          javax.servlet.http.HttpServletResponse response,
                                          WebClient client,
                                          javax.jms.Destination destination,
                                          org.apache.activemq.MessageAvailableConsumer consumer,
                                          long deadline)
      • writeResponse

        protected void writeResponse​(javax.servlet.http.HttpServletRequest request,
                                     javax.servlet.http.HttpServletResponse response,
                                     javax.jms.Message message)
                              throws IOException,
                                     javax.jms.JMSException
        Throws:
        IOException
        javax.jms.JMSException
      • writeMessageResponse

        protected void writeMessageResponse​(PrintWriter writer,
                                            javax.jms.Message message)
                                     throws javax.jms.JMSException,
                                            IOException
        Throws:
        javax.jms.JMSException
        IOException
      • isXmlContent

        protected boolean isXmlContent​(javax.jms.Message message)
                                throws javax.jms.JMSException
        Throws:
        javax.jms.JMSException
      • getWebClient

        public WebClient getWebClient​(javax.servlet.http.HttpServletRequest request)
      • getContentType

        protected String getContentType​(javax.servlet.http.HttpServletRequest request)
      • setResponseHeaders

        protected void setResponseHeaders​(javax.servlet.http.HttpServletResponse response,
                                          javax.jms.Message message)
                                   throws javax.jms.JMSException
        Throws:
        javax.jms.JMSException
      • getReadDeadline

        protected long getReadDeadline​(javax.servlet.http.HttpServletRequest request)
        Returns:
        the timeout value for read requests which is always >= 0 and <= maximumReadTimeout to avoid DoS attacks
      • closeConsumerOnOneShot

        protected void closeConsumerOnOneShot​(javax.servlet.http.HttpServletRequest request,
                                              WebClient client,
                                              javax.jms.Destination dest)
        Close the consumer if one-shot mode is used on the given request.