Hello ,
I have a scheduler that will triggered the service and I have my whole logic there. But, I need to get the request object in the service, is there a way to achieve it?
Note: I shouldn't use servlet to pass the request object as a parameter.
please your suggestions.
Thanks,
Vijay
Solved! Go to Solution.
Topics help categorize Community content and increase your ability to discover relevant content.
Views
Replies
Total Likes
I suggest reviewing the entire design. Using a request object for a scheduler or events doesn't make sense since these are generally not triggered by any request.
However, you can create a HttpServletRequest orMockSlingHttpServletRequest in your OSGi service to simulate a request object.
You might find this example helpful: ServletModel example.
We faced a similar use case where a request object was needed when running a scheduler due to a third-party method requirement. We used `MockSlingHttpServletRequest` to handle this.
If you could provide some more details, that would be helpful.
But you would probably have to rethink this a little. A scheduler is something which is designed to run at specific intervals. And request/response process is happening in real time. So, accessing some request object in the scheduler will only make sense if you are trying to get some particular information from a particular request itself, in which case scheduler is probably not the right fit for your use case.
Since you already have a service, you can think if leveraging Sling model or servlets can fullfill your requirements.
Hope this helps.
Thanks your your suggestion. But my requirement is,
I need to create/update a xml file by fetching the data from another external end point and that should happen once in a day.
so, to call the endpoint I need a request object.
I know the ideal way to have the servlet, but I don't want to go with the servlet as anyone can hit it by path.Also, I don't think that i can use the model here as we can't call the model from an scheduler, so I decided to go with service.
Is there any other way to achieve the requirement? Thanks.
You can use apache httpclient in that case if you are just looking to make the external api call
HttpGet httpGet = new HttpGet(apiUrl);
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
CloseableHttpResponse response = httpClient.execute(httpGet);
String responseStr = EntityUtils.toString(response.getEntity());
return responseStr;
}
You can find more samples here https://sourcedcode.com/blog/aem/handling-http-requests-httpclient-in-aem
I suggest reviewing the entire design. Using a request object for a scheduler or events doesn't make sense since these are generally not triggered by any request.
However, you can create a HttpServletRequest orMockSlingHttpServletRequest in your OSGi service to simulate a request object.
You might find this example helpful: ServletModel example.
We faced a similar use case where a request object was needed when running a scheduler due to a third-party method requirement. We used `MockSlingHttpServletRequest` to handle this.
Probably what you need is a system user to be able to write your xml into repository?
As @arunpatidar said, it does not make sense to have a request when running a scheduler.
A scheduled job runs outside of a request context, for that reason there is no SlingHttpServletRequest available.
@vijayselvas1 Did you find the suggestions from users helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you!
Views
Replies
Total Likes
Views
Likes
Replies