1

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.

Mikhail Chibel
  • 1,865
  • 1
  • 22
  • 34
  • 1
    This is similar to, though not the same as a question I asked: http://stackoverflow.com/questions/29380888/how-to-check-for-request-scope-availability-in-spring My question was spring specific, but I link to it here in the hopes it may help. – Taylor May 19 '17 at 00:22
  • @Taylor indeed it similar. And unfortunately pure JEE not so convenient as Spring. My current workaround is to use try catch. Actully I don't think there is any proper solution for that. I can figure out if thread started by Quartz based name of thread group but it is far from ideal. – Mikhail Chibel May 19 '17 at 02:07

1 Answers1

0
public class WorkOrderList1Action extends Action{
    
    protected ArrayList getreceipt1() {
        ArrayList FIRSTNAME=new ArrayList();
        return FIRSTNAME;           
    }
}
8bit
  • 528
  • 2
  • 6
  • 25
  • Can you add an explanation of your code? – 8bit Jul 10 '23 at 16:06
  • Your code contains a method that returns an empty `ArrayList`. How does that answer the original question? The method in your answer also has `protected` access. Does that imply that it should be overridden by a subclass of `WorkOrderList1Action`? Or maybe you just made a mistake and answered the wrong question? Also note that answers which contain only code are generally not considered good answers. – Abra Jul 11 '23 at 06:08