cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Stay updated on what is happening on the PTC Community by subscribing to PTC Community Announcements. X

How to know if a dynamic subscription alreay exists?

fmanniti
9-Granite

How to know if a dynamic subscription alreay exists?

Hello, I wrote a service I called SetDynamicSuscriptionToStream in a thing template which generates a dynamic subscription to a stream (MyStream).

MyTemplate : ThingTemplate (<- service SetDynamicSuscriptionToStream() is in here)

MyThing : is a Thing which has MyTemplate as Base Thing Template.

In MyTemplate there is a ThingStart subscription which runs SetDynamicSuscriptionToStream().

Now, my problem is anytime I work on MyTemplate and update it, MyThing is automatically restarted and SetDynamicSuscriptionToStream() is triggered and it tries to create the dynamic subscription again, so I get an error message

There is already a subscription to MyStream for MyThing:Type.Thing:Entity.MyStreamEvent.MyEvent

Is there a way to check if a dynamic subscription exists so that I don't get this error?

Thanks

 

1 ACCEPTED SOLUTION

Accepted Solutions

Try/catch on remove, that's the way I do:

 

params = {
           propertyName: subscription.propertyName,
           thingName: subscription.thingName,
           eventName: subscription.eventName,
           serviceName: subscription.serviceName
       };
// -- We ensure not to trigger an error about duplicating a subscription
 // -- Also that's good on the case we are executing this after a dependency
 // -- Restart and becouse of the current Dynamic Subscription it's outdated
try{
     refThing.RemoveDynamicSubscription(params);
     refThing.AddDynamicSubscription(params);
 } catch(err) {
     logger.error("[wupGenericTS("+thingName+")].Init Error re-enabling a dynamic subscription "+JSON.stringify(params)+". ERROR: "+err);
   }

View solution in original post

5 REPLIES 5
jbester
13-Aquamarine
(To:fmanniti)

Good day @fmanniti,

 

I am not sure whether there is a way to get the current subscriptions, I would also like to know if there is a way I am not aware of, but one way you could avoid the error is to put the SetDynamicSuscriptionToStream code body into a try-catch statement (found under flow control in snippets). 

 

The downside to this is you would not pick up an error if something else goes wrong either, but if you are confident in the inputs to create the subscription it should not be a problem.

 

I hope it helps a bit.

 

Kind regards,

Johan Bester

Just remove it first, and then recreate it.

Actually that's not the solution because when I create it in first place (at "real" thing start), I get the error that you cannot remove a dynamic subscription which doesn't exists

Try/catch on remove, that's the way I do:

 

params = {
           propertyName: subscription.propertyName,
           thingName: subscription.thingName,
           eventName: subscription.eventName,
           serviceName: subscription.serviceName
       };
// -- We ensure not to trigger an error about duplicating a subscription
 // -- Also that's good on the case we are executing this after a dependency
 // -- Restart and becouse of the current Dynamic Subscription it's outdated
try{
     refThing.RemoveDynamicSubscription(params);
     refThing.AddDynamicSubscription(params);
 } catch(err) {
     logger.error("[wupGenericTS("+thingName+")].Init Error re-enabling a dynamic subscription "+JSON.stringify(params)+". ERROR: "+err);
   }

Yes, I already have a try-catch but that's the problem because I'm using it in all services to create my own stream of error and the stream is always so full because of this error.

I was trying to comment the line, but in this way I lose all the other errors and I don't want because we're still in test phase so I'm not that confident yet.

Top Tags