Skip to main content
14-Alexandrite
November 19, 2024
Solved

ThingworxMultiEventSubscription with Java using ExtensionSDK

  • November 19, 2024
  • 1 reply
  • 729 views

Hello everybody,

has anyone exprience or can help to create a MultiEventSubscription using the ExtensionSDK?
So I am currently using Thingworx 9.6.1 with the ExtensionSDK 9.6.0. The latest available Eclipse Plugin i found is 9.0.1. When I use this combination to create an Extension and a ThingShape with an event and a subscription i get this result:

@ThingworxEventDefinitions(events = {
@ThingworxEventDefinition(name = "Message", description = "", category = "Message", dataShape = "TWX.Core.Message_DS", isInvocable = true, isPropertyEvent = false, isLocalOnly = false, aspects = {}) })
@ThingworxSubscriptions(subscriptions = {
@ThingworxSubscription(source = "", eventName = "Message", sourceProperty = "", handler = "OnMessage", enabled = true) })
public class ActorTS {
	private static Logger _logger = LogUtilities.getInstance().getApplicationLogger(ActorTS.class);

	public ActorTS() {
		// TODO Auto-generated constructor stub
	}

	@ThingworxServiceDefinition(name = "OnMessage", description = "Subscription handler", category = "", isAllowOverride = false, aspects = {"isAsync:false" })
	public void OnMessage(
		@ThingworxServiceParameter(name = "eventData", description = "", baseType = "INFOTABLE") InfoTable eventData,
		@ThingworxServiceParameter(name = "eventName", description = "", baseType = "STRING") String eventName,
		@ThingworxServiceParameter(name = "eventTime", description = "", baseType = "DATETIME") DateTime eventTime,
		@ThingworxServiceParameter(name = "source", description = "", baseType = "STRING") String source,
		@ThingworxServiceParameter(name = "sourceProperty", description = "", baseType = "STRING") String sourceProperty) {
	}
}

 

But I deeper look into the ExtensionSDK.shows, that there are special annotations for @ThingworxMultiEventSubscription:

