You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by zappee <ar...@gmail.com> on 2016/02/21 03:11:46 UTC

camel rest route

Hi,

Currently I am learning how to use Apache Camel Rest and I would like to ask
some questions about it. I would greatly appreciate any answer.
 
I have a rest camel route which is packed in a jar. This jar locates in an
ear file and deployed on GlassFish app. server.
*Q1:* I am not able to figure out how I can start/bootstrap this route.

I want to use localhost:port/api/user/generate url for my route. It
generates a simple json answer based on my java pojo with getters/setters.
*Q2:* How to configure the base url of my route OR figure out the correct
path of my deployed route?
*Q3:* How I can send back a json response based on a java object (object is
automatically convert to json by camel on the fly)? Is my code correct (i am
not able to test my code till it is not bootstrapped)?

My ear is deployed on server, stateless ejb service and web application are
working but my camel rest api is not available.

*Structure of my ear:*
*.EAR
 |-lib/
 |  |- dependencies, ex.: camel jars
 |  |- my-common.jar
 |  |- my-camel-restapi.jar <- it contains my camel rest route
 |
 |- my-web-ui.war
 |- my-stateless-ejb.jar


*Camel route java:*
public class UserServiceRouteBuilder extends RouteBuilder {
    private static final String APPLICATION_JSON_UTF_8 =
"application/json;charset=UTF-8";

    @Override
    public void configure() throws Exception {
        restConfiguration().component("jetty")
                .bindingMode(RestBindingMode.json)
                .dataFormatProperty("prettyPrint", "true");

        User user = new User();
        user.setEmail("abc@gmail.com");
        user.setFirstName("John");
        user.setLastName("Connor");

        rest("/user")
                .description("user rest service")
                .produces(APPLICATION_JSON_UTF_8)
               
.get("/generate").route().routeId("GET@/generate").transform().constant(user).endRest();
    }
}





--
View this message in context: http://camel.465427.n5.nabble.com/camel-rest-route-tp5777992.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: camel rest route

Posted by zappee <ar...@gmail.com>.
Hi,

Thx. for the reply. As you suggested the solution is use processor.





--
View this message in context: http://camel.465427.n5.nabble.com/camel-rest-route-tp5777992p5779126.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: camel rest route

Posted by "Joseph E. Kampf Jr." <jo...@gmail.com>.
You configure method is not called every time your route executed. Only once to create the route. If you want some code executed each time to create a new user object, create a processor that executes your user create code and puts the user object in the body of the message. 

Thanks,

Joe

> On Feb 23, 2016, at 11:23 AM, zappee <ar...@gmail.com> wrote:
> 
> Hi,
> 
> Thx for the reply.
> 
> Now I am facing with another strange behavior. I want to fill in my user
> object with random generated data, like this:
> 
> @Override 
> public void configure() throws Exception { 
>    User user = new User(); 
>    user.setFirstName(*Generator.randomFirstName()*);
>    user.setLastName(*Generator.randomLastName()*);
>    *user.setBirthday(new Date());*
> 
>    restConfiguration().component(*"restlet"*) 
>    *.host("localhost").port(8081)* 
>            .bindingMode(RestBindingMode.json) 
>            .dataFormatProperty("prettyPrint", "true"); 
> 
>    rest("/user") 
>            .description("user rest service") 
>            .produces(APPLICATION_JSON_UTF_8) 
>            .get("/hello")
> .outType(User.class).route().routeId("GET@/generate").transform().constant(user).endRest(); 
> }
> 
> 
> But I always get the same values in all fields. For example the birthday
> field is fill in with the current timestamp value during the first rest call
> and same value is displayed later in the next calls:
> 
> First response:
> {
> "id": null,
> "email": null,
> "firstName": "Lindsey",
> "lastName": "Craft",
> "birthday": 1456243732376
> }
> 
> I get the exactly same response if i call my rest in the next 5 mins. How it
> is possible?
> 
> 
> 
> 
> 
> --
> View this message in context: http://camel.465427.n5.nabble.com/camel-rest-route-tp5777992p5778136.html
> Sent from the Camel - Users mailing list archive at Nabble.com.

