Starting a ScheduledExecutorService from a servlet with a set of parameters

525 views Asked by At

How can a background process that uses the ScheduledExecutorService be invoked from a call to a servlet?

I need to pass in some parameters for runnable method as well.

I'm currently using the following code to start a executor.

executor = Executors.newSingleThreadScheduledExecutor();
executor.scheduleAtFixedRate(new RefreshTask(), 0, 1, TimeUnit.SECONDS);

What I need to know is,

  1. How I can start the method manually via a call to a servlet.
  2. How to pass in some parameters(a URL) to the RefreshTask() method

A similar question was asked here: https://stackoverflow.com/a/25245786/3156644 But it's not clear how I should go about injecting the resources or start the process.

Also, I'm using a Jetty embedded server for this project.

1

There are 1 answers

2
redge On

You will need an injection framework in your project.

You should look at CDI which is part of the JavaEE platform. Or if you are familiar with it you could use Spring but I would recommend the CDI standard.

Weld (weld.cdi-spec.org) is the reference implementation for CDI so I would suggest that you start with that and have a look at the parts of the JavaEE tutorial that talk about CDI here