Class MessageServlet

java.lang.Object
jakarta.servlet.GenericServlet
jakarta.servlet.http.HttpServlet
org.apache.activemq.web.MessageServletSupport
org.apache.activemq.web.MessageServlet
All Implemented Interfaces:
jakarta.servlet.Servlet, jakarta.servlet.ServletConfig, Serializable

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:
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    closeConsumerOnOneShot(jakarta.servlet.http.HttpServletRequest request, WebClient client, jakarta.jms.Destination dest)
    Close the consumer if one-shot mode is used on the given request.
    protected void
    doDelete(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response)
    Supports a HTTP DELETE to be equivalent of consuming a single message from a queue
    protected void
    doGet(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response)
    Supports a HTTP DELETE to be equivalent of consuming a single message from a queue
    protected void
    doMessages(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response)
    Reads a message from a destination up to some specific timeout period
    protected void
    doPost(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response)
    Sends a message to a destination
    protected String
    getContentType(jakarta.servlet.http.HttpServletRequest request)
     
    protected long
    getReadDeadline(jakarta.servlet.http.HttpServletRequest request)
     
    getWebClient(jakarta.servlet.http.HttpServletRequest request)
     
    protected void
    handleAsyncRequest(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, WebClient client, jakarta.jms.Destination destination, org.apache.activemq.MessageAvailableConsumer consumer, long deadline)
     
    void
     
    protected boolean
    isXmlContent(jakarta.jms.Message message)
     
    protected void
    setResponseHeaders(jakarta.servlet.http.HttpServletResponse response, jakarta.jms.Message message)
     
    protected void
    writeMessageResponse(PrintWriter writer, jakarta.jms.Message message)
     
    protected void
    writeResponse(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, jakarta.jms.Message message)
     

    Methods inherited from class jakarta.servlet.http.HttpServlet

    doHead, doOptions, doPut, doTrace, getLastModified, service, service

    Methods inherited from class jakarta.servlet.GenericServlet

    destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, log, log

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • MessageServlet

      public MessageServlet()
  • Method Details

    • init

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

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

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

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

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

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

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

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

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

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

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

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

      protected long getReadDeadline(jakarta.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(jakarta.servlet.http.HttpServletRequest request, WebClient client, jakarta.jms.Destination dest)
      Close the consumer if one-shot mode is used on the given request.