Send and receive data with XML - java

I have 2 programs : Agent.java & Simulator.java (don't worry about names, you can call them A & B respectively). Now, I want to send job object from Agent to Simulator using XML format. The job class looks like:
public class job {
int JobID;
job(int JobID){
this.JobID=JobID;
}
public int getJobID(){
//get JobID variable value from here
}
public void setJobID(int temp_JobID){
//change variable JobID here
}
}
Now I store it in a XML format and send to Simulator. I know that I can use other ways to send object job but this XML file format is standard to be followed in my project.
On the other hand , I receive job object, get data from it and use them in program.
So my Q. is: How do I send data using XML? I saw many Q. related to this but they refer to an XML file on hard drive, convert to String, send it and then receive in other program. I think this is not going to work in my case because I have many jobs are coming continuously, and I will receive them on real-time. So, its bad idea to store them on my compute. Isn't there any XML file sender and receiver?

Maybe look at JAXB. You can create xsd-files from your specified format, generate annotated job class from it and also use the generated object factories. Then you have your jobs in memory and you can create a queue of them.

In one of the projects, I used JDOM for exchanging data between files in xml.
Class A constructs xml document from the object's fields and send that document to class B. Class B can create object from that received document.

No need of any file for posting jobs. You can use in-memory objects.
Try using JIBX to Marshall/Unmarshall you job object and have it in memory instead of in file. Once you have that Job object marshall to XML in string format send it to Simulator. On the Simulator side unmashall the XML to Job object again.
If you are using any JMS service for posting jobs to Simulator make that XML string part of you Message.

Related

Apache Camel: How to look inside body to determine file format

We receive .csv files (both via ftp and email) each of which can be one of a few different formats (that can be determined by looking at the top line of the file). I am fairly new to Apache Camel but want to implement a content based router and unmarshal each to the relevant class.
My current solution is to break down the files to a lists of strings, manually use the first line to determine the type of file, and then use the rest of the strings to create relevant entity instances.
Are there a cleaner and better way?
You could use a POJO to implement the type check in whatever way works best for your files.
public String checkFileType(#Body File file) {
return determineFileType(file);
}
private String determineFileType(File file) {...}
Like this you can keep your route clean by separating the filetype check and any other part of processing. Because the filetype check is just metadata enrichment.
For example you could just set the return value as a message header by calling the bean)
.setHeader("fileType", method(fileTypeChecker))
Then you can route the files according to type easily by using the message header.
.choice()
.when(header("fileType").isEqualTo("foo"))
...

How to post a big string/json using AJAX on Play Framework 1.4.x

I have a JSON that looks more or less like this:
{"id":"id","date":"date","csvdata":"csvdata".....}
where csvdata property is a big amount of data in JSON format too.
I was trying to POST this JSON using AJAX in Play! Framework 1.4.x so I sended just like that, but when I receive the data in the server side, the csvdata looks like [object Object] and stores it in my db.
My first thought to solve this was to send the csvdata json in string format to store it like a longtext, but when I try to do this, my request fails with the following error:
413 (Request Entity Too Large)
And Play's console show me this message:
Number of request parameters 3623 is higher than maximum of 1000, aborting. Can be configured using 'http.maxParams'
I also tried to add http.maxParams=5000 in application.conf but the only result is that Play's console says nothing and in my database this field is stored as null.
Can anyone help me, or maybe suggest another solution to my problem?
Thanks you so much in advance.
Is it possible that you sent "csvdata" as an array, not a string? Each element in the array would be a separate parameter. I have sent 100KB strings using AJAX and not run into the http.maxParams limit. You can check the contents of the request body using your browser's developer tools.
If your csvdata originates as a file on the client's machine, then the easiest way to send it is as a File. Your controller action would look like:
public static void upload(String id, Date date, File csv) {
...
}
When Play! binds a parameter to the File type, it writes the contents of the parameter to a temporary file which you can read in. (This avoids running out of memory if a large file is uploaded.) The File parameter type was designed for a normal form submit, but I have used it in AJAX when the browser supported some HTML5 features (File API and Form Data).