Re: camel rest route

Posted by zappee <ar...@gmail.com>.
Hi,

Thx for the reply.

Now I am facing with another strange behavior. I want to fill in my user
object with random generated data, like this:

@Override 
public void configure() throws Exception { 
    User user = new User(); 
    user.setFirstName(*Generator.randomFirstName()*);
    user.setLastName(*Generator.randomLastName()*);
    *user.setBirthday(new Date());*

    restConfiguration().component(*"restlet"*) 
    *.host("localhost").port(8081)* 
            .bindingMode(RestBindingMode.json) 
            .dataFormatProperty("prettyPrint", "true"); 
 
    rest("/user") 
            .description("user rest service") 
            .produces(APPLICATION_JSON_UTF_8) 
            .get("/hello")
.outType(User.class).route().routeId("GET@/generate").transform().constant(user).endRest(); 
}


But I always get the same values in all fields. For example the birthday
field is fill in with the current timestamp value during the first rest call
and same value is displayed later in the next calls:

First response:
{
"id": null,
"email": null,
"firstName": "Lindsey",
"lastName": "Craft",
"birthday": 1456243732376
}

I get the exactly same response if i call my rest in the next 5 mins. How it
is possible?





--
View this message in context: http://camel.465427.n5.nabble.com/camel-rest-route-tp5777992p5778136.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: camel rest route

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

You are using restlet so it cannot reuse ports accross other
deployments in the same JVM. For that you need to use the servlet
component which uses the servlet engine of your app server.

That requires a bit more work to setup, eg in the web.xml etc.

For the api, then you can use swagger, see
http://camel.apache.org/swagger-java

There is some examples that does that (which also uses as servlet)
https://github.com/apache/camel/tree/master/examples/camel-example-swagger-xml



On Sun, Feb 21, 2016 at 4:49 PM, zappee <ar...@gmail.com> wrote:
> Hi Claus,
>
> Thank you for the links. Especially for the first one.
> I do not use Spring, instead of I use simple CDI and my camel route is not
> packaged into war so I do not have any web.xml file.
>
> I've just followed your links and I was able to create a bootstrap java
> class:
>
> @Singleton
> @Startup
> public class Bootstrap {
>
>     private static final Logger LOGGER =
> LoggerFactory.getLogger(Bootstrap.class.getName());
>
>     @Inject
>     private CamelContext context;
>
>     @PostConstruct
>     public void init() {
>         try {
>             LOGGER.info("bootstrapping apache camel");
>             context.addRoutes(new UserServiceRouteBuilder());
>             context.start();
>         } catch (Exception e) {
>             LOGGER.error("an error occurred during bootstrapping camel
> routes", e);
>         }
>     }
>
>     @PreDestroy
>     public void shutdown() {
>         try {
>             context.stop();
>             LOGGER.info("apache camel routes stopped");
>         } catch (Exception e) {
>             LOGGER.error("camel stop error", e);
>         }
>     }
> }
>
>
> I had to move my-camel-restapi.jar from the lib folder to the root of the
> ear file because I got a strange exception. Now the structure of my ear
> looks like this:
>
> *.EAR
>  |-lib/
>  |  |- dependencies, ex.: camel jars
>  |  |- my-common.jar
>  |
>  |- my-web-ui.war
>  |- my-stateless-ejb.jar
>  |- *my-camel-restapi.jar <- it contains my camel rest route*
>
>
> And my camel rote is improved a little bit:
>
> public class UserServiceRouteBuilder extends RouteBuilder {
>     private static final String APPLICATION_JSON_UTF_8 =
> "application/json;charset=UTF-8";
>
>     @Override
>     public void configure() throws Exception {
>         User user = new User();
>         user.set...
>
>         restConfiguration().component(*"restlet"*)
>         *.host("localhost").port(8081)*
>                 .bindingMode(RestBindingMode.json)
>                 .dataFormatProperty("prettyPrint", "true");
>
>         rest("/user")
>                 .description("user rest service")
>                 .produces(APPLICATION_JSON_UTF_8)
>                 .get("/hello")
>
> .outType(User.class).route().routeId("GET@/generate").transform().constant(user).endRest();
>     }
> }
>
> It works fine, if I make a GET call to http://localhost:8081/user/hello then
> I get a proper json answer. Beautiful :) I like Camel, it is a great java
> integration tool/framework. It makes Java stronger :)
>
> But I do not understand something. How it is possible to map the restapi to
> http port 8081? It means for me that somehow camel starts a small web
> container or whatever else on port 8081 inside my GlassFish server. What
> happened inside the EE container of the GlassFish?
>
> If I have more, separated camel group of routes packages in different,
> separated jars, bootstrapped them separately then I need to use different
> ports during the bootstrapping process to avoid the "port in use" error
> message. Am I correct? Or I can configure separated routes to use the same
> host and port (of course paths need to be different)? How many MB memory is
> consumed by my separated jar files with the small "web containers", listen
> on different ports?
>
> Can I bootstrap my routes - locate in different jars - by a common
> Bootstrap.java from example my-commons.jar? In that case theoretically I
> need only one "something" which is listening on ex. port 8081 and all my
> routes (rest api) will be available on the same port.
>
> Thx!
>
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/camel-rest-route-tp5777992p5778013.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2: https://www.manning.com/ibsen2

