Welcome!

Machine Learning Authors: Yeshim Deniz, Zakia Bouachraoui, Pat Romanski, Elizabeth White, Liz McMillan

Related Topics: Java IoT

Java IoT: Article

Integrating AJAX with the JMX Notification Framework

Opposite Ends of the Systems Management Stack

The repaintable 'status' area of the screen is defined as follows:

<span id="adminBanner" class="style1"></span>

'adminBanner' will be used to identify the repaintable area when the XML response is parsed and the message extracted.

The initAdmin() method schedules a JavaScript method, trapAlert(), to be executed after callbackTimeout milliseconds:

function setCallback() {
callBack = setTimeout('trapAlert()',callbackTimeout);
}

function initAdmin() {
setCallback();
}

It's the trapAlert() method that initiates the XMLHttpRequest with the now familiar ring:

function trapAlert() {
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
}

req.onreadystatechange = processRequest;
req.open("GET", './admin?reqid=0', true);
req.send(null);
}

HTTP GET is used to read data (only a small request parameter is used), and the admin servlet is targeted. The request is asynchronous, and when the request state changes, the processRequest JavaScript function is invoked:

req.onreadystatechange = processRequest;

It might seem reasonable to wait for a response before continuing processing. However, you run the risk of having your script hang if a network or server problem prevents completion of the transaction. An asynchronous call with the onreadystatechange event is more resilient.

As the request cycles through to completion, the processRequest event handler is invoked:

function processRequest() {
if (req.readyState == 4) {
if (req.status == 200) {
parseMessages();
}
....
setCallback(); // only do this when complete
}
}

Listing 1 shows the available status codes. When the request is complete and HTTP status code 200 (OK) is returned, the parseMessages() method is called to extract the data from the XML message. Then the trapAlert() method is rescheduled again. If the XML response has a different retry interval, this will have been set by the parseMessages() function.

Parse XML Response and Repaint Screen
The parseMessages() function first extracts the XML response

response = req.responseXML;

and extracts the elements for alert status, alert text, and retry interval:

itemStatus = response.getElementsByTagName('status')[0].firstChild.nodeValue;
itemText = response.getElementsByTagName('textBody')[0].firstChild.nodeValue;
callbackTimeout =
parseInt(response.getElementsByTagName('callBack')[0].firstChild.nodeValue);

The alert text is then repainted on to the adminBanner document element (see above):

document.getElementById("adminBanner").innerHTML = itemText;

The alert message appears on the screen as shown in Figure 3.

Servlet Formats the XML Response
For the browser to display management alerts to the user, XMLHttpRequest is used to request the management state.

When the browser sends the request, the servlet uses the MBean helper to check the alert state and, if an alert is available, constructs an XML document as a response.

If there's no state to return, the response status is set as follows:

response.setStatus(HttpServletResponse.SC_NO_CONTENT);

Otherwise the text/XML response type is set:

response.setContentType("text/xml");

Listing 2 shows the servlet method in full.

When the servlet is invoked and the XML content returned, the console should print:

Received alert: alert.broadcast
<message><status>1</status><textBody><![CDATA[System Down in 10
Minutes]]></textBody><callBack>10000</callBack></message>

Capacity Modelling and Security
Because AJAX opens up the architecture in interesting ways, two key areas require elaboration:

  • Capacity Modelling
  • Security
Caching and response message type (XML or text) can also be important.

Capacity Modelling
AJAX-enabled rich clients won't necessarily submit requests any more frequently than before. But with XMLHttpRequest executed asynchronously in the browser, the number of HTTP requests to the server increases in line with the retry interval.

  • Retry interval (Think Time) = 20 seconds
  • Number of connected users = 5000
  • Transactions per second (TPS) = 5000/20 = 250
We expect an extra 250 requests (transactions) per second generated from the HTTP user base.

Of course, it depends what these requests do at the server to increase latency in response time. In our example, each request must look up MBean properties and format an XML response, but the response is very small and the MBean is in local memory. With each Web server thread able to handle approximately 200 GET requests a second, and the user requests load balanced across a J2EE server cluster of perhaps 200 threads, the increased loading isn't significant.

When modelling AJAX architectures, increased load injection can be offset by reduced bandwidth, since the response contains data only in contrast to data plus mark-up.

Security
Suppose you only wanted users in the WebUser group to access the Admin servlet?

If only authenticated users can access the admin servlet, then the XMLHttpRequest will run as that user if that user has authenticated.

For example, once user Joe logs into the application, and Joe is a member of group WebUser, the XMLHttpRequest will be able to invoke the Admin servlet.

Adding the following code to the admin servlet would confirm the authenticated subject, returning true and Joe respectively:

request.isUserInRole("WebUser");
request.getRemoteUser();

More Stories By Graham P. Harrison

Previously a Senior Consultant with BEA, Graham is the author of Dynamic Web Programming using Java (Prentice Hall, 2000) in addition to a number of articles for the Java Developers Journal and IBM DeveloperWorks. He has a focus on Enterprise Architecture, Performance Tuning and Capacity Planning

Comments (7)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


CloudEXPO Stories
DevOpsSUMMIT at CloudEXPO, to be held June 25-26, 2019 at the Santa Clara Convention Center in Santa Clara, CA – announces that its Call for Papers is open. Born out of proven success in agile development, cloud computing, and process automation, DevOps is a macro trend you cannot afford to miss. From showcase success stories from early adopters and web-scale businesses, DevOps is expanding to organizations of all sizes, including the world's largest enterprises – and delivering real results. Among the proven benefits, DevOps is correlated with 20% faster time-to-market, 22% improvement in quality, and 18% reduction in dev and ops costs, according to research firm Vanson-Bourne. It is changing the way IT works, how businesses interact with customers, and how organizations are buying, building, and delivering software.
Nicolas Fierro is CEO of MIMIR Blockchain Solutions. He is a programmer, technologist, and operations dev who has worked with Ethereum and blockchain since 2014. His knowledge in blockchain dates to when he performed dev ops services to the Ethereum Foundation as one the privileged few developers to work with the original core team in Switzerland.
It cannot be overseen or regulated by any one administrator, like a government or bank. Currently, there is no government regulation on them which also means there is no government safeguards over them. Although many are looking at Bitcoin to put money into, it would be wise to proceed with caution. Regular central banks are watching it and deciding whether or not to make them illegal (Criminalize them) and therefore make them worthless and eliminate them as competition. ICOs (Initial Coin Offerings) are something most have no idea as to what it means and how you utilize it. Where is the "Stamp of Approval" or "Stamp of Legitimacy" on some of these Bitcoin websites (how do you know you are not dealing with a scammer?)
Business professionals no longer wonder if they'll migrate to the cloud; it's now a matter of when. The cloud environment has proved to be a major force in transitioning to an agile business model that enables quick decisions and fast implementation that solidify customer relationships. And when the cloud is combined with the power of cognitive computing, it drives innovation and transformation that achieves astounding competitive advantage.
René Bostic is the Technical VP of the IBM Cloud Unit in North America. Enjoying her career with IBM during the modern millennial technological era, she is an expert in cloud computing, DevOps and emerging cloud technologies such as Blockchain. Her strengths and core competencies include a proven record of accomplishments in consensus building at all levels to assess, plan, and implement enterprise and cloud computing solutions. René is a member of the Society of Women Engineers (SWE) and a member of the Society of Information Management (SIM) Atlanta Chapter. She received a Business and Economics degree with a minor in Computer Science from St. Andrews Presbyterian University (Laurinburg, North Carolina). She resides in metro-Atlanta (Georgia).