In our next example we will send local files to a JMS Queue and then view the Queue using the console.

Edit pom.xml

First we need to add the dependencies for JMS as follows:

<project xmlns="" xmlns:xsi=""


		<!-- Core Camel -->

		<!-- Needed for the FTP Component -->

		<!-- Needed for com/jcraft/jsch/Logger - SFTP -->

		<!-- Needed for JMS -->

		<!-- Needed for ActiveMQ - JMS -->

		<!-- JUnit for testing -->





Next we want to create the code that will use Camel to send our local files to the JMS Queue.

package com.skills421.examples.camel.basics;

import javax.jms.ConnectionFactory;

import org.apache.activemq.spring.ActiveMQConnectionFactory;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jms.JmsComponent;
import org.apache.camel.impl.DefaultCamelContext;

public class JMSCopier
	public void sendFileToJMS(final String filePath, final String queueName) throws Exception
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();

		CamelContext context = new DefaultCamelContext();
		context.addComponent("jms", JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));

		context.addRoutes(new RouteBuilder()
			public void configure()
				from("file:"+filePath+"?noop=true").process(new Processor()
					public void process(Exchange exchange) throws Exception
						System.out.println("Sending file: "+exchange.getIn().getHeader("CamelFileName"));



Note how we have added a process to our from().to() in our configure() methods to give from().process().to().
This enables us to print out a message as each file is processed.


Next we create the JUnit Test file as follows:

package com.skills421.examples.camel.basics;

import static org.junit.Assert.*;

import org.junit.Test;

public class JMSCopierTest

	public void testSendLocalFilesToJMSQueue()
		JMSCopier copier = new JMSCopier();

		String filePath = "/Users/johndunning/Desktop/Camel/CamelIn";
		String queueName = "copierQueue";

			copier.sendFileToJMS(filePath, queueName);
		catch (Exception e)

Note that you will need to configure the filePath for your local system.

Run the Test

Run the JUnit Test and you should see a log something like the following:

INFO: Apache Camel 2.5.0 (CamelContext: camel-1) is starting
INFO: Successfully connected to tcp://localhost:61616
Sending file: file1.txt
INFO: Successfully connected to tcp://localhost:61616
Sending file: file2.txt
INFO: Successfully connected to tcp://localhost:61616
Sending file: file3.txt
INFO: Apache Camel 2.5.0 (CamelContext: camel-1) is shutdown in 0.008 seconds

Check the ActiveMQ Console

Now open the ActiveMQ Console by entering the following URL into your browser: http://localhost:8161/admin/index.jsp

Remember the default login and password are admin and admin

The console will look something like this:

003-ActiveMQConsoleClick on Queues to see the active message queues and you should see your copierQueue in the queue list:


Click on copierQueue to see the individual messages


Finally, click on one of the messages and you should see the content of your file under Message Details as follows:



One thought on “Sending Local files to a JMS Queue

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s