@Target({ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@ThingworxExtensionApiAnnotation(since = {9, 5, 1})
public @interface ThingworxMultiEventSubscription {
 String name() default "";
 
 String description() default "";
 
 boolean enabled() default false;
 
 String handler();
 
 String[] aspects() default {};
 
 ThingworxSubscriptionEvent[] events() default {};
}
But there is no function in Eclipse to use them, no documentation and no example. 
Please help!
Best answer by atondorf

Hi Constantin,

that was the right hint ... especially the @ThingworxSubscriptionEvent-Annotation. But some more steps to go:
- I wanted the Subscription to be stateful and sequentially => aspects = { "isStateful:true" }

- I don't wanted to show up the Subscription Handler as a Separate Service => isPrivate = true

- The parameters of the Subscription Handler are different as it is a "MultiEventSubscription" ... 

@ThingworxEventDefinitions(events = {
 @ThingworxEventDefinition(name = "Message", description = "", category = "AMQP", dataShape = "TWX.Core.Message_DS", isInvocable = true, isPropertyEvent = false, isLocalOnly = true, aspects = {}),
})
@ThingworxMultiEventSubscriptions(subscriptions = {
 @ThingworxMultiEventSubscription(name = "SubMessageNew", enabled = true, handler = "OnMessageNew", aspects = { "isStateful:true" }, events = {
 @ThingworxSubscriptionEvent(source = "", eventName = "Message", sourceProperty = "", alias = "ME_Message") })	
}) 
public class ActorTS {
 @ThingworxServiceDefinition(name = "OnMessageNew", description = "Subscription handler", category = "", isPrivate = true, isAllowOverride = false, aspects = { "isAsync:false" })
 public void OnMessageNew(
 @ThingworxServiceParameter(name = "events", description = "", baseType = "INFOTABLE") InfoTable events, 
 @ThingworxServiceParameter(name = "thisSub", description = "", baseType = "INFOTABLE") InfoTable thisSub
 ) {
 LOGGER.trace("Entering Service: OnMessageNew with:");
 LOGGER.trace("Exiting Service: OnMessageNew");
 }
}

Still not everything clear (PTC please provide a more detailed explanation of Annotations and especially the aspects!), but it's running as intended ... 

 

1 reply

18-Opal
November 19, 2024

Hello, in your example I'd expect it to work like this:

@ThingworxEventDefinitions(events = {
	@ThingworxEventDefinition(name = "Message", description = "", category = "Message", dataShape = "TWX.Core.Message_DS", isInvocable = true, isPropertyEvent = false, isLocalOnly = false, aspects = {}) 
})
@ThingworxMultiEventSubscriptions(subscriptions = {
	@ThingworxMultiEventSubscription(enabled = true, handler = "OnMessage", events = {
		@ThingworxSubscriptionEvent(source = "Thing1", eventName = "Message", sourceProperty = "", alias = "MessageFromThing1"),
		@ThingworxSubscriptionEvent(source = "Thing2", eventName = "Message", sourceProperty = "", alias = "MessageFromThing2")
	}),	
})
public class ActorTS {
	private static Logger _logger = LogUtilities.getInstance().getApplicationLogger(ActorTS.class);

	public ActorTS() {
		// TODO Auto-generated constructor stub
	}

	@ThingworxServiceDefinition(name = "OnMessage", description = "Subscription handler", category = "", isAllowOverride = false, aspects = {"isAsync:false" })
	public void OnMessage(
		@ThingworxServiceParameter(name = "eventData", description = "", baseType = "INFOTABLE") InfoTable eventData,
		@ThingworxServiceParameter(name = "eventName", description = "", baseType = "STRING") String eventName,
		@ThingworxServiceParameter(name = "eventTime", description = "", baseType = "DATETIME") DateTime eventTime,
		@ThingworxServiceParameter(name = "source", description = "", baseType = "STRING") String source,
		@ThingworxServiceParameter(name = "sourceProperty", description = "", baseType = "STRING") String sourceProperty) {
	}
}
atondorf14-AlexandriteAuthorAnswer
14-Alexandrite
November 19, 2024

Hi Constantin,

that was the right hint ... especially the @ThingworxSubscriptionEvent-Annotation. But some more steps to go:
- I wanted the Subscription to be stateful and sequentially => aspects = { "isStateful:true" }

- I don't wanted to show up the Subscription Handler as a Separate Service => isPrivate = true

- The parameters of the Subscription Handler are different as it is a "MultiEventSubscription" ... 

@ThingworxEventDefinitions(events = {
 @ThingworxEventDefinition(name = "Message", description = "", category = "AMQP", dataShape = "TWX.Core.Message_DS", isInvocable = true, isPropertyEvent = false, isLocalOnly = true, aspects = {}),
})
@ThingworxMultiEventSubscriptions(subscriptions = {
 @ThingworxMultiEventSubscription(name = "SubMessageNew", enabled = true, handler = "OnMessageNew", aspects = { "isStateful:true" }, events = {
 @ThingworxSubscriptionEvent(source = "", eventName = "Message", sourceProperty = "", alias = "ME_Message") })	
}) 
public class ActorTS {
 @ThingworxServiceDefinition(name = "OnMessageNew", description = "Subscription handler", category = "", isPrivate = true, isAllowOverride = false, aspects = { "isAsync:false" })
 public void OnMessageNew(
 @ThingworxServiceParameter(name = "events", description = "", baseType = "INFOTABLE") InfoTable events, 
 @ThingworxServiceParameter(name = "thisSub", description = "", baseType = "INFOTABLE") InfoTable thisSub
 ) {
 LOGGER.trace("Entering Service: OnMessageNew with:");
 LOGGER.trace("Exiting Service: OnMessageNew");
 }
}

Still not everything clear (PTC please provide a more detailed explanation of Annotations and especially the aspects!), but it's running as intended ...