camel JMS message transfer using Spring example

Message transfer using JMS in camel Using spring with simple steps.


In our example we will learn how can we transfer message to destination using spring.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.spring and click on Finish



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



  • Right click on src/main/resources and and create one .xml file for example MySpring.xml.



  • 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>
    			<dependency>
    			<groupId>org.apache.camel</groupId>
    			<artifactId>camel-spring</artifactId>
    			<version>2.13.0</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 AppSpringExecutor.

  •  
    package com.preparationforinterview.jms.spring;		
    import org.springframework.context.support.AbstractApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class AppSpringExecutor {
    	public static void main(String args[]) throws InterruptedException {
    		@SuppressWarnings("resource")
    		AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(
    				"MySpring.xml");
    		ctx.start();
    		System.out.println("Entered>>>>>");
    		Thread.sleep(5 * 60 * 1000);
    		ctx.stop();
    	}
    
    }
    

    Explain

    In this example we are loading spring file and starting CamelContext.When we start CamelContext ,it will start to read message from input and will send it to destination.

  • Write lines in MySpring.xml.

  •  	
    	<beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel="http://camel.apache.org/schema/spring"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans
    		http://www.springframework.org/schema/beans/spring-beans.xsd         
    		http://camel.apache.org/schema/spring 
    		http://camel.apache.org/schema/spring/camel-spring.xsd">
    
    	<bean id="jms" class="org.apache.camel.component.jms.JmsComponent">
    		<property name="connectionFactory">
    			<bean class="org.apache.activemq.ActiveMQConnectionFactory"></bean>
    		</property>
    	</bean>
    	<camelContext xmlns="http://camel.apache.org/schema/spring">
    		<route>
    			<from uri="file:input?noop=true" />
    			<to uri="jms:queue:preparationforinterview" />
    		</route>
    	</camelContext>
    	</beans>
    


    Explain

    Last entry in xml file <camelContext xmlns="http://camel.apache.org/schema/spring"/> creates SpringCamelContext which provides CamelContext.In CamelContext we have add route so when we start spring application that routes also start

    To check the output right click on AppSpringExecutor.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.jms.spring.AppSpringExecutor

Download source code

Camel JMS message transfer Example In Camel Using Spring



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