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="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.skills421.examples.camel</groupId> <artifactId>CamelBasics</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <!-- Core Camel --> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-core</artifactId> <version>2.5.0</version> </dependency> <!-- Needed for the FTP Component --> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-ftp</artifactId> <version>2.5.0</version> </dependency> <!-- Needed for com/jcraft/jsch/Logger - SFTP --> <dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>0.1.42</version> </dependency> <!-- Needed for JMS --> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-jms</artifactId> <version>2.5.0</version> </dependency> <!-- Needed for ActiveMQ - JMS --> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-core</artifactId> <version>5.3.2</version> </dependency> <!-- JUnit for testing --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> </dependencies> </project>
pom.xml
Create JMSCopier.java
Next we want to create the JMSCopier.java 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")); } }).to("jms:queue:"+queueName); } }); context.start(); Thread.sleep(10000); context.stop(); } }
JMSCopier.java
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.
Create JMSCopierTest.java
Next we create the JUnit Test file JMSCopierTest.java as follows:
package com.skills421.examples.camel.basics; import static org.junit.Assert.*; import org.junit.Test; public class JMSCopierTest { @Test public void testSendLocalFilesToJMSQueue() { JMSCopier copier = new JMSCopier(); String filePath = "/Users/johndunning/Desktop/Camel/CamelIn"; String queueName = "copierQueue"; try { copier.sendFileToJMS(filePath, queueName); } catch (Exception e) { fail(e.getMessage()); } } }
JMSCopierTest.java
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:

Click 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:

[…] this example, we will retrieve the messages we put on the message queue in post Sending Local Files to a JMS Queue and stored them […]
[…] this example, we will retrieve the messages we put on the message queue in post Sending Local Files to a JMS Queue and stored them […]