Diógenes
Diógenes

Reputation: 11

Cant Schedule Job ErpConfigContext

i'm trying Schedule a job with Quartz and Cant Schedule ErpConfigContext , when i do a request, works fine.

But in scheduled job, this return a error.

//Request working

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    new ODataScheduledFetch().execute();
}

//Job class not working public class JobProductPricing implements Job {

@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
    // TODO Auto-generated method stub

    new ODataScheduledFetch().execute();

}

}

ODataScheduledFetch().execute(), do some thinks and call this method, and when execute query.execute(new ErpConfigContext()); return a error only on Schedule Job

private boolean tryRequestERP(ODataQuery query,ODataQueryResult[] queryResult) {
    boolean boReturn=false;
    try {
        //queryResult 
        queryResult[0]  = query.execute(new ErpConfigContext());
        boReturn = true;
    }catch(Exception e) {
        String error = e.toString();
        System.out.println(error);
        boReturn = false;

    }

    return boReturn;
}

And received this error:

[ com.sap.cloud.sdk.cloudplatform.connectivity.exception.DestinationAccessException: Failed to get ConnectivityConfiguration: no RequestContext available. Have you correctly configured a RequestContextServletFilter or have you wrapped your logic in a RequestContextExecutor when executing background tasks that are not triggered by a request? ]

on this Creating ErpConfigContext threw exception

has a answer that i dont get...

" EDIT: Note that when running your code in a background job (not triggered by a request), you have to wrap your code with RequestContextExecutor::execute.

"

Upvotes: 1

Views: 137

Answers (1)

Christoph Schubert
Christoph Schubert

Reputation: 1124

Looking at your code sample I assume you are not using Spring Boot, which is the focus of the linked question.

What you need to do is wrapping your scheduled call into a RequestContextExecutor::execute call. For your example it would look like this:

@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
    new RequestContextExecutor().execute(() -> {
        new ODataScheduledFetch().execute();
    });
}

Depending on what you do inside your ODataScheduledFetch::execute call you might need to use the JwtBasedRequestContextExecutor (on CloudFoundry only).


For a bit mor background:

All *Accessor classes (e.g. TenantAccessor, DestinationAccessor) rely on a so called RequestContext to be available in the current Thread/Request. Such a RequestContext is created by the RequestContextExecutor as described above.

To make your life easier in the usual use-case (which is on receiving Requests) we automatically load the RequestContextServletFilter which wraps all incoming Requests for you.

That is the reason why your code works in a Request but not scheduled.

Upvotes: 1

Related Questions