Getting Java Into the Act
For the sake of simplicity and brevity, this article focuses on the file-drop method that the Stock sample uses. The best design for an application such as this is to have Notification Services write to a shared network directory that the Java application has access to. For this sample though, both Notification Services and the Java application run on the same machine, and the notifications directory has been mapped to the "F" drive. For example, here's some very simple Java code that monitors the drop directory for updates.
public class nsfilemonitor extends TimerTask
private File theFile;
private long theFileTime;
private String strFile;
public nsfilemonitor(String sFile)
strFile = sFile;
public void setFile()
theFile = new File(strFile);
theFileTime = theFile.lastModified();
public static void main(String args)
nsfilemonitor nMon = new nsfilemonitor(
Timer thetimer = new Timer (true);
public void run()
long nCurrentFileTime = theFile.lastModified();
// Alert -- we have a change in the
// notification file
// We should then also reset the File time
// using setFile
String strDoc = "";
String strLine = "";
BufferedReader bIn =
while ((strLine = bIn.readLine()) != null)
catch (Exception ex)
// Do nothing
String strTest = "Hello";
This application shown above is very straightforward. It caches the modification date and time of the file to watch, and sets up a timer that ticks every 1000 milliseconds. After each timer tick it compares the current modified date of the file with the cached one. If they differ, the file has changed within the last second, so the application opens it and reads the contents into the strDoc
string. The application then resets the cached modified date to the current one.
This sample application doesn't do anything with the notification beyond reading it into memory; but in a real application you would parse what you need out of it (the notification file is formatted as XML) and use the parsed information appropriately.
To write a Java application that gets an SMTP notification is very similar. You'd simply use an application that uses POP3 or another e-mail protocol to sign into the account to which the notifications are sent, check whether any new messages have arrived, and if so, handle them accordingly.
The HTTP channel is perhaps the most generally useful notification delivery channel. Depending on how you implement the notification service instance (HTTP services in SQL Server 2005 are extensible) you have a number of options on how to consume it from Java. One simple method is to create a Java servlet that receives the HTTP notifications as POST commands. Alternatively, the Apache Jakarta project has a full HTTP client
that you can use from within Java to build your own fully-featured client applications.
Notification Services are an extremely useful aspect of SQL Server 2005. Via notifications, you can greatly improve the responsiveness of many applications while simultaneously often significantly reducing server and network resource requirements. In this article you saw that you don't have to write .NET applications to make use of SQL Server Notification Services. Because the services deliver notifications via standard channels such as the file system, SMTP, and HTTP, it's easy to let Java (or other languages) take advantage of them as well.