RecipientList with RequestReply Using Apache Camel - apache-camel

I think that I have a gap in my understanding of the RecipientList . My understanding is that RecipientList EIP can be used to represent dynamic destinations. I am attempting to use is with the RequestReply EIP but I am getting some strange results.
The code below is a unit test for RequestReply and aggregation of replies back to the sender. Message arrives at incomingMessages1-update, gets routed to outgoingMessages-[123]-update queues. The results come back on outgoingMessages-[123]-reply queues. The results are aggregated and sent back on incomingMessages1-reply queue.
See below a unit test that works:
public class AggregateStrategyTestOnMultipleReplyQueues extends CamelTestSupport {
#Test
public void testRequestReplyWithRecipientListAndCustomGather()
throws Exception {
int numberOfMessages = 5;
getMockEndpoint("mock:end").setExpectedMessageCount(numberOfMessages);
context.addRoutes(new RouteBuilder() {
public void configure() throws Exception {
from("jms:incomingMessages1-update")
.multicast(new GatherResponses())
.to("jms:outgoingMessages1-update?exchangePattern=InOut&replyTo=queue:outgoingMessages1-reply&preserveMessageQos=true") //1
.to("jms:outgoingMessages2-update?exchangePattern=InOut&replyTo=queue:outgoingMessages2-reply&preserveMessageQos=true") //2
.to("jms:outgoingMessages3-update?exchangePattern=InOut&replyTo=queue:outgoingMessages3-reply&preserveMessageQos=true") //3
.to("mock:end");
//this is what the adapters will be doing
from("jms:outgoingMessages1-update").setBody(constant("Hello World")).to(
"mock:end");
from("jms:outgoingMessages2-update").setBody(constant("Welcome World")).to(
"mock:end");
from("jms:outgoingMessages3-update").setBody(constant("Hi World")).to(
"mock:end");
}
});
String messageSent = "Message sent from template";
Object response = template
.requestBodyAndHeader(
"jms:incomingMessages1-update?exchangePattern=InOut&preserveMessageQos=true",
messageSent, "JMSReplyTo", "incomingMessages1-reply");
assertEquals("Hello World" + " "+ "Welcome World"+ " "+ "Hi World"+ " " + messageSent ,
response);
}
private class GatherResponses implements AggregationStrategy {
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
if (oldExchange == null) {
return newExchange;
}
String oldBody = oldExchange.getIn().getBody(String.class);
String newBody = newExchange.getIn().getBody(String.class);
String body = oldBody + " " + newBody;
oldExchange.getIn().setBody(body);
return oldExchange;
}
}
}
I attempted to change the code above (//1, //2 and //3 to a recipient list like below) and it didn't work:
from("jms:incomingMessages1-update")
.recipientList(header("myRecipientList")).aggregationStrategy(new GatherResponses()).parallelProcessing().end()
.to("mock:end");
I loaded the URIs like this:
List<String> recipientList = new ArrayList<String>();
recipientList.add("jms:outgoingMessages1-update?exchangePattern=InOut&replyTo=queue:outgoingMessages1-reply&preserveMessageQos=true");
recipientList.add("jms:outgoingMessages2-update?exchangePattern=InOut&replyTo=queue:outgoingMessages1-reply&preserveMessageQos=true");
recipientList.add("jms:outgoingMessages3-update?exchangePattern=InOut&replyTo=queue:outgoingMessages1-reply&preserveMessageQos=true");
Map<String, Object> headers = new HashMap<String, Object>();
headers.put("JMSReplyTo", "incomingMessages1-reply");
headers.put("myRecipientList", recipientList);
I am getting the original message back and I am not seeing the reply queues created. Can you please point me to what I am missing?

You cannot send a List/Map etc as JMS headers. The JMS spec does not allow that.
See section Message format when sending at
http://camel.apache.org/jms
And also the JMS spec / api / javadoc etc.
You can instead store the values in a String separated by comma. The Camel recipient list will automatic use comma as delimiter, so that should then work out of the box.

Related

Apache Flink side-output not outputing exected results when order of processors swapped in the original stream

I have a small Flink app:
public class App {
public static final OutputTag<String> numberOutputTag = new OutputTag<String>("side-output") {
};
public static void main(String[] args) throws Exception {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
DataStreamSource<String> text = env.fromElements(
"abc,123"
);
// Router will split input on commas and redirect number strings to the side output
SingleOutputStreamOperator<String> ingestStream = text
.process(new RouterProcessor())
.process(new UppercaseProcessor())
;
DataStream<String> numberStream = ingestStream.getSideOutput(numberOutputTag)
// Prepends a "$" to the values.
.map(new MoneyMapper());
numberStream.print();
ingestStream.print();
env.execute();
}
}
class RouterProcessor extends ProcessFunction<String, String> {
#Override
public void processElement(String value, Context ctx, Collector<String> out) throws Exception {
String[] tokens = value.split(",");
for (String token : tokens) {
if (token.matches("[0-9]+")) {
ctx.output(App.numberOutputTag, token);
} else {
out.collect(token);
}
}
}
}
class MoneyMapper implements MapFunction<String, String> {
#Override
public String map(String t) throws Exception {
return "$" + t;
}
}
class UppercaseProcessor extends ProcessFunction<String, String> {
#Override
public void processElement(String value, Context ctx, Collector<String> out) throws Exception {
out.collect(value.toUpperCase());
}
}
I'd expect it to output something similar to:
18> ABC
18> $123
However, it only outputs:
10> ABC
If I swap the order of the processors to:
.process(new UppercaseProcessor())
.process(new RouterProcessor())
everything works as expected.
I've read the documentation but I don't see anything that would explain why this is as it is. I'm curious if I'm missing something or doing something wrong.
I've included a GitHub jist here for easier viewing with all the supporting files: https://gist.github.com/baelec/95f41d875dda0a2806a0fb9b9313b90e
Here is a repo if you'd prefer to download the sample project: https://github.com/baelec/flink_sample_broken_0
EDIT: I see that StackOverflow asks us to avoid comments like "Thanks!" but I don't have enough rep to visibly upvote the responses so thanks David and Jaya for your help. I had made some incorrect assumptions regarding side outputs. I appreciate the clarification.
The problem is that you are taking the side output from the UppercaseProcessor, which doesn't use a side output.
It's easier to see what's wrong if you look at the job graph, which looks like this:
If you rearrange the code to be like this:
SingleOutputStreamOperator<String> ingestStream = text
.process(new RouterProcessor());
DataStream<String> numberStream = ingestStream.getSideOutput(numberOutputTag)
.map(new MoneyMapper());
numberStream.print();
ingestStream
.process(new UppercaseProcessor())
.print();
then it works as you expected, and the job graph has become this:
numberOutputTag side output emit logic happens inside RouterProcessor. So you need to extract the side output from the SingleOutputStreamOperator returned by the RouterProcessor process function. But in your code, your side output logic extraction happens after the UppercaseProcessor function.
Change something like below,
SingleOutputStreamOperator<String> tempStream = text.process(new RouterProcessor());
SingleOutputStreamOperator<String> ingestStream = tempStream.process(new UppercaseProcessor());
DataStream<String> numberStream = tempStream.getSideOutput(numberOutputTag).map(new MoneyMapper());
numberStream.print();
ingestStream.print();
Note: Check the usage of tempStream variable in the above example.

Hystrix Circuit breaker not opening the circuit

I am implementing Circuit breaker using Hystrix in my Spring boot application, my code is something like below:
#service
public class MyServiceHandler {
#HystrixCommand(fallbackMethod="fallback")
public String callService() {
// if(remote service is not reachable
// throw ServiceException
}
public String fallback() {
// return default response
}
}
// In application.properties, I have below properties defined:
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=10000
hystrix.command.default.circuitBreaker.requestVolumeThreshold=3
hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds=30000
hystrix.threadpool.default.coreSize=4
hystrix.threadpool.default.metrics.rollingStats.timeInMilliseconds=200000
I see that the fallback() is getting called with each failure of callService(). However, the circuit is not opening after 3 failures. After 3 failures, I was expecting that it will directly call fallback() and skip callService(). But this is not happening. Can someone advise what I am doing wrong here?
Thanks,
B Jagan
Edited on 26th July to add more details below:
Below is the actual code. I played a bit further with this. I see that the Circuit opens as expected on repeated failured when I call the remote service directly in the RegistrationHystrix.registerSeller() method. But, when I wrap the remote service call within Spring retry template, it keeps going into fallback method, but circuit never opens.
#Service
public class RegistrationHystrix {
Logger logger = LoggerFactory.getLogger(RegistrationHystrix.class);
private RestTemplate restTemplate;
private RetryTemplate retryTemplate;
public RegistrationHystrix(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
retryTemplate = new RetryTemplate();
FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
fixedBackOffPolicy.setBackOffPeriod(1000l);
retryTemplate.setBackOffPolicy(fixedBackOffPolicy);
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
retryPolicy.setMaxAttempts(3);
retryTemplate.setRetryPolicy(retryPolicy);
}
#HystrixCommand(fallbackMethod = "fallbackForRegisterSeller", commandKey = "ordermanagement")
public String registerSeller(SellerDto sellerDto) throws Exception {
String response = retryTemplate.execute(new RetryCallback<String, Exception>() {
#Override
public String doWithRetry(RetryContext context) {
logger.info(String.format("Retry count %d", context.getRetryCount()));
return restTemplate.postForObject("/addSeller", sellerDto, String.class);
}
});
return response;
}
public List<SellerDto> getSellersList() {
return restTemplate.getForObject("/sellersList", List.class);
}
public String fallbackForRegisterSeller(SellerDto sellerDto, Throwable t) {
logger.error("Inside fall back, cause - {}", t.toString());
return "Inside fallback method. Some error occured while calling service for seller registration";
}
}
Below is the service class which in turn calls the above Hystrix wrapped service. This class in turn is invoked by a controller.
#Service
public class RegistrationServiceImpl implements RegistrationService {
Logger logger = LoggerFactory.getLogger(RegistrationServiceImpl.class);
private RegistrationHystrix registrationHystrix;
public RegistrationServiceImpl(RegistrationHystrix registrationHystrix) {
this.registrationHystrix = registrationHystrix;
}
#Override
public String registerSeller(SellerDto sellerDto) throws Exception {
long start = System.currentTimeMillis();
String registerSeller = registrationHystrix.registerSeller(sellerDto);
logger.info("add seller call returned in - {}", System.currentTimeMillis() - start);
return registerSeller;
}
So, I am trying to understand why the Circuit breaker is not working as expected when using it along with Spring RetryTemplate.
You should be using metrics.healthSnapshot.intervalInMilliseconds while testing. I guess you are executing all 3 request within default 500 ms and hence the circuit isn't getting open. You can either decrease this interval or you may put a sleep between the 3 requests.

Best way to unmashal an object to a POJO in Apache Camel

I am trying to retrieve an object out of the process method in Camel response.
However once I got an empty response in the following code:
from("timer://simpleTimer?repeatCount=1").routeId("myroute")
.setHeader("client_id", constant("abc"))
.setHeader("client_secret",constant("def"))
.setHeader(Exchange.HTTP_METHOD, constant("POST"))
.setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
.setHeader(Exchange.HTTP_URI, constant(url))
.marshal().json(JsonLibrary.Gson)
.log("trying to send message")
.to(url)
.convertBodyTo(String.class)
.process(new Processor(){
#Override
public void process(Exchange exchange) throws Exception {
final Message message = exchange.getIn();
int responseCode = message.getHeader(Exchange.HTTP_RESPONSE_CODE, Integer.class);
final String responseBody = message.getBody(String.class);
System.out.println("in final block of process:" +
responseCode + ",Body class name=" + responseBody.getClass()+
"body="+responseBody);
}
);
Here body is not being printed.This is strange as the body is being printed
I need a JSON representation back and also want to store it in a object so that we can return it while returning thi object from this method.
Is there something missing?What should be added to meet the requirement?

Changing apache camel message type to InOut

From what I understand, an InOut message is one where a response can be received from the destination.
However, I have not been able to find any example of how to convert a message to InOut type, and how to access the response from the destination
For example, given a route like:
from("direct:start").to("smtps://smtp.gmail.com:465?username=user#gmail.com&password=usrpw&to=address#gmail.com")
How to convert the message routed to smtps component into InOut type?
Can I expect a response from the smtp component, e.g. indicating that the message was sent successfully?
how to access this response?
By default, each ".to(uri)" are in InOut. The body in the next step will be replaced by the response of the InOut destination. For example, in HTTP component, if you have the following route :
from(direct:start)
.to(http://...)
.log(INFO, "${body}")
The response to the http call will be logged.
If you don't find good informations in the document, I highly recommend you to check the code of the related producer to know what's returned or can be used.
https://github.com/apache/camel
For example, for SMTP, I haven't found the doc saying what's happening to the body, but the code is pretty much clear :
public void process(final Exchange exchange) {
ClassLoader tccl = Thread.currentThread().getContextClassLoader();
try {
ClassLoader applicationClassLoader = getEndpoint().getCamelContext().getApplicationContextClassLoader();
if (applicationClassLoader != null) {
Thread.currentThread().setContextClassLoader(applicationClassLoader);
}
MimeMessage mimeMessage;
final Object body = exchange.getIn().getBody();
if (body instanceof MimeMessage) {
// Body is directly a MimeMessage
mimeMessage = (MimeMessage) body;
} else {
// Create a message with exchange data
mimeMessage = new MimeMessage(sender.getSession());
getEndpoint().getBinding().populateMailMessage(getEndpoint(), mimeMessage, exchange);
}
if (LOG.isDebugEnabled()) {
LOG.debug("Sending MimeMessage: {}", MailUtils.dumpMessage(mimeMessage));
}
sender.send(mimeMessage);
// set the message ID for further processing
exchange.getIn().setHeader(MailConstants.MAIL_MESSAGE_ID, mimeMessage.getMessageID());
} catch (MessagingException e) {
exchange.setException(e);
} catch (IOException e) {
exchange.setException(e);
} finally {
Thread.currentThread().setContextClassLoader(tccl);
}
}
Your exchange will have an header with the MAIL_ID ("CamelMailMessageId"), and in case of any messaging exception, the exception will be propagated. The body seems to be left untouched, even if it's InOut.

Salesforce: Trying to deliver a CSV from a scheduled job

I read where someone was able to do this, but I'm having a hard time getting it to work.
Basically, I'm scheduling an HTTP callout to a page that has a controller that builds a CSV and emails it to a recipient.
The Scheduled class:
global class ReportExporter implements System.Schedulable {
global void execute(SchedulableContext sc) {
getmailReportOutput ex = new getmailReportOutput();
ex.exportCSV();
}
}
The getEmailReportOutput class:
public class getmailReportOutput{
public Static String strSessionID;
public getmailReportOutput() {
}
public void exportCSV() {
makeReportRequest();
}
#future (callout=true)
public Static void makeReportRequest() {
strHost ='c.cs4.visual.force.com';
strSessionID = UserInfo.getSessionId();
String requestUrl = 'https://' + strHost + '/apex/TestSendReport#';
HttpRequest req = new HttpRequest();
req.setEndpoint(requestUrl);
req.setMethod('GET');
req.setHeader('Cookie','sid=' + strSessionID );
String output = new Http().send(req).getBody();
System.debug('HTTP RESPONSE RETURNED: ' + output);
}
}
The getEmailReportOutput class does an HTTP Callout to a VF page: I make sure to send the sessionID with the request:
And the "TestSendReport" is just a simple callout to a controller:
<apex:page controller="Exporter" action="{!runrpt}">
</apex:page>
...And the controller is calling the report content:
public class Exporter {
public static Boolean isTest;
public static String strEmailAddr;
public void runrpt() {
executeRpt();
}
#future
public static void executeRpt() {
System.debug('CALLING REPORT EXPORTER...');
String ReportName__c = '00OP0000000Jp3N';
String strEmailAddr = 'myname#email.com';
ApexPages.PageReference report = new ApexPages.PageReference( '/' + RptName__c + '?csv=1');
Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
attachment.setFileName('report.csv');
attachment.setBody(report.getContent());
attachment.setContentType('text/csv');
Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
message.setFileAttachments(new Messaging.EmailFileAttachment[] { attachment } );
message.setSubject('Report');
message.setPlainTextBody('The report is attached.');
message.setToAddresses( new String[] { strEmailAddr } );
Messaging.sendEmail( new Messaging.SingleEmailMessage[] { message } );
}
}
...Any ideas? The debug logs show all is well, but nothing is received. I know this is a wall of code, but it seems to be what people recommend to accomplish the task - I just can't see anything wrong.
I don't see anything obviously missing here :/
Just to be safe - getEmailReportOutput & getmailReportOutput are the same class (typo error in the post, not in your actual code)?
This looks like jumping a lot of hops, do I read it correctly that it's scheduled class -> REST callout -> VF page with action -> #future -> send an email? Geez, a lot can go wrong here ;) I've read somewhere that SF will keep some kind of reference counter and calling out to same instance might block you from using page.getContent...
Can you see the report body System.debug(report.getContent().toString());? Can you try saving this email as task for your own user or under a sample Account for example (setSaveAsActivity())?
As blatant plug as it is - I've used different path to solve similar requirement. Check out https://salesforce.stackexchange.com/questions/4303/scheduled-reports-as-attachment and see if you can get it to work?

Resources