How to convert comma separated data to CopyOnWriteArrayList to be sent as payload in MUnit

I am implementing MUnit for a flow where I need to send the payload through a Set Message Processor as a CopyOnWriteArrayList. The payload data would be fetched from a file.
The file would have comma separated XML Data. I need this data to be sent as CopyOnWriteArrayList.
Please help me on the Java class that can be used in MEL.
Please find below updated details :
For the MUnit test, I have the data in a file as of the format [First_XML_Data,Second_XML_Data]. The First_XML_Data and Second_XML_Data are both XML data. So basically first component in mule flow is Java transformer as below:
public class XMLData extends AbstractTransformer {
#Override
protected Object doTransform(Object src, String enc) throws TransformerException {
CopyOnWriteArrayList<String> list = (CopyOnWriteArrayList<String>) src;
}
}
As you can see, I need to prepare a payload in Set Message Processor so that it can be passed on to this Java transformer. So as of now I have the following MEL in set message processor of MUnit test,
[Arrays.asList((getResource('src/main/resources/xml_data.xml').asString().split(',')))]
The applications throws a type cast exception. So i need to some how send the payload as CopyOnWriteArrayList. As the data is too large , I am picking it up from the File
Thanks.
Instead of use Arrays.asList(), you should construct the CopyOnWriteArrayList.
#[new java.util.concurrent.CopyOnWriteArrayList(getResource('src/main/resources/xml_data.xml').asString().split(','))]

Convert DFDL to XML

I'm trying to parse a web service response message in the following format (message tree):
Message
Properties
Properties..[]
DFDL
ObjectIWantUnmarshalled
AllItsDataIwant[]
And unmarshal the "ObjectIWantUnmarshalled". However, this data is in DFDL format.
In my request, I use the following line in order to format from XML to DFDL:
Document outDocument = outMessage.createDOMDocument(MbDFDL.PARSER_NAME);
But there doesn't seem to be a way to to the opposite, of DFDL to XML.
I have tried:
Document outDocument = inMessage.createDOMDocument(MbXMLNSC.PARSER_NAME);
As well as other attempts to simply unmarshal the data directly from the MbMessage:
jaxbContext_COBOL.createUnmarshaller().unmarshal(inMessage.getDOMDocument())
But I have not been able to get a Document node this way, or any other way, it is always null.
Probably a lot too late, but you were going about this the wrong way.
When using WMB and IIB you should use the built-in XML support - not the javax.XML.* class library. So instead of using the JAXB unmarshaller, you should
create an XMLNSC tree under the output message root
copy the input DFDL message tree to the output XMLNSC message tree ( one line )
...and the message flow will serialize ( unmarshall ) the tree as XML whenever it needs to - when it encounters an output node, or when you call outMessage.toBitstream().

How can I get content from Exchange.In:Body object from a ProcessDefinition in Camel

I am integrating data between two systems using Apache Camel. I want the resulting xml to be written to an xml file. I want to base the name of that file on some data which is unknown when the integration chain starts.
When I have done the first enrich step the data necessary is in the Exchange object.
So the question is how can I get data from the exchange.getIn().getBody() method outside of the process chain in order to generate a desirable filename for my output file and as a final step, write the xml to this file? Or is there some other way to accomplish this?
Here is my current Process chain from the routebuilders configuration method:
from("test_main", "jetty:server")
.process(new PiProgramCommonProcessor())
.enrich("piProgrammeEnricher", new PiProgrammeEnricher())
// after this step I have the data available in exchange.in.body
.to(freeMarkerXMLGenerator)
.to(xmlFileDestination)
.end();
best regards
RythmiC
The file component takes the file name from a header (if present). So you can just add a header to your message with the desired file name.
The header should use the key "CamelFileName" which is also defined from Exchange.FILE_NAME.
See more details at: http://camel.apache.org/file2

Categories

Resources