Running CometD 2 examples – locally on Tomcat using Maven and NetBeans

3

Push technologies is an area of interest to me. I have been looking into frameworks, servers and libraries such as Atmosphere, ADF ADS, CometD 2, Kaazing, jWebSockets, Grizzly and others. In my experience, even though there is a lot of information to be found through Google, it is not simple to quickly gain a good understanding of what all these frameworks do. Part of the challenge is that articles written less than two years ago can already be completely outdated because of the rapid evolution of the software and even the concepts behind them. A striking example is the quick series of changes that the WebSocket specification underwent, leaving servers and clients gasping for breath in its wake, as well as a diverse series of non-compatible implementations of the various stages of the specification.

Other challenges include the lack of explanation of the inner workings of samples and the core of the framework (assuming existing knowledge that I frequently do not have), the sparse instructions when it comes to installation and configuration, the lack of originality in terms of examples (Echo Server, Chat application), the limited documentation – not seldomly out of date with the latest state of the software and the poorly moderated forums.

The very least I want to do in order to starting evaluating any technology is to get something running. And that is where at least there seems to be a convergence in approaches. Maven is at the heart for installing and running samples for most frameworks these days, sometimes complemented with Git to download the sources in the first place.

This article very briefly demonstrates the steps for getting some of the CometD 2 samples up running on my local machine.

First, download a recent distribution for CometD 2, from its download site http://download.cometd.org/.

Image

Save the file locally and extract its contents, to c:\java:

Image

The pom.xml in the root directory for CometD2 is a hint of how exactly the library and its samples can be built: using Maven. In order to proceed, Maven needs to have been set up locally. In a previous article – http://technology.amis.nl/blog/14676/preparing-your-environment-for-modern-open-source-java-libraries-and-frameworks-using-git-and-maven-throwing-in-tomcat-as-a-bonus – I provide the bare minimum instruction for getting Maven up and running.

Samples for CometD2 can be found in various places in the distribution. One obviously is in the cometd-demo directory. Another one for example in cometd-java\cometd-java-examples.

To build installable WAR files to actually run samples, you need to have Maven execute the build phase from the directory. For example: open the command line prompt in C:\java\cometd-2.3.1\cometd-demo and execute mvn clean install.

This will build – and download a lot of files to the local Maven Repository – the CometD2 Demos as well as all of its dependencies (through the nested parent pom hierarchy).

Image

Once the build completes successfully, a WAR file will have been created in the target directory:

Image

Image

In order to run the CometD 2 samples, we simply have to deploy the WAR file on for example Tomcat. Run the Tomcat Server and open the administration page: http://localhost:8085/manager/html.Image

Click on the browse button, select the WAR file that was just created and deploy it:

Image

Even though deployment seems successful, the application cannot be started, due to a class not found exception (ClassNotFoundException org.eclipse.jetty.servlets.CrossOriginFilter):

Image

That is both annoying and hard to understand: the WAR file contains the Jetty Servlets JAR file (in the WEB-INF\lib directory) that contains the CrossOriginFilter class in the right package. So why can it not be found? And why does Google not give me a straightforward answer for this one?

In all fairness – I have not been able to answer these questions. However, I have found a workaround – using NetBeans. The steps in this workaround:

1. Extract the WAR file to a directory (C:\Temp\cometWAR). After exploding the WAR is done, create directory webapp under cometWAR. Copy all other subdirectories under cometWAR to this webapp directory.

2. Open NetBeans. Create a new project using the wizard. Choose the option to create a Java Web project from existing sources.

Image

Press Next.

3. Select c:\temp\cometWAR as the project location:

Image

Optionally change the Project Name. Press Next. The other defaults are probably ok. Press Finish. Ignore the dialog on the build directory (at least I did and that worked for me).

4. NetBeans will now create and open a new project, based on the exploded WAR file (that was the result of the Maven build).

Image

 

5. Associate the project with the Tomcat 7 Server – from the Properties dialog for the project:

Image

and

Image

Press OK to close the dialog.

6. Deploy the application to Tomcat:

Image

After deployment, the application can be seen as normally, successfully deployed and healthily running:

Image

8. From the Tomcat Administration console, the application can be seen and launched:

Image

and finally, it is running:

Image

For example the Chat demo:

Image

Share.

About Author

Lucas Jellema, active in IT (and with Oracle) since 1994. Oracle ACE Director for Fusion Middleware. Consultant, trainer and instructor on diverse areas including Oracle Database (SQL & PLSQL), Service Oriented Architecture, BPM, ADF, Java in various shapes and forms and many other things. Author of the Oracle Press book: Oracle SOA Suite 11g Handbook. Frequent presenter on conferences such as JavaOne, Oracle OpenWorld, ODTUG Kaleidoscope, Devoxx and OBUG. Presenter for Oracle University Celebrity specials.

3 Comments

  1. Pingback: Publishing to CometD Bayeux Channel from inside the Oracle Database – PL/SQL based push to CometD Web Client « AMIS Technology blog

  2. Hi Lucas,
    If you type ‘mvn jetty:run’ inside the cometd-demo directory maven will start jetty with the demo’s project. Should be able to see them on localhost:8080. Saves you installing tomcat and netbeans and some clicking.
    Cheers

    • Hi Alan,

      Thanks for this tip. I was aware of that – I just wanted to see whether I could get the demos running on a fresh environment. I am more interested in learning how to leverage CometD in my own web applications, running on generic Servlet Containers and not being dependent on Jetty.

      I hope all is well with you! Best wishes for 2012.

      Lucas

Leave a Reply