javawaveblogs-20

Wednesday, November 14, 2012

Camel-Jetty Step by Step using Netbeans IDE


Camel-Jetty Step by Step using Netbeans IDE
1. Create a Maven Java project using Netbeans IDE (Version used - 7.3 beta2)
  1. Go to New project wizard and Create a new maven java project
  2. Here for our sample the project is named as “MyHTTP”
  1. Click on finish
  2. now the IDE will create a Java Maven project for you with the recommended folder structure like below.
  1. Just check the POM.XML found inside the “ProjectFiles” folder. which will have the dependencies and plugin’s added automatically by the IDE. POM created for “MyHTTP” is shown below.
  1. Now its time to add Camel dependencies to our POM.XML file. add the following dependencies to your pom.xml file inside the dependencies tag.
        

       






















       
  1. Once the dependencies are added just clean and build your project. so that maven will download the required jar files listed in the dependencies tag to your project from the central repository. and also will pack the required JAR files to your “\MyHTTP\target\” path with name “MyHTTP-1.0-SNAPSHOT.jar”.
  2. Now in your project you can see a main class created with the name “App.java”.
  3. its time to edit the java class App.java and add the following code to it.
package com.mm.myhttp;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
/**
 *
 * @author Muthu
 */
public class App {
    public static void main(String[] args) {
        DefaultCamelContext camelContext = new DefaultCamelContext();
        try {
            camelContext.addRoutes(new RouteBuilder() {
                @Override
                public void configure() throws Exception {
                    from("jetty:http://0.0.0.0/myapp/myservice/?sessionSupport=true")
                            .process(new Processor() {
                        @Override
                        public void process(Exchange exchng) throws Exception {
                            System.out.println("Inside process exchange");
                            String body = exchng.getIn().getBody(String.class);
                            // this is the way to access HttpServletRequest
                            HttpServletRequest req =
                                    exchng.getIn().getBody(HttpServletRequest.class);
                            // send a html response back to client
                            exchng
                                    .getOut()
                                    .setBody(""
                                    + "Simple Demo for Camel-Jetty component"
                                    + "
");
                        }
                    });
                }
            });
            camelContext.start();
        } catch (Exception ex) {
            Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

  1. the important part of the code is “from("jetty:http://0.0.0.0/myapp/myservice/?sessionSupport=true")” from route which starts jetty server and listens to the http url mentioned in the from route.
  2. then the route flows to the process where a html response is set to the body and returned to the client.
  3. To test this application, just run the Java Maven project with App.java as the main class.
  1. In the Output window you can see the http service running at port 80. and our component uses Jetty-7.5.4 to run the service.
  1. Now hit the url(http://localhost/myapp/myservice/) using your browser and you should be able to see the html response back from the service.

No comments:

diggthis