I have a camel route that looks like this:
<camel:camelContext id="context" xmlns="http://camel.apache.org/schema/spring">
<propertyPlaceholder id="camelContextProperties" location="ref:lefrogen"/>
<contextScan/>
<template id="producerTemplate"/>
<template id="consumerTemplate"/>
<route id="chibzo">
<from uri="activemq:queueName"/>
<to uri="seda:internal?multipleConsumers=true"/>
</route>
<route id="chon">
<from uri="seda:internal?multipleConsumers=true"/>
<bean ref="beanName"/>
<to uri="seda:external?multipleConsumers=true"/>
</route>
</camel:camelContext>
The bean class is like this:
#Component
public class BeanName {
private final static Logger LOGGER = Logger.getLogger(BeanName.class.getName());
public static String message = null;
public static String map(String custom) {
message = custom;
return custom;
}
}
I have written a unit test which is supposed to get the data from the bean and then compare it with the data from seda:external. The problem is that I am getting null when I am fetching data from the seda queue. Here's the unit test:
public class Test{
#EndpointInject
ProducerTemplate producerTemplate;
#EndpointInject
ConsumerTemplate consumerTemplate;
#Autowired
BeanName beanName;
#Test
public void testName() throws Exception {
producerTemplate.sendBody("seda:internal","Good bye");
Thread.sleep(10000); //needs to sleep so that the bean gets executed before accessing
//beanName.message;
String message = beanName.message;
LOGGER.info("Printing" + message); //prints (Good bye)
String body = consumerTemplate.receiveBodyNoWait("seda:external", String.class);
LOGGER.info("Body is" + body); //prints null
}
Any reason why the queue is giving null?
Its due concurrent, and you are using recieveBodyNoWait which does a fast check. Instead use receiveBody and provide may a timeout value to avoid waiting if there is no messages on the queue.
Or add sleep in the code if you want to use receiveBodyNoWait still.
Related
Available examples of the usage of the Camel Test component show how to test the expectations of a route:
However what I need to do is mock the body (manually setting it) of an intermediate route, e.g.:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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="exampleBean" class="xxx.ExampleBean"/>
<routeContext id="routesTest" xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="direct:route1" />
<to uri="direct:route2" />
<log message="${body}"/>
</route>
<route>
<from uri="direct:route2"/>
<to uri="bean:exampleBean"/>
<to uri="direct:route3" />
</route>
<route>
<from uri="direct:route3"/>
<log message="${body}"/>
</route>
</routeContext>
</beans>
In this scenario I want to completely avoid the actual execution of bean:exampleBean, mocking the result of its execution.
My test class:
public class MyTests extends CamelSpringTestSupport {
#Produce(uri = "direct:route1")
protected ProducerTemplate inputProducerTemplate;
#EndpointInject(uri = "mock:bean:exampleBean")
protected MockEndpoint mockBeanExampleBean;
#Test
public void testRoute() throws Exception {
CompletableFuture<Object> future = inputProducerTemplate.asyncSendBody("direct:route1", "Some message");
Object o = future.get();
}
#Override
public String isMockEndpoints() {
return "bean:exampleBean";
}
#Override
protected AbstractApplicationContext createApplicationContext() {
return new ClassPathXmlApplicationContext("spring/gesti-test-application-context.xml");
}
}
public class ExampleBean {
public String enhance(String message) {
System.out.println(message);
//Here I would call a REST API
return "MY API RESULT";
}
}
When using mockBeanExampleBean.whenAnyExchangeReceived(exchange -> exchange.getMessage().setBody("My message")); it allows to override the input to exampleBean, but doesn't avoid its execution.
In the context of your unit test, route2 might be a "mock" component instead. A clean way of achieving that is to declare the route(s) in the properties file. The legibility of the routes gets harder, though.
Then, you could:
#EndpointInject("mock://route2")
MockEndpoint mockSecondStep;
mockSecondStep.whenExchangeReceived(1, e -> {
List whatever = new ArrayList<>();
e.getMessage().setBody(whatever);
});
I solved it using an InterceptStrategy:
public class MyTests extends CamelSpringTestSupport {
#Test
public void testRoute() throws Exception {
CompletableFuture<Object> future = template.asyncSendBody("direct:route1", "Some message");
Object o = future.get();
assertEquals("INTERCEPTED!", o);
}
#Override
protected AbstractApplicationContext createApplicationContext() {
return new ClassPathXmlApplicationContext("spring/gesti-test-application-context.xml");
}
#Override
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
#Override
public void configure() {
context.getProcessorDefinition("bean:exampleBean").addInterceptStrategy(
(context, definition, target, nextTarget) -> exchange -> exchange.getOut().setBody("INTERCEPTED!"));
}
};
}
}
public class ExampleBean {
public String enhance(String message) {
System.out.println(message);
//Here I would call a REST API
return "MY API RESULT";
}
}
I use Apache Camel’s Spring Main to boot my Camel application. I need my application to read the command line arguments to set some parameters. So, I cannot use property files.
At the moment, I can pass arguments via the JVM system properties, and it works well:
Application.java
public class Application extends org.apache.camel.spring.Main {
public static void main(String[] args) throws Exception {
Application app = new Application();
instance = app;
app.run(args);
}
}
camel-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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="shutdownBean" class="com.example.ShutdownBean" />
<camelContext xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="file:{{inputFile}}?noop=true"/>
<to uri="bean:shutdownBean" />
</route>
</camelContext>
</beans>
I run the app with java com.example.Application -DinputFile=C:/absolute/path/to/watch and everything works fine:
…
FileEndpoint INFO Using default memory based idempotent repository with cache max size: 1000
InternalRouteStartupManager INFO Route: route1 started and consuming from: file://C:/absolute/path/to/watch
AbstractCamelContext INFO Total 1 routes, of which 1 are started
…
But I would like to have some input validation and make the app easier to use because -D could be confusing for a non Java user. So I change Application.java:
public class Application extends org.apache.camel.spring.Main {
private File inputFile;
public static void main(String[] args) throws Exception {
Application app = new Application();
instance = app;
app.run(args);
}
public Application() {
addOption(new ParameterOption("i", "inputFile", "The input file", "inputFile") {
#Override
protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
File file = FileUtils.getFile(parameter);
// some business validation
setInputFile(file);
}
});
}
private void setInputFile(File inputFile) {
this.inputFile = inputFile;
}
}
Then, I could use the following command to run the application: java com.example.Application -inputFile C:/absolute/path/to/watch
How can I use my inputFile field into my Camel route?
Call addProperty(String key, String value) in your doProcess method. Then it will be accessible throught {{key}} notation.
MyApplication:
public final class MyApplication extends Main {
private MyApplication() {
super();
addCliOption("g", "greeting", "Greeting");
addCliOption("n", "name", "Who to greet");
}
public static void main(String[] args) throws Exception {
MyApplication app = new MyApplication();
app.configure().addRoutesBuilder(MyRouteBuilder.class);
app.run(args);
}
private void addCliOption(String abbrevation, String parameterName, String description) {
addOption(new ParameterOption(abbrevation, parameterName, description, parameterName) {
protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
addProperty("console." + parameterName, parameter);
}
});
}
}
MyRouteBuilder:
public class MyRouteBuilder extends RouteBuilder {
#Override
public void configure() throws Exception {
from("quartz:foo")
.log("{{console.greeting}} {{console.name}}");
}
}
java org.apache.camel.example.MyApplication -greeting Hello -name Morgan
23:10:25.862 [DefaultQuartzScheduler-MyCoolCamel_Worker-1] INFO route1 - Hello Morgan
23:10:26.832 [DefaultQuartzScheduler-MyCoolCamel_Worker-2] INFO route1 - Hello Morgan
23:10:27.829 [DefaultQuartzScheduler-MyCoolCamel_Worker-3] INFO route1 - Hello Morgan
I have been trying to zip multiple csv file in my route. I have been successfully able to do that.
I am using spring to do the same.
Now the new requirement is to password protect them. Following is the aggregation strategy I have used. How to achieve this?
<route autoStartup="false" routePolicyRef="routeTwoTimer" startupOrder="2" id="zippingFileRoute">
<from uri="{{to.file.processed1}}"/>
<choice id="csvZipFile">
<when>
<simple>$simple{header.CamelFileName} regex '^.*(csv|CSV)$'</simple>
<aggregate strategyRef="zipAggregationStrategy" completionFromBatchConsumer="true" eagerCheckCompletion="true">
<correlationExpression>
<constant>true</constant>
</correlationExpression>
<to uri="{{to.file.processed2}}"/>
</aggregate>
</when>
</choice>
</route>
As pointed in comments, Java API is a bit limited in encrypting ZIP files. Apache Camel ZipAggregationStrategy is using ZipOutputStream, so there is this limitation too. You can implement custom Aggregator using any other library, which allows encryption of Zip files. For example Zip4j
Add Maven dependency
<dependency>
<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j</artifactId>
<version>1.3.2</version>
</dependency>
Implement custom Aggregator
import net.lingala.zip4j.core.ZipFile;
//next few imports. I have added this only to take correct ZipFile class, not the JDK one
public class PasswordZipAggregationStrategy implements AggregationStrategy {
public static final String ZIP_PASSWORD_HEADER = "PasswordZipAggregationStrategy.ZipPassword";
#Override
public Exchange aggregate(Exchange oldExchange, Exchange newExchange){
try {
if (newExchange == null) {
return oldExchange;
}
return aggregateUnchecked(oldExchange,newExchange);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private Exchange aggregateUnchecked(Exchange oldExchange, Exchange newExchange) throws Exception{
ZipFile zipFile;
String password;
if (oldExchange == null) { // first
password = newExchange.getIn().getHeader(ZIP_PASSWORD_HEADER, String.class);
zipFile = new ZipFile(newExchange.getExchangeId()+".zip");
File toDelete = new File(zipFile.getFile().getPath());
newExchange.addOnCompletion(new Synchronization() {
#Override
public void onComplete(Exchange exchange) {
toDelete.delete();
}
#Override
public void onFailure(Exchange exchange) {
}
});
} else {
password = newExchange.getIn().getHeader(ZIP_PASSWORD_HEADER, String.class);
zipFile = new ZipFile(oldExchange.getIn().getBody(File.class));
}
if (password==null){
throw new IllegalStateException("Null password given");
}
ZipParameters zipParameters = new ZipParameters();
zipParameters.setPassword(password);
zipParameters.setEncryptFiles(true);
zipParameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_FAST);
zipParameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);
zipFile.addFile(newExchange.getIn().getBody(File.class), zipParameters);
GenericFile genericFile = FileConsumer.asGenericFile(zipFile.getFile().getParent(), zipFile.getFile(), Charset.defaultCharset().toString(), false);
genericFile.bindToExchange(newExchange);
newExchange.getIn().setBody(zipFile.getFile());
newExchange.getIn().setHeader(ZIP_PASSWORD_HEADER, password);
return newExchange;
}
}
Use it
from("file://in")
.to("log:in")
.setHeader(PasswordZipAggregationStrategy.ZIP_PASSWORD_HEADER, constant("testPassword"))
.aggregate().constant(true).completionFromBatchConsumer()
.aggregationStrategy(new PasswordZipAggregationStrategy())
.to("log:out")
.to("file://out");
I'm creating a application with Spring-jersey-camel. I wanted to expose my jersey layer and internally invoke camel routes to invoke resources.
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
applicationContext.xml
<camelContext id="camelContext" xmlns="http://camel.apache.org/schema/spring">
<packageScan>
<package>com.company.myapp.camel</package>
<excludes>**.*</excludes>
<includes>*Routes.java</includes>
</packageScan>
</camelContext>
MyRoutes.java
#Component
public final class MyRoutes extends RouteBuilder {
#Override
public void configure() throws Exception {
from("direct:getOrdersData").validate(body().isNotNull())
.log("Camel to get orders")
.to("restlet:http://localhost:8081/ordersapp/rest/order/123");
}
}
OrderResourceImpl.java
#Component
#Path("/orderLookup")
public class ReservationResources {
#org.apache.camel.produce
ProducerTemplate producer;
public void setProducer(ProducerTemplate producer) throws Exception {
this.producer = producer;
}
#GET
#Produces(MediaType.APPLICATION_JSON)
#Path("{orderId}")
public Response orderLookup(#PathParam("orderId") final long orderrId){
Response r = Response.noContent().build();
//Producer is null. throws nullPointerException
String order= producer.requestBody("direct:getOrdersData", orderId, String.class);
r = Response.ok().entity(reservation).build();
return r;
}
}
Any idea what I'm doing wrong? or how to inject myRoute/ProducerTemplate im my orderResourceImpl.java. Thanks in advance
Two Options,
If ReservationResources is a spring bean then, Inject the Camel Context into it and create a ProducerTemplate from that
ProducerTemplate template = camelContext.createProducerTemplate();
If ReservationResources is not a spring bean then get the Camel Context via a static method https://stackoverflow.com/a/13633109/3696510 and then create the ProducerTemplate.
ProducerTemplate template = StaticSpringApplicationContext.getBean("camelContext").createProducerTemplate()
Also if you do use that StaticSpringApplicationContext mentioned in the link, I would add this method to it.
public static <T> T getBean(String beanName, Class<T> clazz) {
return (T) CONTEXT.getBean(beanName,clazz);
}
The file process strategy checks databases for specific preprocess conditions/steps were executed before processing a file XYZ.
The files XYZ comes from different sub directories of a root directory that camel scans.
If one of the file is not eligible camel should move to the next one. Not sure what flag should I use if any so it won't bother checking the current file again and again instead of moving to the next one.
My File process Strategy:
public class MigrFileProcessStrategy<T> extends GenericFileProcessStrategySupport<T> {
private static final Logger log = LoggerFactory.getLogger(MigrFileProcessStrategy.class);
#Autowired
DefaultMigrationProcessor defaultMigrationProcessor;
#Override
public boolean begin(GenericFileOperations<T> operations, GenericFileEndpoint<T> endpoint, Exchange exchange, GenericFile<T> file) throws Exception {
//check if HIST table has entry for this filename, if yes all preprocessing done, file is ready.
boolean readyForProcessing = false;
String fileAbsPath = file.getAbsoluteFilePath();
File inMigrFile = new File(fileAbsPath);
readyForProcessing = defaultMigrationProcessor.isFileReadyForProcessing(inMigrFile);
if (!readyForProcessing) {
String msg = String.format("\n####Process?:%b File:%s", readyForProcessing, fileAbsPath);
log.info(msg);
}
return readyForProcessing;
}
}
My Config:
<bean id="migrFilesToCopy" class="org.apache.camel.component.file.MigrFileFilter">
<!-- Filter for migr files that need to be copied-->
<property name="caseSensitive" value="true" />
<property name="excludes" value="**/**/*.migratedLnk, **/_inProgress/**, **/_done/**, **/_failed/**" />
</bean>
<endpoint id="endpoint_migrFilesToCopy"
uri="file://#{migrationProcessor.migrRootDir.toFile()}?processStrategy=#migrFileProcessStrategy&directoryMustExist=true&idempotent=true&recursive=true&delete=true&initialDelay=1000&delay=5000&readLock=changed&readLockTimeout=100000&readLockCheckInterval=1000&moveFailed=_failed&maxMessagesPerPoll=10&filter=#migrFilesToCopy" />
<route id="chMigrate" autoStartup="true">
<from uri="ref:endpoint_migrFilesToCopy" />
<pipeline>
<log message="Procesing file: ${header.CamelFileName}" />
<!--threads executorServiceRef="migrThreadPool" -->
<bean ref="migrationProcessor" method="createMetadata" /><!-- MetaDataVo -->
<bean ref="migrationProcessor" method="createCopyObj" /><!-- CacheCopyObj -->
<bean ref="migrationProcessor" method="migrate" />
<!--/threads -->
</pipeline>
</route>
Resolved by extending ant filter:
public class MigrFileFilter extends AntPathMatcherGenericFileFilter implements GenericFileFilter {
private static final Logger log = LoggerFactory.getLogger(MigrFileFilter.class);
#Autowired
DefaultMigrationProcessor defaultMigrationProcessor;
public MigrFileFilter() {
super();
}
#Override
public boolean accept(GenericFile<T> file) {
String fileAbsPath = file.getAbsoluteFilePath();
File inMigrFile = new File(fileAbsPath);
boolean readyForProcessing = false;
if (Files.isDirectory(inMigrFile.toPath())) {
readyForProcessing = true; //To recursivly process directories.
} else {
boolean validFilePatten = super.accept(file);
boolean preprocessDataExist = false;
if (validFilePatten) {
preprocessDataExist = defaultMigrationProcessor.isFileReadyForProcessing(inMigrFile);
}
readyForProcessing = (validFilePatten && preprocessDataExist);
}
return readyForProcessing;
}
Use the filter to filter out unwanted files
You can implement a custom implementation, and just return true|false if you want to include the file or not.
See the section Filter using org.apache.camel.component.file.GenericFileFilter at
http://camel.apache.org/file2