Re: camel rest route

Posted by zappee <ar...@gmail.com>.
Is it a servlet?



--
View this message in context: http://camel.465427.n5.nabble.com/camel-rest-route-tp5777992p5778020.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: camel rest route

Posted by zappee <ar...@gmail.com>.
Hi Claus,

Thank you for the links. Especially for the first one. 
I do not use Spring, instead of I use simple CDI and my camel route is not
packaged into war so I do not have any web.xml file.

I've just followed your links and I was able to create a bootstrap java
class:

@Singleton
@Startup
public class Bootstrap {

    private static final Logger LOGGER =
LoggerFactory.getLogger(Bootstrap.class.getName());

    @Inject
    private CamelContext context;

    @PostConstruct
    public void init() {
        try {
            LOGGER.info("bootstrapping apache camel");
            context.addRoutes(new UserServiceRouteBuilder());
            context.start();
        } catch (Exception e) {
            LOGGER.error("an error occurred during bootstrapping camel
routes", e);
        }
    }

    @PreDestroy
    public void shutdown() {
        try {
            context.stop();
            LOGGER.info("apache camel routes stopped");
        } catch (Exception e) {
            LOGGER.error("camel stop error", e);
        }
    }
} 


I had to move my-camel-restapi.jar from the lib folder to the root of the
ear file because I got a strange exception. Now the structure of my ear
looks like this:

*.EAR
 |-lib/
 |  |- dependencies, ex.: camel jars
 |  |- my-common.jar
 |
 |- my-web-ui.war
 |- my-stateless-ejb.jar
 |- *my-camel-restapi.jar <- it contains my camel rest route*


And my camel rote is improved a little bit:

public class UserServiceRouteBuilder extends RouteBuilder {
    private static final String APPLICATION_JSON_UTF_8 =
"application/json;charset=UTF-8";
 
    @Override
    public void configure() throws Exception {
        User user = new User();
        user.set...

        restConfiguration().component(*"restlet"*)
        *.host("localhost").port(8081)*
                .bindingMode(RestBindingMode.json)
                .dataFormatProperty("prettyPrint", "true");

        rest("/user")
                .description("user rest service")
                .produces(APPLICATION_JSON_UTF_8)
                .get("/hello")
                   
.outType(User.class).route().routeId("GET@/generate").transform().constant(user).endRest();
    }
}

It works fine, if I make a GET call to http://localhost:8081/user/hello then
I get a proper json answer. Beautiful :) I like Camel, it is a great java
integration tool/framework. It makes Java stronger :)

