Skip to main content
1-Visitor
February 26, 2020
Solved

ThingWorx : Fetching service metadata of Things via Java sdk

  • February 26, 2020
  • 1 reply
  • 4135 views

Hi,

 

I am exploring the rest apis and java sdk apis for ThingWorx 8.5. I want to fetch the metadata of a Thing. I can do the same via REST api - <server/Thingworx/Things/{thing_name}. It returns json that contains all the services listed under key serviceMappings with their implementation under key serviceImplementations -

 

image.png

 

But the parallel java api on ConnectedThingClient getThing(<name>).getMetadata() returns _serviceDefinitionCollection and _serviceProcessors as null. 

Am I missing anything here? How can I fetch the metadata for the Things' services ?

 

Update : Java sdk version latest - 6.1.0.679, ThingWorx Foundation version - 8.5

Best answer by billrei

You can make the same calls that the platform uses like EntityServices.getEntityList() but be careful because collecting this kind of data can return a lot of rows. This makes is hard to get your timeout correctly. Use the row limits parameter and do not try to make a single call to get it all because the total number of entities grows over time and eventually you will start timing out on your service calls.

1 reply

12-Amethyst
February 26, 2020

Have you called initializeFromAnnotations() in your VirtualThing constructor?

The classes you are getting back from the getMetaData() call are not obtained from the platform, they are generated from the annotations on your Thing.

 

If you want to make the same service call you did using REST you would have to connect to ThingWorx using the SDK and then call:

 

InfoTable theMetaData = thing.invokeService("getMetadata", new ValueCollection());

 

This call would cause the SDK to make a service request to the platform, like you did with your original REST request.

1-Visitor
February 26, 2020

Hi, 

 

I have a Thing named MyHouse on the platform. I am trying to connect to the platform using following snippet -

VirtualThing myHouse = new VirtualThing("MyHouse", "Sample Description", client);
client.bindThing(myHouse);
client.start();
InfoTable metadata = myHouse.invokeService("GetMetadata", new ValueCollection());

 This returns an empty InfoTable instance.

When I used invokeService() api, I get the correct result InfoTable - 

InfoTable metadata = client.invokeService(RelationshipTypes.ThingworxEntityTypes.Things, "MyHouse", "GetMetadata", new ValueCollection(), 30000);

Is this a correct way to connect to the platform?

12-Amethyst
February 26, 2020

Both methods should work the same. Client.InvokeService() needs to know what Thing you want to talk to. VirtualThing.invokeService() already knows this. The calls work the same way. The difference is that VirtualThing.invokeService() is dependent on the presence of Metadata either provided through annotations or through service bindings established on the platform.  You bypass all that with Client.InvokeService() and I should have offered that to you as an option in the first place. You are doing things the correct way.

 

Also, you are now doing the exact same thing you were doing when you were using the REST interface.