How to access threadLocal variables in classes which extend TaskActivityBehavior class

am trying to set userId variable using spring interceptorHandle class

@Override
public boolean preHandle(HttpServletRequest request,
		HttpServletResponse response, Object object) throws Exception {
	log.info("Before Handler execution");
	String userId = request.getHeader(Constants.USER_ID_HEADER);
	String roles = request.getHeader(Constants.USER_ROLE_HEADER);
	
	log.debug("user["+userId+"] roles["+roles+"]");
	UserModel user = new UserModel();
	user.setUserId(userId);
	user.setRoles(roles);
	
	FlowUserContext.setUser(user);
	return true;
}

below is my FlowUserContext class
public class FlowUserContext {

private static final ThreadLocal<UserModel> CONTEXT = new ThreadLocal<UserModel>();

public static void setUser(UserModel user) {
	CONTEXT.set(user);
}

public static UserModel getUser() {
	return CONTEXT.get();
}

public static void remove() {
	CONTEXT.remove();
}

}

while retrieving same values from activity class(which extends TaskActivityBehavior) am not getting the values from ThreadContextLocal

public void execute(final ActivityExecution execution) throws Exception {
	
	log.debug("in execute executionId ["+execution.getProcessInstanceId()+"]");
	
	log.debug("user info - "+FlowUserContext.getUser().getUserId());
	
	
	
}

Here am getting null value for this FlowUserContext.getUser()
Am trying to get the userId in the flow which is required for me for further operations, am trying to inject it in threadContext so that it will be available throughout the flow. Is there any other way to achieve the same. Any help is appreciated.

i was able to achieve it using servlet filter and modifying the variables object while calling /start rest api.
so all the user info is now available as variables in flow.

1 Like