Pass variable to CtalendJob from route, create file and store it - java

Happy new year everyone :)
I'm new user of Talend studio, this is the second time I'm using ESB conductor and I think I'm doing something wrong here.
So, at the beginning I received a file from a SFTP then I depose it to another SFTP.
There is no problem here.
Second, after send this file, I need to create a second file, take the name of the first one send, rename it and depose it to the destination SFTP.
My Route
So with the first component (cProcessor), I'm setting 2 variables :
exchange.setProperty("target_directory", targetDirectory);
exchange.setProperty("x_filename_trg", trgFileName);
Here, I retrieve the right filename (xxx.trg) and the right targetDirectory.
Now I'm using a cTalendJob to call a job who can create an empty file :
Inside cTalendJob
To recover the variables of my routes, in my cTalendJob I checked "use context of the route"
In my tRouteInput I setted the schema like this cRouteInput
Then I tried to create the new file on the folder :
Create new file
At the moment my problem is : My variable are not transmitted to my cTalendJob. How can I transmit and use them properly ? File Directory + FileName
Can I send this file to the SFTP on the cTalendJob or need I to send it from my route ?
Thank you if you read me and if you can help me :)

Did you try by using a cSetHeader instead of cProcessor, to pass the variable as headers and not as properties ?

Related

Google Drive Java Api - update file in the folder.

i have a folder in the GoogleDrive. Its named 'lgc'. In it, i have a file info. I know the ID of the folder, but i dont know the ID of 'info' text file in it. I want to UPDATE the content of 'info' file, Conditions to update:
I dont want to change file name or title, only wanting to change its contents.
I dont want to use File ID for 'info' file. Using search Query, that will only result file with name as 'info'.
So, How do i find file without id and How do i Update it? I looked Google Developers website. but please help me, i didnt understood it.
tell me answer to following too: how can i do following?
Before Uploading a file, Check if any file with the same Title is present in Drive.
If present, delete present file and upload new file (Instead of Updating it)
Everything in Drive is keyed from the file ID, which is why you can end up multiple files sharing the same name. To modify content in Drive, you will have to at some stage work with the file ID, you cannot avoid that.
You can search for a file by name (title = 'info' and [parentID] in parents), but you will still need that query to return the file ID so that you can then update.
So your steps would be:
1) files.list where q is: title = 'info' and [parentID for lgc] in parents
2) If you get results, do a files.delete for each file ID returned (you could get more than 1)
3) files.insert for your new info file.
Some notes:
Drive doesn't support what you are trying to do as an atomic operation, which means there are some edge cases you will need to handle if you have a) the potential for multiple applications operate on the same account or b) multiple users operating on the same folder or c) the potential for a user themselves to create a file called 'info' in that lgc folder.
Step 1 may return more than a single file, so you should cater for that in step 2.
Another 'info' file may be created between Step 1 and Step 3, so after step 3 you should run the files.list search again to confirm that only your new file exists.

Apache-Camel console input

I am trying to make a small program that takes in console input such as a user's name, school and other information and then creates a file whose file name is that of the user. Each file will then be located in a folder named after the school. I am not sure how to create a file with those qualities since Camel seems to determine the path and file name before any input is read. Is there a way of getting around this problem?
There is an example on the file component page like so:
// set the output filename using java code logic, notice that this is done by setting
// a special header property of the out exchange
exchange.getOut().setHeader(Exchange.FILE_NAME, "report.txt");
you could replace report.txt with the filename you wish to use.
As for the directory, can you not store the directory name in a header and reference it from the endpoint:
.to("file://${headers.directory}");
more info here: http://camel.apache.org/file2.html

Java: Marking/Flagging a file

I would like to know whether or not there is some way of marking a file to identify whether or not the file contains x.
Consider the following example:
During a batch conversion process I am creating a log file which lists the success / failure of individual conversions.
So the process is as follows:
start conversion process
create log file named batch_XXX_yyyy_mm_dd.log
try to convert 'a'
write success to log file
try to convert 'b'
write success to log file
...
try to convert 'z'
write success to log file
close and persist log file
What I would like to be able to do is mark a file in some way that identifies whether any of the conversions logged in the file were unsuccessful.
I do not want to change the file name (visibly) and I do not want to open the file to check for a marker.
Does anyone have any ideas on how this could be achieved?
You can add file attributes in Java 7 through the java.nio.file.Files class.
So it would be possible to mark whether a file contains X using the Files.setAttribute() method:
Files.setAttribute( "path/to/file", "containsX", true );
And then check whether the file does contain X using the Files.getAttribute( ) method:
Files.getAttribute( "path/to/file", "containsX" )
If you are looking into say
file.log
create another file which will maintain this info say
file.log.status
Your status file can then contain all the information you need. It will be easier to get the status of conversion for all the files as well as easy to map back to original file given a status file.

Java: Efficient way to scan a folder for a particular file

I am contacting an external services with my Java app.
The flow is as follow: ->I generate an XML file, and put it in an folder, then the service processes the file and return another file with the same name having an extension .out
Right now after I put the file in the folder I start with a loop, until I get that file back so I can read the result.
Here is the code:
fileName += ".out";
File f = new File(fileName);
do
{
f = new File(fileName);
} while (!f.exists());
response = readResponse(fileName); // got the response now read it
My question comes here, am I doing it in the right way, is there a better/more efficient way to wait for the file?
Some info: I run my app on WinXP, usually it takes the external service less than a second to respond with a file, I send around 200 request per day to this services. The path to the folder with the result file is always the same.
All suggestions are welcome.
Thank you for your time.
There's no reason to recreate the File object. It just represents the file location, whether the file exists or not. Also you probably don't want a loop without at least a short delay, otherwise it'll just max out a processor until the file exists. You probably want something like this instead:
File file = new File(filename);
while (!file.exists()) {
Thread.sleep(100);
}
Edit: Ingo makes a great point in the comments. The file might not be completely there just because it exists. One way to guarantee that it's ready is have the first process create a second file after the first is completely written. Then have the Java program detect that second file, delete it and then safely read the first one.

How to create a directory with multiple levels in one call in Java using FTP

I am using the FTPClient library from Apache and cannot figure out a simple way to create a new directory that is more than one level deep. Am I missing something?
Assuming the directory /tmp already exists on my remote host, the following command succeeds in creating /tmp/xxx
String path = "/tmp/xxx";
FTPClient ftpc = new FTPClient();
... // establish connection and login
ftpc.makeDirectory(path);
but the following fails:
String path = "/tmp/yyy/zzz";
FTPClient ftpc = new FTPClient();
... // establish connection and login
ftpc.makeDirectory(path);
In that latter case, even /tmp/yyy isn't created.
I know I can create /tmp/yyy and then create /tmp/yyy/zzz, but I can't figure out how to create directly /tmp/yyy/zzz.
Am I missing something obvious? Using mkd instead of makeDirectory didn't help.
Also, is it possible in one call to upload a file to /tmp/yyy/zzz/test.txt if the directory /tmp/yyy/zzz/ doesn't exist already?
You need to do them one at a time, first /tmp/yyy and then /tmp/yyy/zzz. There is no short-cut mechanism for what you want to do.
FTP servers typically only allows you to create 1 level of a directory at a time. Thus you'll have to break up the path yourself, and issue one makeDirectory() call for each of the components.
No.
The FTP protocol doesn't permit this. So no, you can't create a directory with multiple levels in one call.

Categories