CometD 2 Java Client Sample – open project in NetBeans based on Maven pom file, modify sources and run Java Based Comet Client

On the heels of previous articles that described setting up NetBeans (https://technology.amis.nl/blog/14661/first-experiences-and-getting-started-with-glassfish-3-1-and-netbeans-7-x-its-a-breeze), Tomcat 7 (https://technology.amis.nl/blog/14688/installing-tomcat-7-and-configuring-as-server-in-netbeans) and the CometD 2 environment (https://technology.amis.nl/blog/14709/running-cometd-2-examples-locally-on-tomcat-using-maven-and-netbeans), this article describes how I opened the Java Client Examples from the CometD project in NetBeans (creating a NetBeans project based on the Maven pom.xml included in the CometD distribution), made a few changes to the source and ran the sample from within NetBeans. Within minutes, the sample was running and ready for further manipulation.

The option to create a NetBeans project based on a pom.xml is very useful. The overall speed and responsiveness of NetBeans is also very pleasant.

The steps:

Start NetBeans.

From the File menu, pick the option Open project.

Image

 

Navigate to the directory where CometD was installed. Browse to [C:\java\]cometd-2.3.1\cometd-java. Notice how NetBeans recognizes and interprets the pom files.

Image

Select the cometd-java-examples directory/maven based project. Press Open project.

The project is now opened in NetBeans. All sources, configuration files and dependencies ready for inspection and manipulation.

Image

To see one of the samples in action – the Java Chat Client that will communicate in a Comet style fashion with users of the Web Based Chat Client – open the source file ConsoleChatClient.java in package org.cometd.examples.

Before we can meaningfully run the ConsoleChatClient, better first run Tomcat. Then check whether the chat demo is available – as well as the port number on which the Tomcat server is to be accessed:

Image

Here we learn that the Tomcat server is listening on port 8085 (configured in the Tomcat server.xml file) and that the chat application is accessible through the cometSamplesFromWAR context path.

Now open the ConsoleChatClient. Locate the line where the defaultURL is set and use http://host:port/applicationContextPath/cometd as the default URL. In my case that means: “http://localhost:8085/cometSamplesFromWAR/cometd

Image

Now we can run this class – through its main main method. The class will register a listener on the Chat channel. The initial messages seem a little unnerving, but it all turns out well:

Image

 

In the browser user Nick will see the Java Client entering the chat:

Image

After Nick types a welcome message, the console in NetBeans will read:

Image

Now respond from the console in NetBeans:

Image

And the response arrives promptly in the browser window:

Image

 

With the sources set up in NetBeans, nothing will stop us from further investigating the options available in the CometD framework.