camel JMS message transfer example

Message transfer using JMS in camel with simple steps.


In our example we will learn how can we transfer message to destination. As a broker we are using ActiveMQ.So we have assumed you have ActiveMQ in your local machine.

  • In eclipse create a maven project MyFirstCamelJMSProject as we created in earlier page and right click on src/main/java to create package. New -> package



  • Give any name for example com.preparationforinterview.jms and click on Finish



  • Right click on package and create a java class for example JmsInCamelExample.



  • Right click on package and create one more java class for example JmsExecutor.



  • Add following dependency in pom.xml file.you can use any latest version.

  •  	
    		<dependency>
    			<groupId>org.apache.camel</groupId>
    			<artifactId>camel-core</artifactId>
    			<version>2.13.0</version>
    		</dependency>
    		<dependency>
    			<groupId>org.apache.camel</groupId>
    			<artifactId>camel-jms</artifactId>
    			<version>2.5.0</version>
    		</dependency>
    		<dependency>
    			<groupId>org.apache.activemq</groupId>
    			<artifactId>activemq-core</artifactId>
    			<version>5.3.2</version>
    		</dependency>
    
  • Run mvn:install from command prompt or if you are using Eclipse right click on pom.xml and run mvn install.It will download camel jars

  • Copy below code in JmsInCamelExample.

  •  		
    package com.preparationforinterview.jms;
    	
    import org.apache.camel.builder.RouteBuilder;
    
    public class JmsInCamelExample extends RouteBuilder {
    
    	@Override
    	public void configure() throws Exception {
    
    		try {
    			from("file:input?noop=true")
    					.process(new MyProcessor())
    					.to("jms:queue:preparationforinterview");
    		} catch (Exception ex) {
    			System.out.println(ex.getMessage());
    		}
    
    	}
    }
    

    Explain

    To create a route in camel we need to extend RouteBuilder class and override configure method.In configure method we define our route.In above example we can read it as from input folder take file and send it to queue "preparationforinterview".Here jms: is called endpoint.

  • Copy below code in JmsExecutor.

  •  	
    package com.preparationforinterview;
    import javax.jms.ConnectionFactory;
    
    import org.apache.activemq.ActiveMQConnectionFactory;
    import org.apache.camel.CamelContext;
    import org.apache.camel.component.jms.JmsComponent;
    import org.apache.camel.impl.DefaultCamelContext;
    
    public class JmsExecutor {
    	public static void main(String args[]) throws Exception {
    		 CamelContext context = new DefaultCamelContext();
    	 ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
    		 context.addComponent("jms",JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));
    		 context.addRoutes(new JmsInCamelExample());
    		 context.start();
    		 Thread.sleep(100000);
    		 context.stop();
    		 }
    
    }
    

    Explain

    First we need to create CamelContext. It has many implementation but we are using DefaultCamelContext.Camel provides ConnectionFactory but to which provider we need to connect we have to give that here we are using ActiveMQ so give ActiveMQConnectionFactory and add it to camel JmsComponent and link it to "jms" name ,you can use any name but whatever you will give, use same in JmsInCamelExample router class.After that add route in camel context.Its like registry of route in CamelContext, till that part we have just configured route in CamelContext but until and unless we start CamelContext it will not execute route.We kept Thread.sleep(1000) so that camel gets enough time to copy file from input to output folder.

    To check the output right click on JmsExecutor.java and run it.It will send content from input folder to preparationforinterview queue.


    To run the program from command prompt, open command prompt and go to project location and run below command.
    mvn compile exec:java -Dexec.mainClass=com.preparationforinterview.JmsExecutor

Download source code

Camel JMS message transfer Example



Visit Others Links

Camel Introduction
Camel Setup
File Copy Example Using Camel
File Copy Example In Camel Using Spring
JMS Message Transfer Example Using Camel
JMS Message Transfer Example In Camel Using Spring
Processor In Camel
Object to JSON Transfer Example Using Spring In Camel
Camel With Bean Injection Example Using Spring DSL
Camel With Bean Injection Registry Example Using Java DSL
Camel Bean Parameter Binding Example Using Java DSL
Camel Error Handling With Redelivery Example Using Java DSL
Camel Exception Handling With Example Using Java DSL
Camel JUnit Test With Example Using Java DSL
Camel JUnit Test With Example Using Spring DSL
Camel Unit Test With Mock Component Example
Camel With Stream Component Example
Camel In-Memory Messaging Example Using Direct
Camel With Timer And Quartz Example
Camel Aggregator EIP Example
Camel Splitter EIP Example
Camel Routing Slip EIP Example
Camel Dynamic Router EIP Example
Camel Load Balancer EIP Example
Top Camel Interview Question
© 2019 PreparationForInterview.com