views:

39

answers:

1

Hi Everyone,

I'm thinking about developing a Java Web Service using RESTEasy. I am going to follow this example: http://technicalmumbojumbo.wordpress.com/2009/10/13/restful-webservices-using-jboss-resteasy-tutorial/

Now, that's very well and good for getting the server to reply back with a simple response, however how do I get some "backend" things going on? For example, I want a queuing system running in the background constantly on the server, processing objects in the queue. When someone accesses a certain URL (The web service), i want the web service to Enqueue something..

The only thing I can think of in my head at the minute, which is probably totally wrong, is to make a seperate Java (J2SE) application, which runs the Queue, and connect the Web Service to it via RMI or Cajo or something..

I guess what I'm asking is that does a Java Web App running on Tomcat have any concept of a "main() method" which gets executed on server startup?

Any help is appreciated

Thanks

+1  A: 

I guess what I'm asking is that does a Java Web App running on Tomcat have any concept of a "main() method" which gets executed on server startup?

You can have a servlet get kick started on the load of the application using the appropriate load-on-startup tag in the web.xml file.

So for example:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">

<!-- snip stuff -->

    <servlet>
        <servlet-name>context</servlet-name>
        <servlet-class>org.lastname.firstname.YourStartupServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

<!-- snip stuff -->

</web-app>
karianna
Can I please have an example of this? I'm sorry I'm very new to all of this.
jtnire
Edited the answer, hope that helps!
karianna
Thanks. And I'm guessing it's the init() method that gets called from the YourStartupServlet class then? From there, I can create a thread which makes the queue, and starts processing?
jtnire
Correct on the init() part. However, spawning a thread however isn't always considered to be best practice. You could look at Message Driven Beans, JMS and other such technologies to perform your asynchronous work.
karianna