I have a code which may be executed from normal servlet thread or from the Quartz job. The code is basically error logger and I want to add request information if available to the log message. The problem is when the code executed from the scheduled task there is obviously no request available, which is fine. The request information comes from injected instance (CDI) which underneath uses injected instance of servlet request.
@Inject
private HttpServletRequest servletRequest;
Attempt to inject HttpServletRequest from the thread running outside of servlet results in org.jboss.weld.exceptions.IllegalStateException: WELD-000710: Cannot inject HttpServletRequest outside of a Servlet request
My question how can I detect if the current thread running in the servlet context or not and avoid trying to inject the request object.