Writing a bundle-based server application

Building server-based applications with Equinox is getting easier. The Equinox project’s server team has created a short tutorial that describes how to get a servlet up and running using a handful of bundles on the Equinox framework. The best part is that everything you need to run the tutorial is already included in the Ganymede M5 release of Eclipse for RCP/Plug-in Developers (previously, you had to load a bunch of prerequisite bundles). The tutorial is a little short on details, so I thought I’d take a stab at expanding on them a bit.

The first step (after downloading and installing Eclipse for RCP/Plug-in Developers) is to create a new Plug-in Project (File > New > Project > Plug-in Project). Next, navigate to the “Dependencies” page in the new plug-in’s Manifest Editor and add the org.eclipse.equinox.http.registry and javax.servlet bundles as “Required Plug-ins”. Next, navigate to the “Extensions” page and add an extension to org.eclipse.equinox.http.registry.servlets. Be sure to pick a suitable class name and alias (the alias is what you’ll use to access the servlet; e.g. for http://localhost/hello, the alias is “/hello”).

manifest.png

Clicking on the “class*:” label will open the “New Class” wizard:

Here, change the superclass to javax.servlet.http.HttpServlet, remove the suggested interface (javax.servlet.Servlet is implemented by javax.servlet.http.HttpServlet anyway), and click “Finish”. In the body of the resulting class, use code-completion (CTRL+1) to override the doGet method.

servlet-code.png

All that’s left is to run it. Right click on the bundle in the Package Explorer, and select “Run As > OSGi Framework”. Use a browser to navigate to your servlet; the URL will look something like “http://localhost/alias” (replace “/alias” with the alias you used when you created the extension).

The first time I ran the results of the tutorial, the server wouldn’t start. Diagnosing the problem can be a challenge if you don’t know where the log is. When you run an Equinox application, the logs end up in *.log files in the [workspace]/.metadata/.plugins/org.eclipse.pde.core/OSGi Framework directory. You can also add the -consoleLog switch in the “Program Arguments” of your launch configuration which will cause logging information to be dumped onto the OSGi console.

It turns out that the port that the HTTP Server wanted to use, 80, was already in use. I opted to use a different port. There are several VM arguments that you can use to tune the settings of the HTTP Server, in particular
-Dorg.eclipse.equinox.http.jetty.http.port=8080
specifies the port on which to run. Adding this entry to my launch configuration got everything up and running on port 8080, requiring me to change the URL in my browser to “http://localhost:8080/alias”.

launch.png

I’m planning to record these steps a short demo that I’ll post on Eclipse Live. I’m also planning to write this up a little better as an Eclipse Corner Article. In the meantime, comments, criticisms, and concerns are invited.

This entry was posted in Uncategorized. Bookmark the permalink.

10 Responses to Writing a bundle-based server application

  1. Jacek Pospychala says:

    thanks Wayne, looks exactly what I was talking about in http://dev.eclipse.org/newslists/news.eclipse.technology.eep/msg00010.html
    can’t wait to see the article.

    I wonder what are your thoughts on the rest of ideas posted in that email, too bad EEP went to review before taking discussion.

  2. Wassim Melhem says:

    There is a solid operating system out of the state of Washington called Windows. You should try it.

  3. Patrick Roumanoff says:

    That’s fantastic to be able to leverage the servlet API from within a bundle. But what are the real life applications of an “OSGi server” outside serving local page to the help system?

    Server side java has been around for a long time and we have come to expect scalability (eg clustering) and High Availability almost out of the box.

    After being exposed to OSGi on the client side, I am really willing to take it to the server side, but without features such as clustering, I am going to have a hard time explaining to those ops guys why this new technology is so much better than [insert your favorite app server with clustering capability].

  4. Wayne Beaton says:

    Wassim, I could just as easily have been running Apache HTTP Server on Windows. Come to think of it, last time I had this sort of problem on Windows, the problem was that Windows didn’t seem to think it was a problem and let me reuse the port anyway. Hilarity ensued.

    Patrick, the Jetty engine used by Eclipse is a real servlet engine that’s used in lots of different places. Likewise, there are a lot of people doing interesting server stuff with Equinox. I’ve only just started looking into this space, and I’m not aware of any specific clustering solutions that exist yet. OSGi server is still relatively young, so this is something that will likely develop with time. That said, not everybody needs clustering, HA, failover, etc. in their solutions. If you do, you probably still (at least today) want the weight and resources of a commercial entity and product behind you.

    As I dig further into this, hopefully I will get some interesting stories to tell…

  5. Mike Francis says:

    Wayne/Patrick

    If you are looking for OSGi server side clustering/resilience, scale out, etc you should take a look at the Newton project (www.codecauldron.org) and the commercial distributed OSGi runtime product called Infiniflow (www.paremus.com) which builds on top of Newton. There is transparent support for Spring Dynamic Modules too.

    We will be at ElipseCon in the exhibition area and we have a couple of talks and are involved in a BOF.

    Regards
    Mike (Paremus)

  6. Alex Blewitt says:

    Wayne,

    You might want to re-title this as ‘web server’ rather than ‘server’. After all, plenty of other servers exist in an OSGi environment other than those that speak HTTP. You might also want to touch base with Simon and mention the same thing – after all, server-side is a location, not a prescription.

    Alex

  7. Pingback: OSGi server side and servlets « Codecurl - David Black

  8. Wayne Beaton says:

    Hey Alex. You’re quite right that server-based != servlet-based. However, strictly speaking, the title is totally correct; the entry talks about building *a* bundle-based server, and an HTTP server certainly qualifies…

  9. Stuart McCulloch says:

    FYI, you should be able to set the port using the “org.osgi.service.http.port” property, which is standard across all HttpService implementations. You might also like to check out some of the extended HttpService features we’re researching over at OPS4J:

    http://wiki.ops4j.org/confluence/x/AYAz (Pax-Web)
    http://wiki.ops4j.org/confluence/x/OQJN (Pax-Web extender)
    http://wiki.ops4j.org/confluence/x/oANN (WAR files with Pax-Web)

    The last link shows how to deploy the Spring “petclinic” example WAR onto OSGi.
    We also have an Eclipse plug-in that lets you deploy profiles, such as WAR support:

    http://wiki.ops4j.org/confluence/x/0QBN

    Development of this is very open (using wiki concepts) so feel free to join in🙂

  10. Pingback: Blog bookmarks 03/02/2008 « My Diigo bookmarks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s