8 Replies Latest reply on Aug 5, 2009 1:45 AM by Claus Ibsen

    Is there way to use bean as conumer from camel?

    jawed khan Newbie

      I am trying to figure how to use bean as camel from("bean:myService") in DSL. Java DSL provides Processor and Bean to this. Even in the Bean there is @consumer annotation also. Is there an example showing this?

      Thanks

      Jawed

        • 1. Re: Is there way to use bean as conumer from camel?
          Claus Ibsen Master

          Hi

           

          Yeah you can use the @Consume annotation to let a bean be an event driven consumer.

           

          There is an example and documentation here:

          http://camel.apache.org/pojo-messaging-example.html

          http://camel.apache.org/pojo-consuming.html

          • 2. Re: Is there way to use bean as conumer from camel?
            Claus Ibsen Master

            You can also do a route like this

             

            from("bean:myBean").to("jms:queue:bar");

             

            Then Camel will invoke myBean, to receive some data to be routed to the JMS queue. Note the Camel will constantly call you bean as soon its finished routing the message. This may not be what you want as you may want it to be scheduled.

             

            However if you want this to be scheduled, eg do this every 5th second then you need to use a timer to trigger the schedule

             

            from("timer:foo?period=5000").to("bean:myBean", "jms:queue:bar");

             

            You can use the ?method=xxx to tell Camel which method name to invoke on your bean if you have more than one method.

             

            From an unit test:

             

                public void testFromBean() throws Exception {

                    getMockEndpoint("mock:bar").expectedBodiesReceived("Hello");

                    getMockEndpoint("mock:bar").expectedMinimumMessageCount(1);

             

                    assertMockEndpointsSatisfied();

                }

             

                @Override

                protected JndiRegistry createRegistry() throws Exception {

                    JndiRegistry jndi = super.createRegistry();

                    jndi.bind("foo", new MyFooBean());

                    return jndi;

                }

             

                @Override

                protected RouteBuilder createRouteBuilder() throws Exception {

                    return new RouteBuilder() {

                        @Override

                        public void configure() throws Exception {

                            from("bean:foo?method=hello").to("mock:bar");

                        }

                    };

                }

             

                private static class MyFooBean {

             

                    public String hello() {

                        return "Hello";

                    }

             

                    public String bye() {

                        return "Bye";

                    }

                }

            • 3. Re: Is there way to use bean as conumer from camel?
              jawed khan Newbie

              Hi Claus,

              I did try the bean endpoint and it works. Besides scheduling, is there any other way to implement Async in Bean? May be using Spring or OSGi?

              Thanks

              Jawed

              • 4. Re: Is there way to use bean as conumer from camel?
                Claus Ibsen Master

                Can you be a bit more specific with what you mean by async?

                 

                You can use seda endpoints that provides async behovioir

                http://camel.apache.org/seda.html

                 

                You can also use .thread(5) in a route to indicate async behavior and that a core pool size of 5 should be used.

                 

                And there are numerous improvements and new features in Camel 2.0.

                http://camel.apache.org/async

                • 5. Re: Is there way to use bean as conumer from camel?
                  jawed khan Newbie

                  Hi Claus,

                  Well, we have an api which implement jms specification. We have enhanced it to be an OSGi service. We want to use this service as OSGi in Camel or CXF so we don't want to implement Camel or CXF component because of support.

                  It consumes message from any jms provider as asynconhouously. I tried to use as bean in camel flow then bean keeps reading it. Per your advise i added timer which works fine. In JMS we can register a listener for jms consumer so whenever message arrives the consumer gets nofitied instead of polling.

                  Can I do similar in Bean? I saw the Camel JMS component and it does have listener.

                   

                  Thanks

                  Jawed

                  • 6. Re: Is there way to use bean as conumer from camel?
                    Claus Ibsen Master

                    You can use annotations on a Bean to let it work like a onMessage in a MDB

                     

                    @Consume("somewhere")

                    public void onSomething(Exchange exchange) throws Exception {

                    }

                     

                     

                    Camel uses its bean parameter mapping so the method signature can be adjusted how you like it, for instance to avoid the Camel Exchange API you can use String as parameter to receive your payload as a String type.

                     

                    @Consume("somewhere")

                    public void onSomething(String payload) throws Exception {

                    }

                     

                    See more at:

                    http://camel.apache.org/pojo-consuming.html

                    • 7. Re: Is there way to use bean as conumer from camel?
                      jawed khan Newbie

                      Hi Claus,

                      I am trying to use @Consume but don't know what attribute value should be. Can you provide me example or explain about @Consume attribute.

                       

                      Here is my flow:

                      from("bean:momosgiservice?method=getMessage")

                                  .to("log:lab1-SMX4.1")

                       

                      Here is my bean method:

                      @Consume()

                           public String getMessage()

                           {

                                if (logger.isDebugEnabled())

                                   logger.debug("Inside MyBean getMessage()" + message);

                                return message;

                           }

                      Thank you.

                      Jawed

                      • 8. Re: Is there way to use bean as conumer from camel?
                        Claus Ibsen Master

                        See the POJO messaging example

                        http://camel.apache.org/pojo-messaging-example.html

                         

                        Basically the from(bean) in the route is not needed as your bean is annotated with @Consume so Camel will use it as a event driven consumer.

                         

                        What you need is to route the message afterwards, you can either use a ProducerTemplate to send it somewhere. For instance by annotating a field with the @Produce to get hold of a producer template.

                         

                        Something like this:

                         

                        @Produce("log:lab1-SMX4.1")

                        private ProducerTemplate template;

                         

                        @Consume("from somewhere")

                        public void onMyMessage(String payload) {

                           .... do something with message if you like

                           template.sendBody(payload);

                        }