|
Page Edited :
SM :
3.2. Using the Camel Java DSL for the first time
3.2. Using the Camel Java DSL for the first time has been edited by Gert Vanthienen (Feb 15, 2008). Content:On this page, we are going to use the Camel Java DSL for the first time to specify a route.
Configuring camel-context.xml to use the Java DSLWe will be using the Java DSL in this tutorial, but Camel also supports a Spring XML-based syntax for specifying routes and will probably support things like a Groovy DSL in the future as well. In you look at the src/main/resources/camel-context.xml file inside the tutorial-camel-su project, you will see how the Maven archetype already generates an example of the XML syntax. We change this file to point to the package that will contain our RouteBuilder classes. When the SU is started, all RouteBuilder classes will be located and the routes in there will be activated. <beans ...> <camelContext id="camel" xmlns="http://activemq.apache.org/camel/schema/spring"> <package>org.apache.servicemix.tutorial.camel</package> </camelContext>
</beans>
Coding the RouteNow, find the sample org.apache.servicemix.tutorial.camel.MyRouteBuilder class the Maven archetype has generated for us in the default src/main/java folder and add this route to send a message every 5 seconds to the configure() method. public class MyRouteBuilder extends RouteBuilder { public void configure() { from("timer://tutorial?fixedRate=true&period=10000") // 1 .setBody(constant("Hello world!")) // 2 .to("log:tutorial"); // 3 } } What will this route do?As you can see, the route is a mix of very specific Java methods (that make up the DSL) and URIs. Now what will this route do once we deploy it on ServiceMix? You can probably make a fair guess by just looking at the code, but let's examine it in a bit more detail:
Deploy the SANow, do a mvn install in your root project folder to build the SU and SA again. Afterwards, deploy the SA to ServiceMix by either:
If you look at the ServiceMix console logging, you should see INFO - Timer-4 - AutoDeploymentService - Directory: hotdeploy: Archive changed: processing tutorial-camel-sa-1.0-SNAPSHOT.jar ... INFO - Timer-4 - ServiceAssemblyLifeCycle - Starting service assembly: tutorial-camel-sa INFO - Timer-4 - ServiceUnitLifeCycle - Initializing service unit: tutorial-camel-su INFO - Timer-4 - ServiceUnitLifeCycle - Starting service unit: tutorial-camel-su INFO - tutorial - tutorial - Exchange[Message: Hello world!] INFO - Timer-4 - AutoDeploymentService - Directory: hotdeploy: Finished installation of archive: tutorial-camel-sa-1.0-SNAPSHOT.jar INFO - tutorial - tutorial - Exchange[Message: Hello world!] INFO - tutorial - tutorial - Exchange[Message: Hello world!] INFO - tutorial - tutorial - Exchange[Message: Hello world!] INFO - tutorial - tutorial - Exchange[Message: Hello world!] ... and one additional line here for every 10 seconds you wait We have just created our first Camel route and deployed it to ServiceMix. In the next step, we are going to look at how we can send messages to other services that are exposed on the ESB from within our Camel route. Things to remember
Further reading
|
Unsubscribe or edit your notifications preferences
