Use Spring in a WAR

I did jar application with Spring 3 TaskScheduler. I run this app with main method:

public static void main(String[] args) {
  GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
  ctx.load("classpath:scheduler-app-context.xml");
  ctx.refresh();
  while (true) {
    // ...
  }
  // ...
}

Is it please possible run this jar, main method in web application(war file)? How to run this in web.xml.

Many thanks

Answers


Do something like this in your web.xml:

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:scheduler-app-context.xml</param-value>
</context-param>

<listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

This will instantiate your Spring context from the XML file. So it's not necessary to do this by hand as in your main method.


If you need simple scheduler in war(with spring framework), you can also do something like this:

(in Spring the "@PostConstruct" will initialize scheduler - so there is no need for main method)

    @Component
    public class Scheduler {

        private static final Logger LOG = LoggerFactory.getLogger(Scheduler.class);


        @PostConstruct
        private void initializeTenSecSchedule() {

            final List<Runnable> jobs = new ArrayList<Runnable>();

            jobs.add(doSomeTestLogs());
            jobs.add(doSomeTestLogs2());

            final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(jobs.size());

            for(Runnable job : jobs){

                scheduler.scheduleWithFixedDelay(job, 10, 10, SECONDS);

            }

        }

        /**
         * ---------------------some schedule tasks--------------------------
         */

        private Runnable doSomeTestLogs(){

            final Runnable job = new Runnable() {
                public void run() { 

                    LOG.debug("== foo SCHEDULE a", 1);
                    System.out.println("Method executed at every 10 seconds. Current time is :: "+ new Date());

                }
            };

            return job;

        }

        private Runnable doSomeTestLogs2(){

            final Runnable job = new Runnable() {
                public void run() { 

                    LOG.debug("== foo SCHEDULE b", 1);
                    System.out.println("Method executed at every 10 seconds. Current time is :: "+ new Date());

                }
            };

            return job;

        }

    }

Need Your Help

Cocos2d unsigned int showing up everywhere

cocos2d-iphone updates unsigned-integer

I'm not sure what I did - I was coding away merrily.. I think it might have something to do with updating Xcode to 4.1.1, but all of a sudden, when I build and run my project, the following compiler

Div with being bleed through with content underneath?

html css background z-index obscured-view

On this page, top-right, we can see the button that I've created "ask a question now". I set the background color, background image, and high Z-Index and yet, still the text underneath is being bled

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.