Pub /Sub manual handling of Subscriptions

Nov 9, 2010 at 6:46 PM

Hi Thomas,

I would like to handle subscriptions manually so I can keep track of who has subscribed.

I have looked at the docs and seen how to handle UnSubscriptions

 [XcoConcurrent]
    void ProcessUnsubscribeManually(Unsubscribe<NewsItem> unsub)
    {
        ...
        publisher.Unsubscribe(unsub.EventPort);
    }
however, why does the same thing not hold for Subscriptions

 [XcoConcurrent]
    void ProcessSubscribeManually(Subscribe<NewsItem> sub)
    {
        ...
        publisher.Subscribe(sub.EventPort);
    }

does not compile and I get cannot convert from Microsoft.Ccr.Core.IPort to Microsoft.Ccr.Core.Port<NewsItem>

Can you shed some light?

Many thanks in advance

Mike

Nov 10, 2010 at 8:45 AM
Edited Nov 10, 2010 at 8:47 AM

Hi Mike,

In principle your code is correct, but the EventPort property of the Subscribe class is not of type Port<NewsItem>, but of the more general Type IPort. Because of that you need to make an explicit cast here:

[XcoConcurrent]
    void ProcessSubscribeManually(Subscribe<NewsItem> sub)
    {
        ...
        publisher.Subscribe((Port<NewsItem>)sub.EventPort);
    }

You are right though that this is difficult to understand, most of all why this is different to the Unsubcribe class. The next AppSpace release version will have this changed, so that EventPort returns a correctly typed port as expected.

Thomas

Nov 10, 2010 at 2:54 PM

Hi Thomas,

Thanks for this.

One more question. If I wanted to send the machinename along with the subscription request, what would be the best way to do this?

In my scenario,  what I would like to do is  record when subscribing workers have responded to a publish and asked for more records (re our previous thread) but then figure out if a request has not been answered in a particular timeframe so that the worker can be removed from the subscription and no more records are sent to this worker for processing e.g. in the even the worker is asking for more records but is actually no longer processing them. This should never happen as a worker that has not processed its current requests will not ask for more items but really what I want to do is remotely turn the windows service on this worker machine to allow it to recover (I of course could figure this out on the worker).

Thanks in advance

Mike

Nov 12, 2010 at 7:50 AM

Hi Mike,

The easiest way to get the address of the sender of a subscription request would be to use the message context:

[XcoConcurrent]
    void ProcessSubscribeManually(Subscribe<NewsItem> sub)
    {
        var senderAddress = MessageContext.Current.RemoteAddress;
        ...
        publisher.Subscribe((Port<NewsItem>)sub.EventPort);
    }

This way you get the complete address of the sender's appspace.

If you need to send more information than that, you could for example
- create a subclass of Subscribe<T> that additionally contains the fields you need,
- or wrap the subscribe message into another class and store the additional information there.

Hope I could help you!

Thomas