But I do not understand something. How it is possible to map the restapi to
http port 8081? It means for me that somehow camel starts a small web
container or whatever else on port 8081 inside my GlassFish server. What
happened inside the EE container of the GlassFish?

If I have more, separated camel group of routes packages in different,
separated jars, bootstrapped them separately then I need to use different
ports during the bootstrapping process to avoid the "port in use" error
message. Am I correct? Or I can configure separated routes to use the same
host and port (of course paths need to be different)? How many MB memory is
consumed by my separated jar files with the small "web containers", listen
on different ports?

Can I bootstrap my routes - locate in different jars - by a common
Bootstrap.java from example my-commons.jar? In that case theoretically I
need only one "something" which is listening on ex. port 8081 and all my
routes (rest api) will be available on the same port.

Thx!  




--
View this message in context: http://camel.465427.n5.nabble.com/camel-rest-route-tp5777992p5778013.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: camel rest route

Posted by Claus Ibsen <cl...@gmail.com>.
See the rest examples that can help you on the way, such as
https://github.com/apache/camel/tree/master/examples/camel-example-cdi-rest-servlet

Since you use JEE then you can use CDI to kick-start as that example does.

Or you can kick-start using war
http://camel.apache.org/tutorial-on-using-camel-in-a-web-application.html

Or use the camel servletlistener
http://camel.apache.org/servlet-tomcat-no-spring-example.html

Q2
you configure this on the rest configuration, or if you use servlet
component then on the servlet itself.

Q3
see the json binding at
http://camel.apache.org/rest-dsl

and add camel-jackson to the classpath.



On Sun, Feb 21, 2016 at 3:11 AM, zappee <ar...@gmail.com> wrote:
> Hi,
>
> Currently I am learning how to use Apache Camel Rest and I would like to ask
> some questions about it. I would greatly appreciate any answer.
>
> I have a rest camel route which is packed in a jar. This jar locates in an
> ear file and deployed on GlassFish app. server.
> *Q1:* I am not able to figure out how I can start/bootstrap this route.
>
> I want to use localhost:port/api/user/generate url for my route. It
> generates a simple json answer based on my java pojo with getters/setters.
> *Q2:* How to configure the base url of my route OR figure out the correct
> path of my deployed route?
> *Q3:* How I can send back a json response based on a java object (object is
> automatically convert to json by camel on the fly)? Is my code correct (i am
> not able to test my code till it is not bootstrapped)?
>
> My ear is deployed on server, stateless ejb service and web application are
> working but my camel rest api is not available.
>
> *Structure of my ear:*
> *.EAR
>  |-lib/
>  |  |- dependencies, ex.: camel jars
>  |  |- my-common.jar
>  |  |- my-camel-restapi.jar <- it contains my camel rest route
>  |
>  |- my-web-ui.war
>  |- my-stateless-ejb.jar
>
>
> *Camel route java:*
> public class UserServiceRouteBuilder extends RouteBuilder {
>     private static final String APPLICATION_JSON_UTF_8 =
> "application/json;charset=UTF-8";
>
>     @Override
>     public void configure() throws Exception {
>         restConfiguration().component("jetty")
>                 .bindingMode(RestBindingMode.json)
>                 .dataFormatProperty("prettyPrint", "true");
>
>         User user = new User();
>         user.setEmail("abc@gmail.com");
>         user.setFirstName("John");
>         user.setLastName("Connor");
>
>         rest("/user")
>                 .description("user rest service")
>                 .produces(APPLICATION_JSON_UTF_8)
>
> .get("/generate").route().routeId("GET@/generate").transform().constant(user).endRest();
>     }
> }
>
>
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/camel-rest-route-tp5777992.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2: https://www.manning.com/ibsen2