I had java scheduler based implementation to read gcp pubsub msg but then I changed the same to use gcp pubsub MessageReceiver to listen and read msg asynchronously.
// Class that implemented MessageReceiver contains code
@Override
public void receiveMessage( PubsubMessage message, AckReplyConsumer ackReplyConsumer ) {
//some code
processMsg( message, blobs, profiler, ackConsumer );
}
private void processMsg( PubsubMessage message, Page<Blob> blobs, Profiler profiler, AckReplyConsumer ackConsumer ) {
setContext("id");
}
private void setContext( String id ) {
UserContext userContext = UserContext.getContext();
// when I change implementation to- use MessageReceiver instead of scheduler, I am getting userContext=null here;
}
//class UserContext{
private static final ThreadLocal<UserContext> threadLocal = new ThreadLocal<UserContext>();
public static UserContext getContext() {
try {
final UserContext userContext = threadLocal.get();
if ( null != userContext ) {
return (UserContext)userContext.clone();
}
} catch( CloneNotSupportedException e ) {
}
return null;
}
}
How to get userContext=notNull when we call UserContext.getContext(). I am new to thread so not sure how to resolve this null pointer.