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 an answer that solved your problem? Please mark it as an Accepted Solution so others with the same problem can find the answer easily. X

Running another thread in the Thingworx Extension

jaeeunc
1-Visitor

Running another thread in the Thingworx Extension

Hi,

When I tried to run another thread in the Thingworx extension I'm making and tried to access an entity in the thread,

I got an error related 'permission', 'not authorized'.. something.

(for example, when writing property, 'com.thingworx.common.exceptions.InvalidRequestException: Not authorized.....'

or

ThingManager.getInstance().getEntity('mythingname') returns null )

-> Is it not permitted for the extensions to run another thread in it?

Or is there any way to get the access permission in the other thread?

3 REPLIES 3

Security permissions are tracked using ThreadLocal storage in the ThreadLocalContext class.  If you create a new thread that needs to access data from Thingworx, you will need to use ThreadLocalContext to assign it the proper security context.

Hi  Jesse,
I want to ask the a question about using ThreadLocalContext.I am trying run a thread in a function.

	public void startThread() {

		Thread t = new Thread(() -> {
			String line = "";
			try {
				SecurityContext systemContext = SecurityContext.createSystemUserContext();
				ThreadLocalContext.setSecurityContext(systemContext);
				Thing thing = ThingUtilities.findThing("AEOSEvents");
				ThreadLocalContext.clearSecurityContext();

				while ((line = reader.readLine()) != null) {
                                   thing.setPropertyValue("rawEvents", new StringPrimitive(line));
			}

				}
			} catch (IOException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
		});
		t.start();

	}

This function gets the data coming from the socket and assing this data into thinking property.But I get the same error.Not authorized for Property Write in thingworx.I looked at the thingworx extension API Doc. But I did not find any examples about this ThreadLocalContext.Where do I make mistakes.

baobao
4-Participant
(To:keles0glu)

I have the same problem, the solution is to set securitycontext this code to your thread it will work.

 

 

public void messageArrived(String topic, MqttMessage message) throws Exception { 
SecurityContext securityContext = SecurityContext.createSuperUserContext(); ThreadLocalContext.setSecurityContext(securityContext);

this.setPropertyValue("topic",new StringPrimitive(topic));
this.setPropertyValue("message",new StringPrimitive(message));

}

 

Announcements


Top Tags