Hi
We have a spring boot application, in which temporal workers are also running.
we want to propagate ServletRequestAttributes from service class to workflow.
- Tried writing CustomPropogator. in getCurrentContext() method got all the reqAttributes which were present in the service class. Now getting a few errors as mentioned below.
@Override
public Object getCurrentContext() {
Map<String, Object> contextMap = new HashMap<>();
var requestAttrs = RequestContextHolder.getRequestAttributes();
System.out.println("in getCurrentContext requestAttrs " + requestAttrs);
if (Objects.nonNull(requestAttrs)) {
HttpServletRequest request = ((ServletRequestAttributes) requestAttrs).getRequest();
Enumeration<String> s = request.getAttributeNames();
s.asIterator()
.forEachRemaining(
i -> {
//System.out.println("key inside getCurrentContext:" + i);
contextMap.put(i, request.getAttribute(i));
});
}
return contextMap;
}
a) Now in setCurrentContext() method tries to set the context in MDC. so that MDC can be used in wofkflowImpl and ActivityImpl class to pass the context info further.
The Problem is MDC takes only string as its key and value but reqAttributes are not necessary to be in string form. so how to propagate such objects.
b) If we are using MDC, which is the right place to clear all entries from MDC once execution is complete.
@Override
public void setCurrentContext(Object context) {
Map<String, Object> contextMap = (Map<String, Object>) context;
for (Map.Entry<String, Object> entry : contextMap.entrySet()) {
try {
MDC.put(entry.getKey(), (String) entry.getValue());
}
c)Are there any alternative solutions available instead of MDC, so we can use context in workflow and activity.
d) Many objects throw errors during serializing/deserializing method of the custom propagator, here is the code of that methods.
@Override
public Map<String, Payload> serializeContext(Object context) {
Map<String, Object> contextMap = (Map<String, Object>) context;
Map<String, Payload> serializedContext = new HashMap<>();
for (Map.Entry<String, Object> entry : contextMap.entrySet()) {
serializedContext.put(entry.getKey(),
DataConverter.getDefaultInstance().toPayload(entry.getValue()).orElse(null));
}
return serializedContext;
}
@Override
public Object deserializeContext(Map<String, Payload> context) {
Map<String, Object> contextMap = new HashMap<>();
for (Map.Entry<String, Payload> entry : context.entrySet()) {
contextMap.put(entry.getKey(), DataConverter.getDefaultInstance()
.fromPayload(entry.getValue(), Object.class, Object.class));
}
}
return contextMap;
}
- Tried implementing the Interceptor concept as well, but they also have access to WorkflowInput/ActivityInput. What is the recommended way to pass certain context-related information via Interceptors?
- WorkflowInput/ActivityInput has header information in it. Can we leverage this header to somehow pass our information? Need to understand how is the header populated here.
It would be great if we can get some samples around this.
Thanks in advance !!!.