0

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.

Community
  • 1
  • 1
nipuna-g
  • 6,252
  • 3
  • 31
  • 48

1 Answers1

-1

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

redge
  • 1,182
  • 7
  • 6
  • Will CDI work with jetty? If not, would using another DI framework such as Guice work? – nipuna-g Jun 10 '15 at 23:32
  • You should be able to get Weld working with jetty using the following http://www.eclipse.org/jetty/documentation/current/framework-weld.html It may be possible to get jetty to use Guice for injection into executors but I am less familiar with guice. – redge Jun 11 '15 at 10:36