views:

148

answers:

2

I've got a stand alone, headless, java server app that does a bunch of queue-based processing against a database that I'm thinking of migrating into a java app server. I've got plenty of back-end java experience and a bit of JSP, but not a lot of servlet experience.

It seems like the approach would be to just wrap my app into a servlet and have it deploy on startup (and make sure that it only one instance gets deployed).

Few questions:

1) Since my app doesn't have any HTTP (or other) request/response mechanism, would it be silly to implement a servlet that has no URL mappings? Looking at the API, would I just implement a GenericServlet and just leave the service() method empty?

2) Another portion of my java app opens/manages it's own network sockets (non-HTTP) to accept a stream of incoming data. I think it would take quite a bit of work to get it to fit into the servlet request/response model. Is it ok that a servlet opens/manages its own network sockets?

3) We also have a bunch of web apps (currently in coldfusion) that are not very well integrated with the java app (in that they can only communicate via the DB). We are looking at railo (another servlet) and I'm trying to figure out how easy it would be for the coldfusion/railo apps (running in the same app server) to communicate directly with each other. Maybe a web page that displays current runtime stats/metrics of the java engine, and eventually invoking some of the business logic in the java engine as well.

Thanks, Brian

+1  A: 
  1. Servlets are a generic mechanism that are not specifically tied to the HTTP world (despites the fact that HttpServlets are used in 99.999% of cases). You could subclass the Servlet class to implement, say, a MailServlet that would respond to mail events - but as far as I know, current webservers support only HTTP mappings.

  2. Sockets belong to the Java EE world, and it is considered a Bad Thing to launch custom threads in this environment - and you most certainly would need to do that if you open sockets (for polling data, etc.)

Olivier Croisier
I definitely have a bunch of threads that I launch/manage. Is it really that bad to have a servlet spawn threads? I read that it has to be threadsafe itself, but never saw anything about spawning threads.
Brian
@Brian it is a generally frowned-upon practice; because a poorly managed thread launched by your servlet can prevent the servlet container/server from responding to shutdown commands. Look at using the Executor framework instead.
matt b
@Brian - Why spawning threads is considered bad -> http://stackoverflow.com/questions/533783/why-spawning-threads-in-j2ee-container-is-discouraged/533847#533847. In your case, it will negate your reasoning of managablity since you are doing things that the container cannot manage.
Robin
+2  A: 

If you don't want to intercept on HTTP requests, then just don't extend HttpServlet. This makes no sense. If you actually want to execute it as a "background task" on webapp's startup and stop it on webapp's shutdown, then just implement ServletContextListener accordingly.

public class Config implements ServletContextListener {

    private YourApp yourApp;

    public void contextInitialized(ServletContextEvent event) {
        yourApp = new YourApp();
        yourApp.start();
    }

    public void contextDestroyed(ServletContextEvent event) {
        yourApp.stop();
    }

}

which you can register in web.xml as follows:

<listener>
    <listener-class>com.example.Config</listener-class>
</listener>

If YourApp actually doesn't fire the task in a separate thread, then you'll need to wrap it up in a Runnable and execute it using ExecutorService. E.g.

public class Config implements ServletContextListener {

    private ExecutorService executor;

    public void contextInitialized(ServletContextEvent event) {
        executor = Executors.newSingleThreadExecutor();
        executor.submit(new YourApp()); // YourApp should implement Runnable.
    }

    public void contextDestroyed(ServletContextEvent event) {
        executor.shutdown();
    }

}

If, after all, your webapp does nothing else than that, then I question the value of running it in a servletcontainer. Instead rather just run it as a standalone Java application using the main() method.

BalusC
It does run standalone via main() currently. I'm evaling app server because: 1) manageability (especially if we can run it alongside the rest of our web apps, 2) letting our webapps integrate with the engine (question #3 in my original post)3) long term: exposing some of its internals as web-services.
Brian
also, how are application services exposed in app servers? Like the scheduler service in Coldfusion/Railo? Would they use the ServletContextListener mechanism?
Brian
Then just go ahead with the `ServletContextListener` approach, you'll only need to wrap it in a `Runnable`. I don't do Coldfusion/Railo, but I can imagine that they do similar thing. Else just crawl in their docs/sourcecode to be sure.
BalusC