Retrieving Local Files from a JMS Queue


In 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 locally.

Edit JMSCopier.java

Add the retrieveFileFromJMS method to the JMSCopier class as follows:

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();
}

public void retrieveFileFromJMS(final String queueName, final String filePath) throws Exception
{
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();

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

context.addRoutes(new RouteBuilder()
{
public void configure()
{
from("jms:queue:"+queueName).process(new Processor()
{
public void process(Exchange exchange) throws Exception
{
System.out.println("Retrieving file: "+exchange.getIn().getHeader("CamelFileName"));

}
}).to("file:"+filePath+"?noop=true");
}
});

context.start();
Thread.sleep(10000);
context.stop();
}
}

JMSCopier.java

Edit JMSCopierTest.java

Add the method testRetrieveLocalFilesFromJMSQueue to the JMSCopierTest class as follows:

package com.skills421.examples.camel.basics;

import static org.junit.Assert.*;

import org.junit.Ignore;
import org.junit.Test;

public class JMSCopierTest
{

@Test
@Ignore
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());
}
}

@Test
public void testRetrieveLocalFilesFromJMSQueue()
{
JMSCopier copier = new JMSCopier();

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

try
{
copier.retrieveFileFromJMS(queueName, filePath);
}
catch (Exception e)
{
fail(e.getMessage());
}
}

}

JMSCopierTest.java
Note, you will need to modify the filePath to a local filePath on your own system.

Run the JUnit Test

Now, assuming you have messages in your message queue, running the test should download the files from the MessageQueue into your local directory, and display the following output in the console:

INFO: Apache Camel 2.5.0 (CamelContext: camel-1) started in 1.324 seconds
Retrieving file: file1.txt
Retrieving file: file2.txt
Retrieving file: file3.txt
Feb 08, 2014 8:34:00 PM org.apache.camel.impl.DefaultCamelContext doStop
INFO: Apache Camel 2.5.0 (CamelContext:camel-1) is shutting down

Note that the message queue will now be empty.

Advertisement

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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