Strange output from FuseESB - java

I am working on parsing the request.
I developed route in Java for parsing incoming request.
I am using Camel 2.9 with FuseESB 7.0.1.0.84.
I used simple(“{body}”).getText() to fetch incoming request as per Camel Manual
So I am checking the incoming request by using the code as:
if (xmlStringToParse == null || xmlStringToParse.equals("") || xmlStringToParse.equals("${body}")) {
parsedXMLPath = "<error>Incoming request is as folows:"
+ "\nValue of xmlStringToParse: " + xmlStringToParse
+ "\n xmlStringToParse is empty: " + (xmlStringToParse.equals(""))
+ "\n xmlStringToParse equals ${body}: " + (xmlStringToParse.equals("${body}"))
+ "\nAgain checking incoming request:\n" + xmlStringToParse
+ "</error>";
}
Where xmlStringToParse = simple(“${body}”).getText()
The strange outcoming observed:
Value of xmlStringToParse is changed in just one line from soap request to "". Also “xmlStringToParse equals ${body} “ is printed as “xmlStringToParse equals” without printing ${body}. ${body} is not printed in logs.
You can find the log output follows:
<error>
Value of xmlStringToParse: <somesoapRequest>
xmlStringToParse is empty: false
xmlStringToParse equals : true
Again checking incoming request:
</error>
Can anyone tell me how to solve this issue and the reason for this strange behavior?

I used simple(“{body}”).getText() to fetch incoming request as per Camel Manual
Where did you see that? Do you have a link?
You should get the message body in another way than what you do, such as
String body = exchange.getIn().getBody(String.class);
Or if you use bean parameter binding, you can bind the message body and just declare the parameter to be of String type
public void foo(String body) {
...
}
See more details at the Camel docs such at: http://camel.apache.org/bean-binding.html

Related

camel pollEnrich is not working for the second time

I am reading and processing 2 files from 2 different file locations and comparing the content.
If 2nd file is not available , the rest of the process execute with 1st file. If 2nd file is available, comparison process should happen. For this I am using camel pollEnrich, but here the problem is that, camel is picking the 2nd file at first time only. Without restarting the camel route 2nd file is not getting picked up even if it is present there.
After restarting the camel route it is working fine, but after that its not picking the 2nd file.
I am moving the files to different locations after processing it.
Below is my piece of code,
from("sftp:" + firstFileLocation + "?privateKeyFile=" + ppkFileLocation + "&username=" + sftpUsername
+ "&readLock=changed&idempotent=true&move=" + firstFileArchiveLocation)
.pollEnrich("sftp:" + secondFileLocation + "?privateKeyFile=" + ppkFileLocation + "&username=" + sftpUsername
+ "&readLock=changed&idempotent=true&fileExist=Ignore&move="+ secondFileLocationArchive ,10000,new FileAggregationStrategy())
.routeId("READ_INPUT_FILE_ROUTE")
Need help.
You're setting idempotent=true in the sftp consumer, which means camel will not process the same file name twice. Since you're moving the files, it would make sense to set idempotent=false.
Quoted from camel documentation
Option to use the Idempotent Consumer EIP pattern to let Camel skip
already processed files. Will by default use a memory based LRUCache
that holds 1000 entries. If noop=true then idempotent will be enabled
as well to avoid consuming the same files over and over again.
I'm adding an alternative solution based on comments for the answer posted by Jeremy Ross. My answer is based on the following code example. I've only added the configure() method in the test route for brevity.
#Override
public void configure() throws Exception {
String firstFileLocation = "//127.0.0.1/Folder1";
String secondFileLocation = "//127.0.0.1/Folder2";
String ppkFileLocation = "./key.pem";
String sftpUsername = "user";
String sftpPassword = "xxxxxx";
String firstFileArchiveLocation = "./Archive1";
String secondFileLocationArchive = "./Archive2";
IdempotentRepository repository1 = MemoryIdempotentRepository.memoryIdempotentRepository(1000);
IdempotentRepository repository2 = MemoryIdempotentRepository.memoryIdempotentRepository(1000);
getCamelContext().getRegistry().bind("REPO1", repository1);
getCamelContext().getRegistry().bind("REPO2", repository2);
from("sftp:" + firstFileLocation
+ "?password=" + sftpPassword + "&username=" + sftpUsername
+ "&readLock=idempotent&idempotent=true&idempotentKey=\\${file:name}-\\${file:size}-\\${file:modified}" +
"&idempotentRepository=#REPO1&stepwise=true&download=true&delay=10&move=" + firstFileArchiveLocation)
.to("direct:combined");
from("sftp:" + secondFileLocation
+ "?password=" + sftpPassword + "&username=" + sftpUsername
+ "&readLock=idempotent&idempotent=true&idempotentKey=\\${file:name}-\\${file:size}-\\${file:modified}" +
"&idempotentRepository=#REPO2" +
"&stepwise=true&delay=10&move=" + secondFileLocationArchive)
.to("direct:combined");
from("direct:combined")
.aggregate(constant(true), (oldExchange, newExchange) -> {
if (oldExchange == null) {
oldExchange = newExchange;
}
String fileName = (String) newExchange.getIn().getHeaders().get("CamelFileName");
String filePath = (String) newExchange.getIn().getHeaders().get("CamelFileAbsolutePath");
if (filePath.contains("Folder1")) {
oldExchange.getIn().setHeader("File1", fileName);
} else {
oldExchange.getIn().setHeader("File2", fileName);
}
String file1Name = oldExchange.getIn().getHeader("File1", String.class);
String file2Name = oldExchange.getIn().getHeader("File2", String.class);
if (file1Name != null && file2Name != null) {
// Compare files
// Both files are available
oldExchange.getIn().setHeader("PROCEED", true);
} else if (file1Name != null) {
// No comparison, proceed with File 1
oldExchange.getIn().setHeader("PROCEED", true);
} else {
// Do not proceed, keep file 2 data and wait for File 1
oldExchange.getIn().setHeader("PROCEED", false);
}
String fileName1 = oldExchange.getIn().getHeader("File1", String.class);
String fileName2 = oldExchange.getIn().getHeader("File2", String.class);
oldExchange.getIn().setBody("File1: " + fileName1 + " File2: " + fileName2);
System.out.println(oldExchange);
return oldExchange;
}).completion(exchange -> {
if(exchange.getIn().getHeader("PROCEED", Boolean.class)) {
exchange.getIn().removeHeader("File1");
exchange.getIn().removeHeader("File2");
return true;
}
return false;
}).to("log:Test");
}
In this solution, two SFTP consumers were used, instead of pollEnrich, since we need to capture the file changes of both SFTP locations. I have used an idempotent repository and an idempotent key for ignoring duplicates. Further, I've used the same idempotent repository as the lock store assuming only camel routes are accessing the files.
After receiving the files from SFTP consumers, they are sent to the direct:combined producer, which then routes the exchange to an aggregator.
In the example aggregator strategy I have provided, you can see, that the file names are being stored in the exchange headers. According to the file information retrieved from the headers, the aggregator can decide how to process the file and whether or not to proceed with the exchange. (If only file2 is received, the exchange should not proceed to the next stages/routes)
Finally, the completion predicate expression decides whether or not to proceed with the exchange and log the exchange body, based on the headers set by the aggregator. I have added an example clean-up process in the predicate expression processor as well.
Hope you will get the basic idea of my suggestion to use an aggregator from this example.

