Ajax Message Queue

Ajax Message Queue

Ajax Message Queue is an ajax control which is able to poll messages (IJSONMessage) from server.

This control can be used to poll messages :

On server side, messages are stored into a queue. Queue is defined by :

  • a context : like log4j, chatMessage, customMessage,...
  • a scope :
    • application scope for manage global queue.
    • session scope for manage user session queue.
    • request scope for manage queue for a specific request URI.

Message Queue Filter

Swith scope queue, ServletContext, HttpSession are used to store queues. Those context are available with HttpServletRequest. But sometimes in some case, this context doesn't exist. For instance log4j have not this HttpServletRequest context. To resolve this problem the HttpServletRequest object is setted into ThreadLocal, by the the Filter MessageQueueFilter.

Message Queue Repository helper class which is used to get queue (and store queue in the good context) can be therefore used, by any classes (HttpServletRequest is not waited into parameter of the method getQueue).

Here a schema which show the role of Message Queue Filter for any http request :

Message Queue Filter is too used for poll messages when JSControlsTags.AjaxMessageQueue javascript control call server. Here a schema which show the role of Message Queue Filter for AJAX http request :

Message Queue Filter must be defined into your web.xml :

  <!--  Message Queue Filter --> 
  <filter>
      <filter-name>MessageQueueFilter</filter-name>
      <filter-class>net.sourceforge.jscontrolstags.messagequeue.MessageQueueFilter</filter-class>
      <init-param>
        <param-name>config</param-name>
        <param-value>/WEB-INF/messagequeue-config.xml</param-value>
      </init-param>   
  </filter>
  <filter-mapping>
     <filter-name>MessageQueueFilter</filter-name>
     <url-pattern>/*</url-pattern>
  </filter-mapping> 
  

Message Queue configuration

Queue can be configurate (TODO).

Exemple of messagequeue-config.xml

  <messagequeue>
    <exclude extension="js,css" />
    <!-- JSONMessage Queue Context used into WEB Application. -->
    <context name="log4j" >
      <scope name="request" clear="true" >
      </scope>
    </context>
    <context name="log4j_with_lazy_false" >
      <scope name="request" lazy="false" >
      </scope>
    </context>  
  </messagequeue> 
  

Javascript to include

  <!-- include javascript scriptaculous -->
  <script language="javascript" type="text/javascript" src="../js/scriptaculous/prototype.js"></script>
  <script language="javascript" type="text/javascript" src="../js/scriptaculous/effects.js"></script>
  <script language="javascript" type="text/javascript" src="../js/scriptaculous/controls.js"></script>
  <!-- include javascript jscontrolstags messagequeue -->
  <script language="javascript" type="text/javascript" src="../js/jscontrolstags-messagequeue.js"></script>

jscontrols-ajax:messageQueue

  <!-- Include javascript required -->                    
  ...
  
  <jscontrols-ajax:messageQueue 
          var="ajaxMessageQueue_RequestScope"
          source="log4jMessages_request"
          context="log4j"
          scope="request"
          fromRequestURI="${pageContext.request.contextPath}/log4jmessage.do"
          frequency="2"
          batchSize="$('messageBatchSize_request').value"
          startPollingOnLoad="false"  
          repeatAfterEmptyMessageReceived="3"
          onStartPolling="onStartMessage_request"
          onStopPolling="onStopMessage_request"
          onEmptyMessageReceived="onEmptyMessage_request" />
        
ParameterDescriptionRequired
varName of the JavaScript AjaxMessageQueue created.no
sourceHTML element to update when messages are received from server.yes
contextContext (log4j, ...) of the queue to poll messages.yes
scopeScope (application, session and request) of the queue to poll messages. By default scope=application.no
fromRequestURIThis parameter is used only with scope=request. AjaxMessageQueue will be received messages from queue where messages are added with this requestURI.no
formatFormat of message.no
frequencyInterval (not frequency) between refreshes, in seconds. Defaults to 2 seconds. This number will be multiplied by the current decay when invoking theAjax.Updater objectno
decayKeeps the current decay level applied when re-executing the task.no
batchSizeNumber of messages to poll in each request. If not specified, defaults to 10.no
startPollingOnLoadtrue if AjaxMessageQueue must start poll messages from server when page is loaded, false otherwise. By default startPollingOnLoad=false.no
stopPollingAtEmptyMessageReceivedtrue if AjaxMessageQueue must stop polling messages when it received empty message, and false otherwise. By default stopPollingAtEmptyMessageReceived=true.no
onStartPollingCallback called when AjaxMessageQueue start polling messages.no
onStopPollingCallback called when AjaxMessageQueue stop polling messages.no
isStopPollingFunction to stop polling with custom condition.no
onEmptyMessageReceivedCallback called when AjaxMessageQueue received empty message.no
onMessageReceivedCallback called when AjaxMessageQueue received message.no
defaultMessageReceivedtrue if default message received (insertion of message into source container) must be executed and false otherwise. By default defaultMessageReceived=trueno
repeatAfterEmptyMessageReceivedno

Scope management

  • application and session
  • request