Skip to main content
1-Visitor
Oct 14, 2015 3:30 PM UTC
Solved

Extension : Thingworx Event Subscription

Hi,

 

I have created an extension in java. I want to create a subscription.

I've found "@ThingworxSubscription" annotation to create a subscription. But I didn't find source event !

 

For exemple, I can't create a subscription on event "DataChange".

Is it possible to get event list in java code ?

 

Thanks.

 

Regards,

 

    Julien

1-Visitor
Dec 2, 2015 9:57 PM UTC

To specify a subscription on the DataChange event in your example, you would want to do the following:

On the class, specify the following annotation (or similar):
@ThingworxSubscriptions(subscriptions = {

@ThingworxSubscription(eventName = "DataChange", sourceProperty="myProp", enabled=true, handler="SubscriptionHandler")

})

Then within your template or shape you will want to define the SubscriptionHandler service:

@ThingworxServiceDefinition(name="SubscriptionHandler", isPrivate=true)

public void SubscriptionHandler(

    @ThingworxServiceParameter(name="eventData", baseType="INFOTABLE", aspects = {"datashape:DataChangeEvent"}) InfoTable eventData,

    @ThingworxServiceParameter(name="eventName", baseType="STRING") String eventName,

    @ThingworxServiceParameter(name="eventTime", baseType="DATETIME") DateTime eventTime,

    @ThingworxServiceParameter(name="source", baseType="STRING") String source,

    @ThingworxServiceParameter(name="sourceProperty", baseType="STRING") String sourceProperty)

You should name the service something more useful than just SubscriptionHandler, I just used that as an example.

We're hoping to improve the documentation surrounding extension development in the next few releases, so hopefully some of these mysteries will be a little less mysterious.

12 replies

Oct 15, 2015 5:46 PM UTC

I have never done this, and there are no examples but from what you can tell from Eclipse IDE autocomplete, you get the following

@ThingworxSubscriptions(subscriptions = {

  @ThingworxSubscription(source="", sourceProperty="", eventName="", enabled=true, handler="")

})

Not sure if this will work but you can try it. Source and Source property, eventName and enabled, seem obvious but handler, I don't know. Maybe looking at at an XML export of a ThingTemplate where you have defined a subscription might lead to come clues.

Translate
1-Visitor
Oct 16, 2015 8:04 AM UTC

hi, Thanks

I've found the same thing. This code is placed before class définition.

A subscription is created but i don't know where write the subscription code and where to found event input. I've look a XML export and i'm surprised to see a service definition in subscriptions tag.

Translate
Oct 16, 2015 2:32 PM UTC

Yeah, I would imagine that is what the handler is, but how to write that and wire into Java code, I don't know. I have the same question posted in my lengthly list of questions elsewhere. Hopefully someone comes to our rescue - it's gotta be possible.

Translate
1-Visitor
Dec 2, 2015 9:57 PM UTC

To specify a subscription on the DataChange event in your example, you would want to do the following:

On the class, specify the following annotation (or similar):
@ThingworxSubscriptions(subscriptions = {

@ThingworxSubscription(eventName = "DataChange", sourceProperty="myProp", enabled=true, handler="SubscriptionHandler")

})

Then within your template or shape you will want to define the SubscriptionHandler service:

@ThingworxServiceDefinition(name="SubscriptionHandler", isPrivate=true)

public void SubscriptionHandler(

    @ThingworxServiceParameter(name="eventData", baseType="INFOTABLE", aspects = {"datashape:DataChangeEvent"}) InfoTable eventData,

    @ThingworxServiceParameter(name="eventName", baseType="STRING") String eventName,

    @ThingworxServiceParameter(name="eventTime", baseType="DATETIME") DateTime eventTime,

    @ThingworxServiceParameter(name="source", baseType="STRING") String source,

    @ThingworxServiceParameter(name="sourceProperty", baseType="STRING") String sourceProperty)

You should name the service something more useful than just SubscriptionHandler, I just used that as an example.

We're hoping to improve the documentation surrounding extension development in the next few releases, so hopefully some of these mysteries will be a little less mysterious.

Translate
5-Regular Member
Mar 15, 2016 3:03 PM UTC

To elaborate a little on this topic - for those wanting to use Events and Subscriptions in Java code, with the 7.1.0 release of the Eclipse Plugin, there will be wizards to generate the code for Events and Subscriptions within your .java files.  Hopefully this will help take some of the mystery out of the whole process.


The release date is still unofficial, but is planned for the same week as the 7.1.0 ThingWorx Platform release.

Translate
1-Visitor
Jun 2, 2016 1:19 PM UTC

Hi,

I've encountered some problems similar to what's been discussed here.

1) Is there an equivalent of this in the Edge .NET Sdk. I looked at the com.thingworx.metada and com.thingworx.metada.annotations, and couldnt find neither a type definition nor an attribute for "ThingworxSubscription" or something like that.

2) Also, I do wonder if the implementation above differs from adding a subscription in the platform composer, and calling the remote service callback in the handler script in the platform server.

I would really appreciate your help.

Thanks

Translate
1-Visitor
Jun 2, 2016 2:15 PM UTC

Not directly, no.  The easiest way to do this would be to create a service on your Edge virtual thing, then create a subscription on the RemoteThing that binds to your virtual thing where the subscription calls that service, passing it the event data as a parameter.

Translate