Apache Camel: Producer template does not add to SEDA endpoint - apache-camel

I am not sure what the problem is with my small application, if it resides on the RouteBuilder or within the ProducerTemplate
Either way, my "Test message" is not logged when running this application.
What may be going wrong here?
public static void main(String[] args) {
Main main = new Main();
main.addRouteBuilder(new RouteBuilder() {
#Override
public void configure() throws Exception {
from("seda:myqueue").log(LoggingLevel.INFO, "${in.body").end();
}
});
ProducerTemplate producerTemplate = main.getOrCreateCamelContext().createProducerTemplate();
producerTemplate.setDefaultEndpointUri("seda:myqueue");
producerTemplate.sendBody("Test message");
}

It doesn't look like you are creating and starting the context which is probably why the message never reaches your route. Here is an example to get you started:
https://examples.javacodegeeks.com/enterprise-java/apache-camel/apache-camel-hello-world-example/
import org.apache.activemq.camel.component.ActiveMQComponent;
import org.apache.camel.CamelContext;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
public class CamelHelloWorldExample {
public static void main(String[] args) throws Exception {
CamelContext context = new DefaultCamelContext();
try {
context.addComponent("activemq", ActiveMQComponent.activeMQComponent("vm://localhost?broker.persistent=false"));
context.addRoutes(new RouteBuilder() {
#Override
public void configure() throws Exception {
from("activemq:queue:test.queue")
.to("stream:out");
}
});
ProducerTemplate template = context.createProducerTemplate();
context.start();
template.sendBody("activemq:test.queue", "Hello World");
Thread.sleep(2000);
} finally {
context.stop();
}
}
}
Notice context.start() and context.stop();

Related

Doing bean inject in camel test

I have a camel app which look something like below which has a route like below:-
from("direct:getMarketplaceOrders").to("bean:orderHelper?method=getMarketplaceOrders");
The entry point of the code look something like below:
public class OrderMainApp {
public static void main(String... args) throws Exception {
OrderMainApp orderMainApp = new OrderMainApp();
DefaultCamelContext camelContext = new DefaultCamelContext();
ProducerTemplate producer = camelContext.createProducerTemplate();
camelContext.setRegistry(orderMainApp.createRegistry(producer));
camelContext.addRoutes(new OrderRouteBuilder(producer));
camelContext.start();
}
protected JndiRegistry createRegistry(ProducerTemplate producer) throws Exception {
JndiRegistry jndi = new JndiRegistry();
OrderHelper orderHelper = new OrderHelper();
orderHelper.setProducer(producer);
jndi.bind("orderHelper", orderHelper);
return jndi;
}
}
In OrderRouteBuilder configure has routes like below:-
//processor is a custom JSONProcessor extending Processor
from("jetty:http://localhost:8888/orchestratorservice").process(processor);
from("direct:getMarketplaceOrders").to("bean:orderHelper?method=getMarketplaceOrders");
My goal is to test the response I receive from bean:orderHelper?method=getMarketplaceOrders when I place a request on direct:getMarketplaceOrders
orderHelper.getMarketplaceOrders looks like below:-
public OrderResponse getMarketplaceOrders(GetMarketplaceOrdersRequest requestParam) throws Exception
My test class look something like below:-
public class OrderMainAppTest extends CamelTestSupport {
#Produce(uri = "direct:getMarketplaceOrders")
protected ProducerTemplate template;
#EndpointInject(uri = "bean:orderHelper?method=getMarketplaceOrders")
protected MockEndpoint resultEndpoint;
#Test
public void testSendMatchingMessage() throws Exception {
String expectedBody = "<matched/>";
template.sendBody("{\"fromDateTime\": \"2016-01-11 10:12:13\"}");
resultEndpoint.expectedBodiesReceived(expectedBody);
resultEndpoint.assertIsSatisfied();
}
#Override
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
#Override
public void configure() {
from("direct:getMarketplaceOrders").to("bean:orderHelper?method=getMarketplaceOrders");
}
};
}
}
Whenever I am running the test I am getting the below exception:-
java.lang.IllegalArgumentException: Invalid type: org.apache.camel.component.mock.MockEndpoint which cannot be injected via #EndpointInject/#Produce for: Endpoint[bean://orderHelper?method=getMarketplaceOrders]
I am guessing this is because I am not able to pass on OrderHelper to the camel test context. Can some one let me know how can I inject the bean in the mock result end point?
EDIT:-
I tried modifying my test class as follows:-
public class OrderMainAppTest extends CamelTestSupport {
protected OrderHelper orderHelper = new OrderHelper();
#Produce(uri = "direct:getMarketplaceOrders")
protected ProducerTemplate template;
#EndpointInject(uri = "mock:intercepted")
MockEndpoint mockEndpoint;
#Before
public void preSetup() throws Exception {
orderHelper.setProducer(template);
};
#Test
public void testSendMatchingMessage() throws Exception {
GetMarketplaceOrdersRequest request = new GetMarketplaceOrdersRequest();
request.setFromDateTime("2016-01-11 10:12:13");
request.setApikey("secret_key");
request.setMethod("getMarketplaceOrders");
request.setLimit(10);
request.setOffset(2);
template.sendBody(request);
mockEndpoint.expectedBodiesReceived("{\"success\":\"false\"");
}
#Override
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
#Override
public void configure() {
interceptSendToEndpoint("bean:orderHelper?method=getMarketplaceOrders")
.to("mock:intercepted"); from("direct:getMarketplaceOrders").to("bean:orderHelper?method=getMarketplaceOrders");
}
};
}
#Override
protected JndiRegistry createRegistry() throws Exception {
return getRegistry();
}
protected JndiRegistry getRegistry() {
JndiRegistry jndi = new JndiRegistry();
jndi.bind("orderHelper", orderHelper);
return jndi;
}
}
The above code is making the request correctly and is flowing through my app correctly. But I am not able to intercept the response of orderHelper.getMarketplaceOrders. The above code is intercepting only the request. I tried changing to template.requestBody(request). But still no luck.
This error means you can't inject a bean: endpoint into a MockEndpoint.
If you want to "intercept" the call into your OrderHelper, you can use interceptSendToEndpoint in your route :
#EndpointInject(uri = "mock:intercepted")
MockEndpoint mockEndpoint;
...
#Override
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
#Override
public void configure() {
interceptSendToEndpoint("bean:orderHelper?method=getMarketplaceOrders")
.to("mock:intercepted");
from("direct:getMarketplaceOrders")
.to("bean:orderHelper?method=getMarketplaceOrders");
}
};
See : http://camel.apache.org/intercept.html
By updating my createRouteBuilder as shown below. I am able to intercept the response and send it to a mock endpoint where I can do the assertion.
#Override
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
#Override
public void configure() {
from("direct:getMarketplaceOrders").to("bean:orderHelper?method=getMarketplaceOrders").onCompletion()
.to("mock:intercepted");
}
};
}

