Tips & Tricks - Polling

General

This workshop demonstrates how you can deliberately replace the content of a page in just a few minutes using the Intrexx polling mechanism - and only when this is actually required. It is aimed at readers with some programming experience. It is a common requirement that parts of a page (e.g. one or more tables) should be updated when changes are made to the underlying data records. This is especially the case when these are pages that are shown on shop floors, for example:

  • Andon boards

  • Kanban board

  • Bulletin board

  • Packaging regulations

  • Up-to-date information about manufacturing steps at a machine

  • Dashboards

and many other use cases. The function is required when the portal is not directly operated by employees but when a highly-visible screen needs to be refreshed automatically. Developers usually take the approach of reloading the entire displayed page with all of its elements in these cases. However, this creates unneccesary workload for the server because you do not always need to refresh all of the content. This approach can lead to long and increased loading times for many procedures.

Example files

The example files "70-test-polling.xml" (Spring configuration) and "Polling-Sample.zip" (application and process) can be downloaded here.

Functionality

Intrexx provides a functionality that enables a number to be queried on the server. This number is connected to a key.

Example: "testKey":

15 When a data record is changed and you want to force a refresh, you can increase this number using an Intrexx process. The browser page requests this value at a predefined interval. As soon as the value changes, the polling mechanism calls up a function defined by you. Here is the sequence:

"testKey": 15 -> Change to data record triggers the event -> Process responds and changes the value: "testKey": 16 -> Polling is triggered: Content is reloaded

The advantage of this method is that the request of the polling endpoint only takes about 4-5 milliseconds. However, reloading the entire page would take much longer - up to 100 times longer with a lot of content.

Implementation

Definition of the polling endpoint

The polling endpoint is a Java class with the name "JsonUsnPollingEndpoint". The polling endpoint is a Java class with the name "JsonUsnPollingEndpoint". It has the property of returning the value to the client in a JSON structure. Proceed as follows:

  1. Proceed as follows: Create a file with a name (as unique as possible) in the portal directory internal/cfg/spring (e.g. 77-mytestpolling.xml). This is insignificant in the case of polling.

  2. Copy the following content into this file:

    <?xml version="1.0" encoding="UTF-8"?> 
    <beans  xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><!-- Polling endpoint configuration --> 
    	<bean class="de.uplanet.lucy.server.polling.PollingEndpointConfigurator">		<property name="endpointName" value="testKey" />		<property name="endpoint">			<bean class="de.uplanet.lucy.server.polling.JsonUsnPollingEndpoint" /> 
    		</property> 
    	</bean></beans>
  3. Then save the file.

  4. Restart the portal service so that the new polling endpoint is active.

Increase the value in the polling endpoint via a process

Proceed as follows:

Polling JavaScript in the application

Now the following JavaScript needs to be added to your application for the polling. The page that should be refreshed needs to call the function "startPolling()" in the onLoad event. The function stopPolling() needs to be called in the onUnload event. The developer's task is to bring the checkReload() function to life. Currently, only a notification will be shown in the browser when a change has been made.

/** 
* Wird im OnLoad der Seite aufgerufen. 
* Initialisiert den Pollingmechanismus 
* key: Dieser Wert (Im Beispiel "testKey") wird initial festgelegt und muss bei den 
* Bean-Properties definiert sein. Der verwendete Polling-EndPoint wird dann entsprechend 
* überprüft. 
* success (optional): Bei Abweichung des Wertes wird die Methode checkReload() 
* aufgerufen. 
* error(optional): Kommt es beim Lesen des Wertes vom EndPoint zu einem Fehler wird in 
* diesem Beispiel die Methode "errorOnPolling()" aufgerufen. 
* id: Mit dieser Subscriber-Id arbeitet das Polling intern. So können z.B. zu einem 
* identischen Key mehrere Polling-Anfragen mit unterschiedlicher Subscriber-Id 
* gestartet werden. 
* Weiterhin ist es so auch möglich, das Polling-Interval dynamisch anzupassen (z.B. 
* wenig Änderungen: langsamer) 
* millisec: Polling Interval in Millisekunden, im Beispiel 5000 
* @method startPolling 
*/ 

function startPolling()
{ 
	registerAjaxPolling({ 
		key: "testKey", 
		success: function(){ 
			checkReload(); 
		}, 
		error: function(){
			errorOnPolling(); 
		},
		id: "testId",
		millisec: 5000 
	}); 
}

/** 
* Beendet das Polling und sollte immer im OnUnload einer Seite aufgerufen werden 
* @method stopPolling 
*/ 

function stopPolling()
{ 
	unregisterAjaxPolling("testKey", "testId"); 
} 

//In diesem Beispiel wird die Tabelle auf der Übersichtsseite neu geladen 

function checkReload()

{ 
	alert("Page should be reloaded."); 
	return true; 
} 

// In diesem Beispiel wird ein Benachrichtigungs-Fenster angezeigt. 

function errorOnPolling()
{ 
	Notifier.status.notify("Text: Error on retrieving reload information", "Titel: Polling", "ID: MyId"); 
}

Polling should always be used sparingly because it adds to the server workload. Choose your intervals carefully.