11 Replies Latest reply on Jul 2, 2012 5:32 AM by nicolas duminil

    A simple "Hello World" for Karaf ?

    nicolas duminil Master

      Greetings,

       

      I was wondering where could I find a simple "Hello World" project using Spring DM to print the message in the Karaf console as soon as started ? The way I'm working currently is using the camel-archetype-spring-dm maven archetype which generates a camel context, in which I need to define a route, etc. But how could I just print "Hello World" in a bundle, without havaing BundleActivator but just Spring dm ? And is there any maven archetype to generates this project ?

       

      Many thanks,

       

      Nicolas

        • 1. Re: A simple "Hello World" for Karaf ?
          Claus Ibsen Master

          Hi

           

          Yes you can use the camel-archetype-spring-dm to create a new Spring-DM based project with Camel.

           

          And then just replace the generated route with what you want instead.

          For example a timer to fire every 5th second and then print to the log would be

           

          <route>
            <from uri="timer:foo?period=5s"></from>
            <log>Hello World</log>
          </route>
          

           

          If you want to print to the karaf shell console (which will be annoying as its printing where you edit, but then you need to use the camel-stream component)

          http://camel.apache.org/stream, eg

          <to uri="stream:out"/>
          

           

          Remember to add camel-stream as dependency then.

           

           

          The camel-archetype-blueprint generates a sample project that is similar to what you ask about, but its for OSGi blueprint. You can look at that, and then do similar for Spring-DM instead. Which essentially would be to changed the namespace in the XML, and depend on camel-spring instead etc.

          • 2. Re: A simple "Hello World" for Karaf ?
            nicolas duminil Master

            Hi Claus,

             

            Many thanks for taking the time again to answer to me. This is exactly what I have done, with the exception of stream:out. My initially code looks like:

             

               

             

            It doesn't display anything neither. Of course, I added the required dependency and, additionally, I needed to instal the features camel-streams (why is it not installed by default ?).

             

            Of course, running the route with camel:run does as expected.

             

            Could you please light my lanterne again ?

             

            Many thanks in advance,

             

            Nicolas

            • 3. Re: A simple "Hello World" for Karaf ?
              Gert Vanthienen Novice

              Nicolas,

               

              In the Karaf console, do you see the the Spring status listed as 'Started' for your application bundle?  If not, you may want to check the log files for error messages during route startup.

               

              We only install the most commonly used Camel components by default but you can always add the optional ones with a 'features:install' as you already figured out.  In Fuse ESB Enterprise 7 we do have a download available that contains the JARs for all components, so they can be installed very quickly without requiring network access for downloading them.

               

              While trying out things with the plain Spring XML syntax alone (and no Java code), I often just copy the XML file into the deploy directory directly.  That allows you to edit the file, save and instantly see the results as the Spring XML gets redeployed automatically.  Once you have it working there, all you have to do is embed in a bundle.

               

               

              Regards,

               

              Gert

              • 4. Re: A simple "Hello World" for Karaf ?
                Claus Ibsen Master

                If you use  then the message is written to the server log. You can see the log from the shell command: log:display

                 

                There is also a log:tail (I think thats the name)

                 

                And the log itself is written in ESB_HOME/data/logs directory.

                • 5. Re: A simple "Hello World" for Karaf ?
                  nicolas duminil Master

                  Hi, Many thanks for your replies.

                   

                  @Gert: I got

                  karaf@root> osgi:list | grep spring

                  spring-osgi-core (1.2.1)

                  spring-osgi-io (1.2.1)

                  spring-osgi-annotation (1.2.1)

                  spring-osgi-extender (1.2.1)

                  activemq-spring (5.5.1.fuse-06-03)

                  camel-spring (2.8.0.fuse-05-03)

                   

                  So it seems normal.

                   

                  @Claus: I got

                  araf@root> log:display-exception

                  rg.apache.camel.InvalidPayloadException: No body available of type: java.lang.String on: Message: . Caused by: No type converter available to convert from type: null to the required typ

                  : java.lang.String with value null. Exchange[Message: ]. Caused by: [org.apache.camel.NoTypeConversionAvailableException - No type converter available to convert from type: null to the

                  equired type: java.lang.String with value null]

                         at org.apache.camel.impl.MessageSupport.getMandatoryBody(MessageSupport.java:101)[88:org.apache.camel.camel-core:2.8.0.fuse-05-03]

                         at org.apache.camel.component.stream.StreamProducer.writeToStream(StreamProducer.java:125)[239:org.apache.camel.camel-stream:2.8.0.fuse-05-03]

                         at org.apache.camel.component.stream.StreamProducer.process(StreamProducer.java:75)[239:org.apache.camel.camel-stream:2.8.0.fuse-05-03]

                         at org.apache.camel.impl.converter.AsyncProcessorTypeConverter$ProcessorToAsyncProcessorBridge.process(AsyncProcessorTypeConverter.java:50)[88:org.apache.camel.camel-core:2.8.0.fuse-05-03]

                         at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:78)[88:org.apache.camel.camel-core:2.8.0.fuse-05-03]

                         at org.apache.camel.processor.SendProcessor$2.doInAsyncProducer(SendProcessor.java:114)[88:org.apache.camel.camel-core:2.8.0.fuse-05-03]

                         at org.apache.camel.impl.ProducerCache.doInAsyncProducer(ProducerCache.java:284)[88:org.apache.camel.camel-core:2.8.0.fuse-05-03]

                         at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:109)[88:org.apache.camel.camel-core:2.8.0.fuse-05-03]

                         at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:78)[88:org.apache.camel.camel-core:2.8.0.fuse-05-03]

                         at org.apache.camel.processor.DelegateAsyncProcessor.processNext(DelegateAsyncProcessor.java:98)[88:org.apache.camel.camel-core:2.8.0.fuse-05-03]

                         at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:89)[88:org.apache.camel.camel-core:2.8.0.fuse-05-03]

                         at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:69)[88:org.apache.camel.camel-core:2.8.0.fuse-05-03]

                         at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:78)[88:org.apache.camel.camel-core:2.8.0.fuse-05-03]

                         at org.apache.camel.processor.DelegateAsyncProcessor.processNext(DelegateAsyncProcessor.java:98)[88:org.apache.camel.camel-core:2.8.0.fuse-05-03]

                         at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:89)[88:org.apache.camel.camel-core:2.8.0.fuse-05-03]

                         at org.apache.camel.processor.interceptor.TraceInterceptor.process(TraceInterceptor.java:99)[88:org.apache.camel.camel-core:2.8.0.fuse-05-03]

                         at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:78)[88:org.apache.camel.camel-core:2.8.0.fuse-05-03]

                         at org.apache.camel.processor.DelegateAsyncProcessor.processNext(DelegateAsyncProcessor.java:98)[88:org.apache.camel.camel-core:2.8.0.fuse-05-03]

                         at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:89)[88:org.apache.camel.camel-core:2.8.0.fuse-05-03]

                         at org.apache.camel.fabric.FabricTraceProcessor.process(FabricTraceProcessor.java:81)[88:org.apache.camel.camel-core:2.8.0.fuse-05-03]

                         at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:78)[88:org.apache.camel.camel-core:2.8.0.fuse-05-03]

                         at org.apache.camel.processor.RedeliveryErrorHandler.processErrorHandler(RedeliveryErrorHandler.java:319)[88:org.apache.camel.camel-core:2.8.0.fuse-05-03]

                         at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:210)[88:org.apache.camel.camel-core:2.8.0.fuse-05-03]

                         at org.apache.camel.processor.RouteContextProcessor.processNext(RouteContextProcessor.java:45)[88:org.apache.camel.camel-core:2.8.0.fuse-05-03]

                         at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:89)[88:org.apache.camel.camel-core:2.8.0.fuse-05-03]

                         at org.apache.camel.processor.DefaultChannel.process(DefaultChannel.java:304)[88:org.apache.camel.camel-core:2.8.0.fuse-05-03]

                         at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:78)[88:org.apache.camel.camel-core:2.8.0.fuse-05-03]

                         at org.apache.camel.processor.Pipeline.process(Pipeline.java:116)[88:org.apache.camel.camel-core:2.8.0.fuse-05-03]

                         at org.apache.camel.processor.Pipeline.process(Pipeline.java:79)[88:org.apache.camel.camel-core:2.8.0.fuse-05-03]

                         at org.apache.camel.processor.RouteContextProcessor.processNext(RouteContextProcessor.java:45)[88:org.apache.camel.camel-core:2.8.0.fuse-05-03]

                         at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:89)[88:org.apache.camel.camel-core:2.8.0.fuse-05-03]

                         at org.apache.camel.processor.UnitOfWorkProcessor.processAsync(UnitOfWorkProcessor.java:150)[88:org.apache.camel.camel-core:2.8.0.fuse-05-03]

                         at org.apache.camel.processor.UnitOfWorkProcessor.process(UnitOfWorkProcessor.java:117)[88:org.apache.camel.camel-core:2.8.0.fuse-05-03]

                         at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:78)[88:org.apache.camel.camel-core:2.8.0.fuse-05-03]

                         at org.apache.camel.processor.DelegateAsyncProcessor.processNext(DelegateAsyncProcessor.java:98)[88:org.apache.camel.camel-core:2.8.0.fuse-05-03]

                         at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:89)[88:org.apache.camel.camel-core:2.8.0.fuse-05-03]

                         at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:69)[88:org.apache.camel.camel-core:2.8.0.fuse-05-03]

                         at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:104)[88:org.apache.camel.camel-core:2.8.0.fuse-05-03]

                         at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:85)[88:org.apache.camel.camel-core:2.8.0.fuse-05-03]

                         at org.apache.camel.component.timer.TimerConsumer.sendTimerExchange(TimerConsumer.java:134)[88:org.apache.camel.camel-core:2.8.0.fuse-05-03]

                         at org.apache.camel.component.timer.TimerConsumer$1.run(TimerConsumer.java:63)[88:org.apache.camel.camel-core:2.8.0.fuse-05-03]

                         at java.util.TimerThread.mainLoop(Timer.java:512)[:1.6.0_24]

                         at java.util.TimerThread.run(Timer.java:462)[:1.6.0_24]

                  aused by: org.apache.camel.NoTypeConversionAvailableException: No type converter available to convert from type: null to the required type: java.lang.String with value null

                         at org.apache.camel.impl.converter.BaseTypeConverterRegistry.mandatoryConvertTo(BaseTypeConverterRegistry.java:145)[88:org.apache.camel.camel-core:2.8.0.fuse-05-03]

                         at org.apache.camel.core.osgi.OsgiTypeConverter.mandatoryConvertTo(OsgiTypeConverter.java:110)[91:org.apache.camel.camel-spring:2.8.0.fuse-05-03]

                         at org.apache.camel.impl.MessageSupport.getMandatoryBody(MessageSupport.java:99)[88:org.apache.camel.camel-core:2.8.0.fuse-05-03]

                         ... 42 more

                   

                  So the reason nothing was displayed is this exception. The route is:

                   

                       

                  What could be the problem ?

                   

                  Many thanks.

                   

                  Nicolas

                  • 6. Re: A simple "Hello World" for Karaf ?
                    Claus Ibsen Master

                    The timer sets a null body when the stream out wants to write. We should most likely improve camel-stream to ignore null bodies then.

                     

                    What you need is to transform the message to something the stream can write

                     

                    eg you can use the message translator EIP for that

                    http://camel.apache.org/message-translator.html

                     

                    <transform>
                      <constant>Hello World</constant>
                    </transform>
                    

                     

                    • 7. Re: A simple "Hello World" for Karaf ?
                      Claus Ibsen Master

                      I have logged a ticket to improve this in the future about the null body

                      https://issues.apache.org/jira/browse/CAMEL-5395

                      • 8. Re: A simple "Hello World" for Karaf ?
                        nicolas duminil Master

                        Hi Claus,

                         

                        Many thanks for the update.

                         

                        Yeah, this is why I'm saying it's complicated. If in order to do a simple OSGi Hello World, which doesn't have anything to do with Camel, I need a Camel route, a timer and a transformer, then this is to say that there is no a maven archetype creating a simple Hello Worls to be deployed in an OSGi container.

                         

                        Am I wrong ?

                         

                        Kind regards,

                         

                        Nicolas

                        • 9. Re: A simple "Hello World" for Karaf ?
                          Claus Ibsen Master

                          The camel maven archetypes for

                          - spring-dm

                          - blueprint

                           

                          Generates projects that are ready to be installed and deployed in an OSGi container.

                           

                          If you use Fuse IDE then its even easier, as there is a wizard for creating this, eg File -> New Project... -> Fuse Project -> select the archetype of choice -> next.. next.. finish.

                          • 10. Re: A simple "Hello World" for Karaf ?
                            Claus Ibsen Master

                            If you want to do a pure vanilla OSGi project then all you need is a either a OSGi blueprint XML file or a Spring XML file, and drop that XML file into the deploy directory of Fuse ESB, then its auto installed.

                             

                            But if you want a vanilla OSGi project then you need to use an OSGi Activator to hook your application into OSGi so its bootstrapped when the OSGi bundle is started/stopped.

                             

                            Spring-DM and OSGi blueprint does this automatic by bootstrapping that said XML file for you.

                             

                            The Apache Karaf project have some Maven projects for creating OSGi projects, you can try one of them, eg: org.apache.karaf.archetypes:karaf-blueprint-archetype

                             

                            If you have Maven 3.0.3 or better, then just type

                            mvn archetype:generate
                            

                             

                            Then its interactive, and you just type: karaf ENTER

                            and it list all archetypes related to Karaf.

                             

                            Likewise you can do that for camel etc.

                            • 11. Re: A simple "Hello World" for Karaf ?
                              nicolas duminil Master

                              Hi Claus,

                               

                              Yes, this is what I was looking for. Many thanks.

                               

                              Kind regards,

                               

                              Nicolas