Browse DevX
Sign up for e-mail newsletters from DevX


Letting Java in on SQL Server Notifications : Page 3

SQL Server's Notification Services let you instruct the database to let your applications or your users know when data changes or reaches critical thresholds. Furthermore, standard notification delivery methods ensure that notifications aren't limited to .NET or Windows clients.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

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.

package com.devx.ns4j.mypackage; import java.util.*; import java.io.*; 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( "F:\\filenotifications.txt"); nMon.setFile(); Timer thetimer = new Timer (true); thetimer.schedule(nMon,0,1000); while(true) { } } public void run() { long nCurrentFileTime = theFile.lastModified(); if (nCurrentFileTime!=theFileTime) { // Alert -- we have a change in the // notification file // We should then also reset the File time // using setFile String strDoc = ""; String strLine = ""; try { BufferedReader bIn = new BufferedReader(new FileReader(theFile)); while ((strLine = bIn.readLine()) != null) strDoc+=strLine; } catch (Exception ex) { ex.printStackTrace(); } setFile(); } else { // 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.

Laurence Moroney is a freelance enterprise architect who specializes in designing and implementing service-oriented applications and environments using .NET, J2EE, or (preferably) both. He has authored books on .NET and Web services security, and more than 30 professional articles. A former Wall Street architect, and security analyst, he also dabbles in journalism, reporting for professional sports. You can find his blog at http://www.philotic.com/blog.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date