Apache Camel's load balanced route doesn't work if one of the endpoint stops connecting

I have a scenario in which if my endpoint1 is down, all messages should be routed to endpoint2 or vice versa. In case both are up then messages should be sent in round robin fashion. Can someone please give some idea how to handle this scenario.
from(itemFileConfig.getWorkingDir())
.log("Entered into file consumption part::")
.autoStartup(true)
.process(fileProcessor)
.split(body().tokenize("\n"))
.loadBalance()
.roundRobin()
.to("direct:kafkaPosting1", "direct:kafkaPosting2")
.end();
from("direct:kafkaPosting1")
.to("kafka:" + config.getTopicName() + "?" + "brokers=" +
config.getBoostStapServers1() + "&" +"serializerClass=" +
config.getSerializer())
.end();
from("direct:kafkaPosting2")
.to("kafka:" + config.getTopicName() + "?" + "brokers=" +
config.getBoostStapServers2() + "&" +"serializerClass=" +
config.getSerializer())
.end();
Thanks in advance
// use load balancer with failover strategy
// 1 = which will try 1 failover attempt before exhausting
// false = do not use Camel error handling
// true = use round robin mode
.loadBalance().failover(1, false, true)
.to("direct:kafkaPosting1").to("direct:kafkaPosting2");

500 Internal Server Error instead of 404 in Spring Boot

