Chapter 3. Subscription-based notifications 83
Refer to Chapter 1, “Intelligent Notification Services” on page 1 for details about
how to create and configure Intelligent Notification Services users.
The following sections explain how to develop and deploy each component of
this sample scenario using IBM WebSphere Studio.
3.3 Content adapter
Before you develop a content adapter, you must understand how you will obtain
data from the information source. For purposes of this example, this section
contains four parts:
򐂰 Developing a sample content source program that feeds the content adapter.
򐂰 Deploying that sample content source program.
򐂰 Developing a sample content adapter.
򐂰 Deploying that sample content adapter.
3.3.1 Developing a content source program
The characteristics of this sample content source program are:
򐂰 The program monitors the HTTP service status of the determined servers
every five minutes.
򐂰 There is a properties file that contains the URL addresses of the servers to be
monitored. This file is read by the content source program.
򐂰 A server status string is generated by this program after each server is
monitored. The possible values for this string are:
All servers are up.
At least one server is down.
All servers are down.
򐂰 Communication between the content source and the content adapter is
through sockets. A properties file contains the information that is needed to
establish this connection.
The steps to develop this sample program are:
1. Create a Java Project:
a. Open the IBM WebSphere Studio Site Developer and select File
New
Project.
b. Select Java from the left panel and Java Project from the right panel.
Click Next.
84 IBM WebSphere Everyplace Access V5, Volume IV: Advanced Topics
c. Enter a project name. In this example it is MonitoringContentFeed. Click
Next.
d. Define the Java build settings and click Finish.
2. Create a new package. In this example the package is
com.ibm.pvc.ins.sm.apps.
3. In the new package, create a new Java file for the content source. In this
example, the name of the Java file is MonitoringContentFeeder.java.
4. Import the required packages as shown in Example 3-1.
Example 3-1 Required packages for MonitoringContentFeeder class
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.PropertyResourceBundle;
import java.net.*;
5. Create a new class. Declare auxiliary variables for sockets communication,
properties file (fully qualified class name), and server status messages as
shown in Example 3-2.
Example 3-2 Auxiliary variables for MonitoringContentFeeder class
public class MonitoringContentFeeder {
public static String host;
public static int port;
private final static String propertiesFile = "MonitoringFeederProperties";
private final static String allUp = "All Servers are Up";
private final static String allDown = "All Servers are Down";
private final static String atLeastOneDown = "At least one server is Down";
6. Create a function that establishes a stream socket connection to the host
where the content adapter resides, and send the server status information
Chapter 3. Subscription-based notifications 85
through this connection as shown in Example 3-3. Information about socket
connection is read from a properties file.
Example 3-3 feedToContentAdapter method implementation
private static boolean feedToContentAdapter(String sfeeded) {
try {
PropertyResourceBundle pr =
(PropertyResourceBundle)PropertyResourceBundle.getBundle(propertiesFile);
host = pr.getString("MonitoringContentAdapter.host");
port =
Integer.parseInt(pr.getString("MonitoringContentAdapter.port"));
Socket socket = new Socket(host, port);
OutputStreamWriter out = new
OutputStreamWriter(socket.getOutputStream());
out.write(sfeeded,0,sfeeded.length()-1);
out.flush();
out.close();
} catch (UnknownHostException e) {
return false;
} catch (IOException e) {
return false;
}
return true;
}
7. In the main method, define an infinite loop with the following attributes:
a. Wait and sleep for five minutes so that the server is checked every five
minutes for a status change.
b. Read the properties file to obtain the URL addresses and the quantity of
servers that are monitored.
c. For each monitored server, try to establish an HTTP connection.
d. Depending on the HTTP connection results, set the server status
message. (One message status is generated for the whole group of
servers.) Send the server status message to the content adapter using the
function defined earlier. (In this example, it is feedToContentAdapter.)
86 IBM WebSphere Everyplace Access V5, Volume IV: Advanced Topics
Example 3-4 Main method of class MonitoringContentFeeder
public static void main(String[] args)
{
String monitoringServerURL;
InputStream connection;
System.out.println("Starting Monitoring Content Feeder....");
do {
String mssg = "ServerStatus: ";
int srvQuantity = 0;
int count = 0;
try {
Thread.sleep(300000);
PropertyResourceBundle pr =
(PropertyResourceBundle)PropertyResourceBundle.getBundle(propertiesFile);
srvQuantity = Integer.parseInt(pr.getString("ServersQty"));
for (int i=1;i<=srvQuantity;i++) {
monitoringServerURL = pr.getString("MonitoringServerURL"+i);
try {
connection = new URL(monitoringServerURL).openStream();
if (connection != null)
count++;
} catch (Exception e){
}
}
if (count == srvQuantity) {
mssg = mssg + allUp;
} else
if (count == 0) {
mssg = mssg + allDown;
} else {
mssg = mssg + atLeastOneDown;
}
mssg = mssg + System.getProperty("line.separator");
if (!feedToContentAdapter(mssg)) {
System.err.println("Content feed error.");
return;
}
System.out.println("Message: "+mssg);
} catch (Exception e) {
System.out.println(e.getMessage());
}
} while(true);
}

Get IBM WebSphere Everyplace Access V5 Handbook for Developers and Administrators Volume IV: Advanced Topics now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.