You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by hardikdesai81 <ha...@gmail.com> on 2013/07/29 17:33:38 UTC

Camel Spring DSL - FTP consumerTemplate.receive gives null Exchange

Hi folks,

I am using Camel with Spring DSL. I have implemented the FTP producer
correctly and I am able to generated a JSON object at runtime and send it to
an FTP Endpoint. However, when trying to consume that same JSON using
Consumer Template, I am facing a sporadic problem.

Following is my Spring context XML:
	<bean id="propertyConfigurer"
	
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:ftp.properties</value>
				<value>classpath:jdbc.properties</value>
			</list>
		</property>
		<property name="ignoreUnresolvablePlaceholders" value="true" />
	</bean>

	<camel:camelContext id="camelContext">
		<camel:template id="camelTemplate" />
		<camel:consumerTemplate id="consumerTemplate" />
		<camel:endpoint id="myFTPEndpoint"
		
uri="${ftp.protocol}://${ftp.userName}@${ftp.host}${ftp.remoteDirectory}?password=${ftp.password}&amp;passiveMode=${ftp.passiveMode}&amp;download=${ftp.download}&amp;localWorkDirectory=${ftp.localWorkDirectory}"
/>
	</camel:camelContext>

Following is my Producer code which is working fine:
@Service
public class FtpSenderServiceImpl implements FtpSenderService {

	private final ProducerTemplate producerTemplate;
	private final Endpoint ftpEndpoint;
	
	@Autowired
	FtpSenderServiceImpl(ProducerTemplate producerTemplate, Endpoint
ftpEndpoint) {
		this.producerTemplate = producerTemplate;
		this.ftpEndpoint = ftpEndpoint;
	}

	@Override
	public void sendFile(File file) throws CamelExecutionException,
UnsupportedEncodingException {
		producerTemplate.sendBodyAndHeader(ftpEndpoint, file,
				Exchange.FILE_NAME, file.getName());
	}
}

Following is my Consumer code:
@Service
public class FtpReceiverServiceImpl implements FtpReceiverService {

	private final ConsumerTemplate consumerTemplate;
	private final Endpoint ftpEndpoint;
	
	@Autowired
	FtpReceiverServiceImpl(ConsumerTemplate consumerTemplate, Endpoint
ftpEndpoint) {
		this.consumerTemplate = consumerTemplate;
		this.ftpEndpoint = ftpEndpoint;
	}

	@Override
	public List<String> receiveFiles() throws InterruptedException {
		List<String> files = new ArrayList<String>();
		Exchange ex = consumerTemplate.receive(ftpEndpoint, 5000);
		while(ex!=null) {
			files.add((String) ex.getIn().getHeader("CamelFileName"));
			ex = consumerTemplate.receive(ftpEndpoint, 5000);
		}
		return files;
	}
}

Basically, the receiveFiles() method should create a list of all files on
the FTP site. When I start my server, and immediately invoke my Service to
call receiveFiles(), then consumerTemplate.receive(ftpEndpoint, 5000)
returns a 'null' Exchange. 

But, if I wait for few seconds, and then execute the same
consumerTemplate.receive(ftpEndpoint, 5000), I am successfully able to fetch
the DefaultExchange object.

Why doesn't the consumerTemplate.receive(ftpEndpoint, 5000) return the valid
Exchange object in first attempt? Is there some configuration that I can set
in my FTP URI that can force it to invoke receive() only after a valid FTP
connection has been established?





--
View this message in context: http://camel.465427.n5.nabble.com/Camel-Spring-DSL-FTP-consumerTemplate-receive-gives-null-Exchange-tp5736439.html
Sent from the Camel - Users mailing list archive at Nabble.com.