When I try to find out the value that is not there in the database I get 500 Internal Server Error. I have already provided logic to throw ResourceNotFoundException error, but, it's not working for some reason. What do I need to do to get 404 ResourceNotFoundException instead of 500 Internal Server Error.
Here's my code:
#PostMapping("/start/{id}")
public ResponseEntity<String> startEvent(#PathVariable() Long id) {
Event event = this.eventRepository.findById(id).get();
if (event == null) {
throw new ResourceNotFoundException("Event with id " + id + " not found.");
}
event.setStarted(true);
this.eventRepository.save(event);
return ResponseEntity.ok("Event " + event.getName() + " has started");
}
I guess eventRepository.findById(id) //id = 200 returns 500 response as record with id 200 does not exist in the database. What should I do to get ResourceNotFoundException?
eventRepository.findById returns Optional (in Spring Data JPA 2.0.6, see https://docs.spring.io/spring-data/jpa/docs/2.0.6.RELEASE/reference/html/#repositories.core-concepts)
Optional.get on empty optional causes NoSuchElementException (https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html#get--). Your if (event == null) comes too late.
Checking stactrace, you should see that exception comes from line with this.eventRepository.findById and actual exception is NoSuchElementException
To fix that you should change your code to
Optional<Event> optionalEvent= this.eventRepository.findById(id);
if (!optionalEvent.isPresent()) {
throw new ResourceNotFoundException("Event with id " + id + " not found.");
}
Event event=optionalEvent.get();
//the rest of your logic
You may also write your code in more functional way
Event event = this.eventRepository
.findById(id)
.orElseThrow(() -> new ResourceNotFoundException("Event with id " + id + " not found."))
Summary
Do not call get() on Optional without checking if it is present (using isPresent() method)
eventRepository.findById() return an Optional
Therefore you have to test for existence before get()
Optional<Event> optEvent = eventRepository.findById();
if (!optEvent.isPresent()) {
//throw exception here
}

How to send content of a file in a tSendMail

i try since some days to send one mail with different informations from child job and father job.
see my jobs below:
and my tRunJob_3
at the end of this job, i can put informations from tAggregateRow in the email, but it send several email (one mail by condition "if" approved DeinitJob --- if --- tRunJob_3)
my file generated by tFileOutputDelimited_1 in the father job contains all the information i need to put in the final mail.
1: how to display these informations in one email (no attachement) ?
2: i have this error in my console:
For input string: "7.91'7.91"
multiple points
what it means ?
EDIT:
with the modification below, it send me 1 email with the informations collected
in my tjavaflex:
code initial
// start part of your Java code
boolean loop ;
System.out.println("## START\n#");
code principal
// here is the main part of the component,
// a piece of code executed in the row
// loop
// code sample:
System.out.println("## LOAD...\n#");
if ((String)globalMap.get("message") != null) {
globalMap.put("message", (String)globalMap.get("message") + row5.mag + " qt: " + row5.qt + " p1: " + row5.p1 + "\n" ) ;
}
code final:
// end of the component, outside/closing the loop
loop = true ;
System.out.println("## END\n#");
with a if loop between tjava and tsendmail
but still have my error:
For input string: "7.91'7.91"
multiple points

AmazonCloudWatchClient not sending HTTP requests

first time i'm using aws api in java to get the cloud watch statistics for my ec2-instance. i googled about this and i found some code snippet. here it is
AmazonCloudWatchClient cloudWatch = new AmazonCloudWatchClient(
new BasicAWSCredentials(AccessKey, SecretKey));
cloudWatch.setEndpoint("ec2-<my-static-ip>.compute-1.amazonaws.com");
long offsetInMilliseconds = 1000 * 60 * 60 * 24;
Dimension instanceDimension = new Dimension();
instanceDimension.setName("Instanceid");
instanceDimension.setValue(InstanceId);
GetMetricStatisticsRequest request = new GetMetricStatisticsRequest()
.withStartTime(
new Date(new Date().getTime()
- offsetInMilliseconds))
.withNamespace("AWS/EC2")
.withPeriod(60 * 60)
.withDimensions(
new Dimension().withName("InstanceId").withValue(
InstanceId))
.withMetricName("CPUUtilization")
.withStatistics("Average", "Maximum")
.withEndTime(new Date());
GetMetricStatisticsResult getMetricStatisticsResult = cloudWatch
.getMetricStatistics(request);
double avgCPUUtilization = 0;
List dataPoint = getMetricStatisticsResult.getDatapoints();
for (Object aDataPoint : dataPoint) {
Datapoint dp = (Datapoint) aDataPoint;
avgCPUUtilization = dp.getAverage();
System.out.println(InstanceId
+ " instance's average CPU utilization : "
+ dp.getAverage());
}
} catch (AmazonServiceException ase) {
System.out
.println("Caught an AmazonServiceException, which means the request was made "
+ "to Amazon EC2, but was rejected with an error response for some reason.");
System.out.println("Error Message: " + ase.getMessage());
System.out.println("HTTP Status Code: " + ase.getStatusCode());
System.out.println("AWS Error Code: " + ase.getErrorCode());
System.out.println("Error Type: " + ase.getErrorType());
System.out.println("Request ID: " + ase.getRequestId());
}
so, using this code i tried to get statistics, but first time it throws error saying
com.amazonaws.AmazonClientException: Unable to execute HTTP request:Connection to https://ec2-<my-static-ip>.compute-1.amazonaws.com refused
then i thought it was sending https requests. so i enabled ssl on my instance and tried, then i'm getting below exception.
com.amazonaws.AmazonClientException: Unable to execute HTTP request: peer not authenticated
i was using OpenJDK in my instance, so i thought that may causing the problem. then i removed openjdk and installed Oracle JDK 1.7. but still same problem.
My questions are,
1) how can i send only HTTP (instead of HTTPS) requests to get statistics?
2)how to get rid of this problem, so that i can get my results?
But please don't ask me to read any docs, because i messed up by searching in net, blogs,forums, docs... etc. then i end up here. so, please just provide me solution or tell me where i'm going wrong.
Can anybody please help me out this issue.
thank you in Advance.
Got Solution.
1) removed setting end point for AmazonCloudWatchClient.
2) problem with the AWS credentials (Access key ID, Secret key).So, i created another set of credentials and gave CloudWatchFullAccess policy for the user.
Now it is working like Charm... :-)
Thanks.

Categories