Unable to execute OS Command from Camel

I'm trying to execute OS commands using Camel's exec Component. Unfortunately I don't see any output of the Commands executed. Here is my code which contains a simple exec taken from the Documentation:
public class CamelExampleTest extends CamelTestSupport {
#Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
#Override
public void configure() throws Exception {
from("direct:startpoint").id("route1") //
.to("exec:wc?args=--words /usr/share/dict/words")//
.convertBodyTo(String.class) //
.process(new Processor() {
#Override
public void process(Exchange exchng) throws Exception {
String body = exchng.getIn().getBody(String.class);
System.out.println(body);
}
}).to("mock:endpoint");
}
};
}
#Test
public void test() throws InterruptedException {
System.out.println("running test");
MockEndpoint resultEndpoint = context.getEndpoint("mock:endpoint", MockEndpoint.class);
}
}
Any apparent mistake in my code?
You send nothing to direct:startpoint.
Try something like this:
#Test
public void test() throws InterruptedException {
System.out.println("running test");
MockEndpoint resultEndpoint = context.getEndpoint("mock:endpoint", MockEndpoint.class);
context.createProducerTemplate().sendBody("direct:startpoint","Hello world");
}

Camel test using Intercept

I am trying to intercept a message to skip the Http request and proceed with my route.
Below is the class you can copy/paste to try it out.
Using camel-test, camel-core, camel-http4 2.10.2 and httpclient-osgi, httpcore-osgi 4.2.2
Here is the code :
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.AdviceWithRouteBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;
/**
* Created with IntelliJ IDEA.
* User: lleclerc
* Date: 12-11-28
* Time: 16:34
* To change this template use File | Settings | File Templates.
*/
public class IsUseAdviceWithJUnit4Test extends CamelTestSupport {
private String providerEndPointURI = "http://stackoverflow.com";
private String timerEndPointURI = "timer://myTimer";
private String mockEndPointURI = "mock:myMock";
private String directEndPointURI = "direct:myDirect";
private boolean messageIntercepted;
#Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
#Override
public void configure() throws Exception {
from(timerEndPointURI + "?fixedRate=true&delay=1000&period=1000")
.to(providerEndPointURI + "?throwExceptionOnFailure=false")
.to(mockEndPointURI);
}
};
}
#Test
public void testIsUseAdviceWith() throws Exception {
messageIntercepted = false;
context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {
#Override
public void configure() throws Exception {
replaceFromWith(directEndPointURI);
mockEndpoints();
interceptSendToEndpoint(providerEndPointURI)
.skipSendToOriginalEndpoint()
.process(new Processor() {
#Override
public void process(Exchange exchange) throws Exception {
messageIntercepted = true;
System.out.println("INTERCEPTED");
}
});
}
});
// we must manually start when we are done with all the advice with
context.start();
getMockEndpoint(mockEndPointURI).expectedMessageCount(1);
template.sendBody(directEndPointURI, "a trigger");
assertMockEndpointsSatisfied();
assertEquals(true, messageIntercepted);
assertNotNull(context.hasEndpoint(directEndPointURI));
assertNotNull(context.hasEndpoint("mock:" + directEndPointURI));
assertNotNull(context.hasEndpoint(mockEndPointURI));
}
#Override
public boolean isUseAdviceWith() {
return true;
}
#Override
public boolean isUseRouteBuilder() {
return true;
}
}
Thank you for your help !
There was bugs inside camel-http4.
http://camel.465427.n5.nabble.com/Found-a-bug-with-camel-http4-td5723733.html
http://camel.465427.n5.nabble.com/Test-Intercept-with-adviceWith-and-http-td5723473.html

