Thanks, this worked.
And also I found out another way to implement the ftp uploading function
with camel-ftp

//Do the connection
CamelContext context = new DefaultCamelContext();
ConsumerTemplate consumer = context.createConsumerTemplate();
SftpComponent component = new SftpComponent(context);
SftpConfiguration conf = new SftpConfiguration(getFtpURI());
conf.setKnownHostsFile(knownHostsFile);
conf.setPassword(password);

SftpOperations sftp = new SftpOperations();
sftp.connect(new SftpEndpoint(null, component, conf));

//Do the upload
Exchange exchange = consumer.receive(getFileURI(), 3000);
sftp.storeFile(name, exchange);

For other function, refer to
org.apache.camel.component.file.remote.SftpOperations


willem.jiang wrote:
> 
> The sendTemplate.sendBody() is a sync API and it will return if the ftp 
> uploading processor is finished.
> So your main method could be
>      // create context
>      CamelContext context = new DefaultCamelContext();
>      // create consumer and producer template
>      ConsumerTemplate consumer = context.createConsumerTemplate();
>      ProducerTemplate producer = context.createProducerTemplate();
> 
> 
>      // loop to empty file directory
>          while (true) {
> 
>        // receive the message from the file directory, wait at most 3 sec
>              Exchange exchange = 
> consumer.receive("file:c:\\uploadfiles?noop=true", 3000);
>              if (msg == null) {
>                  // no more messages in queue
>                  break;
>              }
> 
>              // You can do the message transformation here
> 
>              // send it to the ftp endpoint
>              Exchange result = producer.send("FTP URI", exchange);
> 
>              // You may check the result state to see if there is any 
> exception is thrown
>          }
> 
> 
> Willem
> 
> 
> Hebert Hu wrote:
>> I looked into the example and still uncertain about how to check whether
>> the
>> sendTemplate is returned. Would you please show me a code snippet?
>> 
>> willem.jiang wrote:
>>> If you are using file endpoint to pull the directory, it is hard to tell 
>>> if the uploading processor is over,
>>> Can you take look at the camel polling consumer API[1] (Timer based 
>>> polling consumer example), in this way you can add you logic to check if 
>>> the file pulling is finished, and the sendTemplate will return when the 
>>>   uploading work is over.
>>>
>>> [1]http://camel.apache.org/polling-consumer.html
>>>
>>> Willem
>>>
>>> Hebert Hu wrote:
>>>> I started the upload like this
>>>>
>>>> CamelContext context = new DefaultCamelContext();
>>>> RouteBuilder route = createRouteBuilder();
>>>> context.addRoutes(route);
>>>> context.start();
>>>>
>>>> I'm able to get the route service status by 
>>>> RouteDefinition definition = context.getRouteDefinitions().get(0);
>>>> context.getRouteStatus(definition)
>>>>
>>>> But how can I tell whther the upload is finished?
>>>> As I always get "Started" even the upload is finished.
>>>>
>>>>
>>>> Hebert Hu wrote:
>>>>> Hi, 
>>>>>
>>>>> I've encountered a strange problem when using Camel Sftp component for
>>>>> uploading.
>>>>>
>>>>> Here's my code
>>>>>
>>>>>     protected RouteBuilder createRouteBuilder() throws Exception {
>>>>>         return new RouteBuilder() {
>>>>>             public void configure() throws Exception {
>>>>>                
>>>>> from("file:c:\\uploadfiles?noop=true").to(getFtpUrl());
>>>>>                 from(getFtpUrl()).to("mock:result");
>>>>>             }
>>>>>         };
>>>>>     }
>>>>>
>>>>> private String getFtpUrl(){
>>>>>   return
>>>>> "sftp://usern...@sftpserver/ftpload?password=password&binary=true&knownHostsFile=c://.ssh//known_hosts";;
>>>>>   }
>>>>>
>>>>> The problem is: When I start my upload procedure, for the files in the
>>>>> c:\uploadfiles (multiples files), some of them will create a
>>>>> .camellock
>>>>> file while others not. As in the remote server, some of the files that
>>>>> have a .camellock copy will be uploaded while others not. Noted that
>>>>> not
>>>>> all the files that has .camellock copies will be uploaded. And an
>>>>> exception is thrown
>>>>> SEVERE: Cannot retrieve file: ftpload/a.txt
>>>>> org.apache.camel.component.file.GenericFileOperationFailedException:
>>>>> Cannot retrieve file: ftpload/a.txt
>>>>>   at
>>>>> org.apache.camel.component.file.remote.SftpOperations.retrieveFileToStreamInBody(SftpOperations.java:326)
>>>>>   at
>>>>> org.apache.camel.component.file.remote.SftpOperations.retrieveFile(SftpOperations.java:312)
>>>>>   at
>>>>> org.apache.camel.component.file.GenericFileConsumer.processExchange(GenericFileConsumer.java:183)
>>>>>   at
>>>>> org.apache.camel.component.file.GenericFileConsumer.processBatch(GenericFileConsumer.java:120)
>>>>>   at
>>>>> org.apache.camel.component.file.GenericFileConsumer.poll(GenericFileConsumer.java:93)
>>>>>   at
>>>>> org.apache.camel.impl.ScheduledPollConsumer.run(ScheduledPollConsumer.java:99)
>>>>>   at
>>>>> java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:432)
>>>>>   at
>>>>> java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:295)
>>>>>   at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:150)
>>>>>   at
>>>>> java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$101(ScheduledThreadPoolExecutor.java:80)
>>>>>   at
>>>>> java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.runPeriodic(ScheduledThreadPoolExecutor.java:157)
>>>>>   at
>>>>> java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:181)
>>>>>   at
>>>>> java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:665)
>>>>>   at
>>>>> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:690)
>>>>>   at java.lang.Thread.run(Thread.java:799)
>>>>> When I run the upload procedure again, more .camellock files appeared,
>>>>> more files been uploaded, and the exception is still thrown.
>>>>> For example. 10 files in c:\uploadfiles, run the upload for the first
>>>>> time, 5 .camellock appeared, 1 file with .camellock uploaded, and an
>>>>> exception. Run for the second time, 2 more .camellock appeared, 1 more
>>>>> file uploaded, and an exception again.
>>>>>
>>>>> I'm using Camel-core 2.0.0, camel-ftp 2.0.0
>>>>>
>>>>> Can any one shed some light on me?
>>>>>
>>>
>>>
>> 
> 
> 
> 

-- 
View this message in context: 
http://old.nabble.com/Issue-in-upload-with-Camel-tp26940685p26976537.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Reply via email to