Running CometD 2 examples – locally on Tomcat using Maven and NetBeans
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/.
Save the file locally and extract its contents, to c:\java:
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).
Once the build completes successfully, a WAR file will have been created in the target directory:
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.
Click on the browse button, select the WAR file that was just created and deploy it:
Even though deployment seems successful, the application cannot be started, due to a class not found exception (ClassNotFoundException org.eclipse.jetty.servlets.CrossOriginFilter):
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.
3. Select c:\temp\cometWAR as the project location:
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).
5. Associate the project with the Tomcat 7 Server – from the Properties dialog for the project:
Press OK to close the dialog.
6. Deploy the application to Tomcat:
After deployment, the application can be seen as normally, successfully deployed and healthily running:
8. From the Tomcat Administration console, the application can be seen and launched:
and finally, it is running:
For example the Chat demo:
- Preparing your environment for modern open source Java libraries and frameworks using Git and Maven, throwing in Tomcat as a bonus
- Installing Tomcat 7 and Configuring as Server in NetBeans
- Combining Hibernate and Facelets with Maven, Netbeans and GlassFish
- Running a BPEL process created with Netbeans in Apache ODE
- Better Builds With Maven – Free book about Maven 2