Using Events with Microsoft Exchange
Exchange is very different in the way it exposes events - you do not use a regular type library (CDO.DLL) and run com2java on it, etc. If you noticed, the type library contained in CDO.DLL does not have any declarations for exposed events. Events in the Exchange Server are actually run as a separate service known as the Microsoft Exchange Event Service (EVENTS.EXE). Two things of note:
1. The Event Service can only monitor folders stored on an Exchange server. It will not monitor folders stored on the local machines of users. For that, you'll need the Outlook type library instead (which contains events you can hook into) and not CDO.DLL.
2. The Event Service fires events asynchronously rather than synchronously in the context of the Exchange Information Store, so the Information Store won't block your event script, other processes, or people from working on items in the folder if the script hasn't run yet.
The general idea
The Event Service monitors changes to a folder and triggers an event if such change occurs. A change could either be one of the following:
- A new item is added to a folder (e.g. an new mail arrives in your inbox)
- An existing item is modified (e.g. the business phone number of a contact item is changed)
- An item is deleted
- Scheduled events (e.g. events that are triggered every Saturday at 1:00 am)
Once the Event Service realizes that a change has occurred, it fires an event. It then looks for a corresponding event handler in the folder. The Exchange Event Service ships with one pre-built event handler, named the Exchange Event Scripting Agent that you can bind to events. This allows you to write either VBScript or JScript to perform actions when specific events occur. Among things that you can do, you can call CreateObject(.) to create a Java object (via J-Integra) notifying it that an event has occurred. The following example demonstrates how this is done. Before you run the following example, we strongly suggest that you go through our basic VB-accessing-Java example to familiarize yourself on how to use J-Integra to call Java objects from COM clients. Once you are comfortable with that example, come back and run the following example.
Basic steps to get events running using the default Microsoft Event Scripting Agent
1. The first thing you'll need to do is to make sure the events.exe service is running in the Exchange Server. There are two ways to do this. Under Services, start the service named "Microsoft Exchange Event Service". I would also set it to Automatic start and give it a user account of the Administrators group. During development, it might be best to start events.exe in a Windows console as doing it this way provides valuable debugging information. Events.exe is usually located in C:\Program Files\Exchsrvr\BIN.
2. You'll need to give users the permission to create and bind agents in the system. This is accomplished by setting their permissions for a system folder named EventConfig_<servername>. This folder is found under the Events Root system folder in the Exchange Administrator program. Under Exchange Server 2000, click on Start -> Programs -> Microsoft Exchange -> System Manager. Go to Folders -> Public Folders -> Events Root -> EventConfig_<servername>. If you do not see "Events Root", right click on Public Folders and select "View system folders". Right click on EventConfig_<servername> and select Properties -> Permissions -> Client Permissions. In the Client Permissions dialog box, add users or distribution lists and assign them Author or higher permissions.
3. Next, you'll need to configure individual folders that want to receive events. To do this, login to Outlook using an account that has permissions to create agents as discussed above. Right-click on a folder, and select Properties. You should see a tab named "Agents". For the Agents tab to appear you must be the owner of the folder and the Server Scripting add-in must be installed. By default, Outlook does *not* install the Server Scripting add-in. To install the Server Scripting add-in, select Tools -> Options -> Advanced Options -> Add-In Manager. Check the Server Scripting check box to add the Agents tab to the folders where you have permissions to create agents. On the Agents tab, you can create, change, disable or delete agents in your folder.
4. Once you have the Agents tab up, click on New to create a new agent. The dialog window that comes up is self-explanatory. You can select which of the 4 events you want to occur (timer, new item occurs, changed item or deleted item). You can further select "A scheduled event occurs" checkbox to create a schedule-based event. For this example, simply choose to monitor "new item", "item changed" and "item deleted" events.
5. Proceed to the actual scripting by selecting the Script radio button (if not already selected) and then click on Edit Script. Windows Notepad will automatically display an ASP file that contains the event procedures to handle the 4 events. Only the structure is provided so you have to write your own code on how you would want to handle the events. In your implementation of the event handlers, you can use VBScript's CreateObject method to get access to a Java object via J-Integra. We have provided a sample VBScript program that does just this. Download ExchangeEvents.zip and open agents.asp using notepad or any text editor. Copy the contents of agents.asp and paste it to the agent script that showed up in Outlook when you click on Edit Script. Make sure that you overwrite the entire contents of the agent script with the one we have provided. Study the contents of the new script to see what's going on. Save and close the script.
6. Next, we register a JVM in the Exchange Server so it will know where and how to create the Java objects. Using 2 tools (regjvmcmd.exe and regprogid.exe) that come with the J-Integra for COM package, run the following commands in the Exchange Server:
regjvmcmd EventsJVM localhost
regprogid EventsJVM.EventDetailsJavaVersion "EventsJVM:EventDetailsJavaVersion"
For a discussion on what these commands do, refer to the regjvm and regprogid documentation.
7. Finally, we need to run the JVM that will host the Java objects that will be created and invoked by the event handlers. For ease of demonstration, you may run the JVM in the Exchange Server. During deployment, you may run the JVM in any machine that supports Java including Unix machines. Just make sure that you re-run regjvmcmd in the Exchange Server by providing the IP address of the machine running the JVM instead of "localhost". From ExchangeEvents.zip (which you have already downloaded) you will find 2 Java classes. The first one - EventsJVM.java - registers a JVM and will act as a server that listens to incoming connections. The other Java class - EventDetailsJavaVersion.java - is the actual Java Object that will be invoked by the event handlers. Compile both files - you will need jintegra.jar and cdo.jar, both are found in the lib folder of the J-Integra package. Run EventsJVM.
Now that we have all the pieces (events.exe, event handlers and the JVM) up and running, perform a test by doing any of the following in the folder where agents are bound: create a new item, modify an existing item or delete an item. Assuming these events are fired in this order, you should see an output in the JVM console similar to:
Note that by Microsoft Exchange standards, it takes 60 seconds or so from the time the action occurs to the time the events are triggered.
For more information on Exchange Event Scripting Agent, refer to:
If you prefer to write event handlers in C++, VB, or even pure Java (via J-Integra), you'll need to develop your own event handler rather than use the Event Scripting Agent. Consult http://support.microsoft.com/support/kb/articles/Q192/3/39.ASP which gives a "skeleton" example and also includes Agents.hlp, which is a more detailed explanation on how to create a custom handler.