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

Community Tip - Did you get called away in the middle of writing a post? Don't worry you can find your unfinished post later in the Drafts section of your profile page. X

Information on ThingStart Event and constructor and destructor for a Thing

amittal-3
13-Aquamarine

Information on ThingStart Event and constructor and destructor for a Thing

Hello,

I have a requirement to perform some action at startup (like filling some data tables or running some services associated with Thingworx entities), for that I am using a subscription to ThingStart Event. But I would like to have some more insights on how and when does ThingStart event occurs/triggers. I read in some documentation, that ThingStart event is fired when the Thing is created, so does that mean it occurs only once in the lifetime of a Thing? I tried to look for some detailed information for ThingStart event, but was unable to find it.

 

Another part of my question is what is the provision to have constructor and destructor feature for Thingworx entities, say for example, I would like to associate my Things with a Value Stream, when it is created (so I could do - Create a new Thing, Create a new Value Stream and assign the ValueStream to a that thing).

Similarly while destroying a Thing I would like to de-assign the Value stream, delete the thing and delete the value stream.

 

So how can I achieve this kind of behaviour in Thingworx?

 

Thanks in advance

Regards

Aditya Mittal

1 ACCEPTED SOLUTION

Accepted Solutions
AdamR
12-Amethyst
(To:amittal-3)

Hi Aditya,

 

ThingStart occur every time a Thing is started (not just when it is created).  This occurs each time the Thing is saved (the platform removes the current running thing from the JVM in order to load/start the newly saved structure).  It will also occur when the platform starts up, when it enters and active state (if it was de-activated) or if the RestartThing service runs.

Do keep in mind that when the platform starts there is no predefined order in which each Thing will become active.  So if you are relying on another Thing in the platform (like a DataTable) to be available at this event,  you may have errors.

 

To create a Thing you can use the Resources built in to the platform.  There is an EntityServices resource (CreateThing) that you can use to create the Thing and the ValueStream (which is just a Thing with the ValueStream template).  After creating both you use the SetValueStream service on the new Thing to assign the value stream.  

 

You should not need to de-assign the ValueStream from the Thing if you are deleting that Thing.  They existing independently so you can just delete them both.  However you would need to delete the Thing before the ValueStream since there is a dependency there.

 

Let me know if that makes sense or if you have additional questions.

 

Adam

View solution in original post

4 REPLIES 4
AdamR
12-Amethyst
(To:amittal-3)

Hi Aditya,

 

ThingStart occur every time a Thing is started (not just when it is created).  This occurs each time the Thing is saved (the platform removes the current running thing from the JVM in order to load/start the newly saved structure).  It will also occur when the platform starts up, when it enters and active state (if it was de-activated) or if the RestartThing service runs.

Do keep in mind that when the platform starts there is no predefined order in which each Thing will become active.  So if you are relying on another Thing in the platform (like a DataTable) to be available at this event,  you may have errors.

 

To create a Thing you can use the Resources built in to the platform.  There is an EntityServices resource (CreateThing) that you can use to create the Thing and the ValueStream (which is just a Thing with the ValueStream template).  After creating both you use the SetValueStream service on the new Thing to assign the value stream.  

 

You should not need to de-assign the ValueStream from the Thing if you are deleting that Thing.  They existing independently so you can just delete them both.  However you would need to delete the Thing before the ValueStream since there is a dependency there.

 

Let me know if that makes sense or if you have additional questions.

 

Adam

amittal-3
13-Aquamarine
(To:AdamR)

Hello Adam,

Thanks for the detailed reply on the queries that I had.

I think that would be all the information I would need as of now on these topics. I shall 'accept it as Solution'.

Regards

Aditya

amittal-3
13-Aquamarine
(To:amittal-3)

Hello Adam,

Related to the above topic, I think we forgot to discuss one part which is - 

Say that the thing is deleted (which had a valuestream assigned to it), now that value stream is no longer required to be kept as a Resource, how will I know that now I should delete the value stream. I mean is there a possibility that I get the event when the  thing is deleted so that I can subscribe to that event to delete value stream or carry out other tear-down process which are required after delete of any particular thing, maybe remove its references from other places.

Or do we have the possibility of some sort of destructor, to which we have access  and which gets called before the memory for that thing is de-allocated. So I can remove the references of the thing before its getting deleted in its destructor.

I hope I am able to pen down my query in proper manner, so that you can guide me, if I am thinking correctly or not.

Thanks in advance

Regards

Aditya

AdamR
12-Amethyst
(To:amittal-3)

Hi Aditya,

 

Apologies for not seeing your reply sooner.  There is not any built in descructor event or call that can be caught in a subscription or service.  You could do a periodic check (using a daily scheduler) to see if there are value steams that are no longer attached to any other entity.  There is a service on each Thing named HasIncomingDependencies which can be called to check this.

 

Thanks,

Adam

Top Tags