|
|
The publisher is the entity that fires an event. It can be an application, as in this example, or it can be a component. COM+ doesnt limit your options in this area. The only thing the publisher must do is invoke the method required to fire the event contained within the event object. The sample application fires an event that broadcasts messages to all subscribers. Figure 10-4 shows the simple interface for this portion of the example.
Figure 10-4:
The publisher relies on a simple interface to fire an event that broadcasts a message to the subscribers.
As you can see, all you need to do is type a message and click Publish to publish the event. The code for this example looks remarkably similar to the code in Listing 8-6. However, instead of accepting input from the component, this example pushes data to the component in a manner similar to the example in Listing 8-2. You still need to add a reference to the type library for the event object type library, as shown here:
//Importthetypelibraryandusetheassociatednamespace. #import "SimpleEventObject.TLB" named_guids usingnamespaceSimpleEventObject;
The publishernot the subscribersmust reference the event object. Some developers get
Listing 10-2: Simple event publisher
|
|
voidCPublisherDlg::OnBnClickedPublish()
{
ISendMsg*pSendMsg;//PointertoConstStringobject.
_bstr_tOutput;//Temporarydatastring.
CStringData;//Datafromthemessagebox.
//InitializetheCOMenvironment.
CoInitialize(NULL);
//Createtheobject.
CoCreateInstance(CLSID_SendMsg,
NULL,
CLSCTX_ALL,
IID_ISendMsg,
(void**)&pSendMsg);
//Sendthemessage.
MsgText.GetWindowText(Data);
Output=Data;
pSendMsg->FireBroadcastMsg(Output);
//UninitializetheCOMenvironment.
CoUninitialize();
}
|
|
As you can see, the code begins by creating an instance of the object. As usual, the CLSID_SendMsg and IID_ISendMsg values come from the TLH and TLI files created from SimpleEventObject.TLB during compilation.
Once the code has an instance of the object to use, it obtains the string typed by the
|
|
|
|
The
Remember that the publisher is firing the event and that the event object
Listing 10-3: Component style subscriber
|
|
namespaceSubscriberComponent
{
///<summary>
///Thisclasssubscribestotheeventobjectthatholds
///themessagecreatedbythepublisher.
///</summary>
[Guid("3162ED10-C74D-4967-BA51-C8EBD10A8D03"),
ClassInterface(ClassInterfaceType.None)]
publicclassSendMsg:ServicedComponent,ISendMsg
{
publicSendMsg()
{
}
///<summary>
///Thismethodactsasatemplatefortheactual
///eventobject.
///</summary>
///<paramname="strMsg">Thedatawewanttosend
///asabroadcastmessage.</param>
publicvoidFireBroadcastMsg(StringstrMsg)
{
MessageBox.Show(strMsg,
"PublisherMessage",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
}
|
|
You should notice something almost immediately about the subscriberit contains no interface description. When you look at the project, youll notice a reference to the event object, SimpleEventObject . The event object provides the interface in this case. Using the interface in this way ensures that the subscriber matches the event object so that the two are compatible.
Note that the namespace for this subscriber is different than the event object. This isnt required, but using a different namespace does make it easier to identify the subscriber. The use of a different GUID for the
SendMsg
class is required. The event object class is different than the subscriber class. In fact, you dont have to use the same
The FireBroadcastMsg() method contains a simple implementation. The only thing that happens, in this case, is that the event handler displays a message box containing the string passed from the publisher. Obviously, you could set this up to do anything that any other event handler would do.
Youll create an application and register the subscriber just as you always do when working with COM+. Chapter 2 contains complete instructions for this process. The example uses an application name of SimpleSubscriber. However, when working with a subscriber component, you must create the subscription. The following steps show how:
In Component Services, create a COM+ component named SimpleSubscriber and install the SubscriberComponent.
Right click the SimpleSubscriber\
Click Next. Youll see a Select Subscription Method(s) page like the one shown in Figure 10-5. This is where you choose the methods that you want to use to accept events from a publisher. Notice that all the interfaces and
Figure 10-5:
The first step is to select the methods you want to receive events.
Select just the
FireBroadcastMsg()
method, as shown in Figure 10-5, and then click Next. Windows will search the COM+ catalog for components that publish events that might fulfill your components needs. Once this search process is complete, youll see a Select Event Class page like the one shown in Figure 10-6. Notice that the event object we created appears in this dialog box. If you dont see the event object, you need to stop the procedure now and check the previous steps to create the SimpleEventApp application listed earlier in this chapter. Make sure you see the correct event object listed here;
Figure 10-6:
Make sure the event object appears in the dialog.
Select the SimpleEventObject.SendMsg class, and then click Next. Youll see the Subscription Options page shown in Figure 10-7. This is where youll provide a name for your subscription and enable it. Always enable the subscription unless you dont want to receive events right away.
Figure 10-7:
Always enable the subscription as part of the configuration process.
Type a subscription name (the example uses My Subscription), select the Enable This Subscription Immediately option, and then click Next. Youll see a success message.
Click Finish. At this point, you should see a new subscription added to the Subscriptions folder. Figure 10-8 shows a typical example of a subscription.
Figure 10-8:
The subscription should appear in the Subscription folder once you create it successfully.
Youll need to export the application from the server and install it as a proxy application on the client as normal. At this point, everything is in place to test the first publish/subscribe setup. Start the Publisher application, type a message, and then click Publish. After a few seconds, the server should display a message box containing the text that you typed in the publisher. If you dont see the message box, make sure you enabled the subscriber. Also, verify that both the event object and the subscriber applications are active.
|
|