Apache camel error: The method process is not applicable for the arguments

I have written a simple route, which would get any http request and save it in file:output.
Once saved a processor is created which would read all the requests.
Here is my code:
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.impl.DefaultCamelContext;
public class LoadBalancer {
public static void main(String args[]) throws Exception {
CamelContext context = new DefaultCamelContext();
context.addRoutes(new RouteBuilder() {
public void configure() {
from("jetty://http://localhost:8080")
.to("file:output");
from("file://output").process(new processor()
{
public void process(Exchange e)
{
System.out.println("Recieved exchange:" + e.getIn());
}
}
);
//.loadBalance().roundRobin().to("http://172.28.39.138:8080","http://172.168.20.118:8080");
}
});
context.start();
Thread.sleep(100000);
context.stop();
}
}
Now when i compile it, i get the following error:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method process(Processor) in the type ProcessorDefinition<RouteDefinition> is not applicable for the arguments (new processor(){})
processor cannot be resolved to a type
On the line `from("file://output").process(new processor()`
I couldn't figure out what kind of error it it.
Am I doing anything wrong in the code?
Any help would be very much appreciated.
Cheers!!
inlined Processors should be written like this...
from("file://output").process(new Processor() {
public void process(Exchange exchange) throws Exception {
System.out.println("Recieved exchange:" + e.getIn());
}
});

Stop/Remove Route Dynamically/Programmatically Doesn't Remove the Corresponding Thread

During the processing of an Exchange received from JMS I'm creating dynamically a route that fetches a file from FTP to the file system and when the batch is done I need to remove that same route. The following code fragment shows how I do this:
public void execute() {
try {
context.addRoutes(createFetchIndexRoute(routeId()));
} catch (Exception e) {
throw Throwables.propagate(e);
}
}
private RouteBuilder createFetchIndexRoute(final String routeId) {
return new RouteBuilder() {
#Override
public void configure() throws Exception {
from("ftp://" + getRemoteQuarterDirectory() +
"?fileName=" + location.getFileName() +
"&binary=true" +
"&localWorkDirectory=" + localWorkDirectory)
.to("file://" + getLocalQuarterDirectory())
.process(new Processor() {
RouteTerminator terminator;
#Override
public void process(Exchange exchange) throws Exception {
if (camelBatchComplete(exchange)) {
terminator = new RouteTerminator(routeId,
exchange.getContext());
terminator.start();
}
}
})
.routeId(routeId);
}
};
}
I'm Using a thread to stop a route from a route, which is an approach recommended in the Camel Documentation - How can I stop a route from a route
public class RouteTerminator extends Thread {
private String routeId;
private CamelContext camelContext;
public RouteTerminator(String routeId, CamelContext camelContext) {
this.routeId = routeId;
this.camelContext = camelContext;
}
#Override
public void run() {
try {
camelContext.stopRoute(routeId);
camelContext.removeRoute(routeId);
} catch (Exception e) {
throw Throwables.propagate(e);
}
}
}
In result the route does stop. But what I see in the jconsole is that the thread that corresponds to the route isn't removed. Thus in time these abandoned threads just keep accumulating.
Is there a way to properly stop/remove a route dynamically/programmatically and also to release the route's thread, so that they don't accumulate through time?
This is fixed in the next Camel release 2.9.2 and 2.10. Fixed by this ticket:
https://issues.apache.org/jira/browse/CAMEL-5072

Resources