Announcement Announcement Module
No announcement yet.
How best to invoke Spring-enabled POJOs from a scheduler Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How best to invoke Spring-enabled POJOs from a scheduler

    I'm most of the way through reworking an EJB application into a Spring application. Some of the ex-ejbs need to be invoked by a scheduler rather than by a web controller, and I'd like to find out how people in the forum usually do this. (We may use Quartz scheduler in the future, but for now the scheduler is Web Methods.)

    When the app used to use ejbs, the scheduler had in its classpath a file that had the server, jndi-port, etc., and that also had username and password. This allowed the scheduler to invoke the ejbs under a particular role.

    That's the functionality I want to replicate. To avoid misunderstanding, let me mention that the problem is not how to protect my ex-ejbs against access by someone whose not in the correct role. I now use acegi rather than ejb-specific security, so that's taken care of.

    My problem is much more trivial. I'd like to deploy these scheduler-invoked POJOs in a way that lets the scheduler log in and then use them, and I'd like to do it in some sort of standard way.

    I thought briefly about using HttpInvoker, but the thing that strikes me is there's really no reason these beans need to be remote relative to the scheduler. I'd probably prefer that the scheduler simply grab the ApplicationContext locally, somehow log in, and get the desired bean.

    But enough of what I'd prefer. What would you do in this situation?

  • #2
    You use org.springframework.scheduling.timer.ScheduledTime rTask to launch a timerTask bean. The timerTask bean typically is an extension of java.util.TimerTaskm which provides a public void run(); method. You put your logic into run().

    As you're using Acegi Security, you can modify which username and password to run under quite easily. Try this code (0.8.2 specific, from 0.9.0 and current CVS you'll need to change the class name used for the ContextHolder as per the upgrade-080-090.txt file that will be in the root of the Acegi Security distribution):

    public void run() {
      Authentication auth = new UsernamePasswordAuthenticationToken(username, password);
      SecureContext sc = new SecureContextImpl();
      try {
        // I now have security identity, so run my protected bean
      } catch (Exception ignored) {}
      } finally {
    private void runCode() {
    HTH get you on the right track.


    • #3
      Thanks. Just what I need.