0

I have a case where I need to use Scheduled. This schedule will execute this method every 30 min, so the method is

    @Transactional
    public void processEnable()
            throws Exception
    {
        try
        {

        }
        catch (Exception e)
        {
            logger.warn("[Enable] [STATUS] - ERROR  ");
            logger.warn("[Enable] [EXCEPTION] " + e.getMessage(), e);
            throw e;
        }
    }

and I want to get this method to execute for 30 min

and i am doing this with that.

@Scheduled(cron = "0 0 1-2 * * *")
public void Enable()
{
    try
    {
        Service.Enable();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

but the problem is how to get the parameter here, is my first time, in fact, using @Scheduled and I don't know how it works or how can I get the parameter from the user so I can get this method works each 30 min.

I am having no idea to get the parameter I want.

Ilia Tapia
  • 629
  • 3
  • 10
  • 23
  • So, you want to call `processEnableApp` method after you receive those parameters, don't you? Plus, is it gonna be done for 1 user or multiple? Because you only have 1 scheduler, I think. – Mansur Jul 10 '19 at 12:59
  • is an app for multiple users who can have 1 or multiple apps, that's why I need the parameters to get the correct app in this case – Ilia Tapia Jul 10 '19 at 13:01
  • You can actually, autowire the Scheduler to the class you need. Move those parameters to the `processEnableApp` method, and call the scheduler whenever you want. However, not sure, if the scheduling will be kicked in in that case. – Mansur Jul 10 '19 at 13:03
  • Thanks, I appreciate your comments a lot @MensurQulami but I mean still, I don't get it how will I get parameters, I am used to getting them from API that is being called, Sorry but still doesn't understand this. – Ilia Tapia Jul 10 '19 at 13:08
  • Don't you understand getting parameters for Scheduler or getting parameters in general? – Mansur Jul 10 '19 at 13:11
  • No no, just getting parameters from Scheduler, I meant by API to specific that I get them from frontend when a user does something, but as Scheduler is an automatic process I don't know how to get them – Ilia Tapia Jul 10 '19 at 13:14
  • And what I suggest is trying to trigger the Scheduler manually. You can autowire it. Then change the method to `Scheduler.processEnableApp(Integer idLang, Integer idApp, Authentication userAuthenticated)`. Now you can pass the parameters. All you need to do is to call it from the class you need. But as I said, I am not sure if it will 100% work. – Mansur Jul 10 '19 at 13:18
  • 1
    I mean I want a Scheduler cause I don't want to go at the page and do it manually, in this case, I can just add an extra API, for example, and call it each time the page is reloaded but I want to use Scheduler – Ilia Tapia Jul 10 '19 at 13:21

3 Answers3

0

If you want the external objects to be used within your @Scheduled methods, you should inject them into the DemoService class using autowiring rather than passing them as parameters to the @Scheduled methods.

Abd Abughazaleh
  • 4,615
  • 3
  • 44
  • 53
0

In your Scheduled method:

  1. you need to set the user executing the code to get spring security annotations satisfied. So using a system user do:
SecurityContext securityContext= SecurityContextHolder.getContext();
securityContext.setAuthentication(auth);
  1. get all the authenticated users at the moment the job starts running. See this answer
List<Object> principals = sessionRegistry.getAllPrincipals();
  1. iterate the list of Authentication objects from the previous step and call processEnableApp (passing the parameters to the method)
wi2ard
  • 1,471
  • 13
  • 24
0

Your cron expression is wrong. You need this */30 * * * *

Ali Ben Zarrouk
  • 1,891
  • 16
  • 24