Browse files

Merge pull request #37 from amolnayak311/INTSAMPLES-61

* amolnayak311-INTSAMPLES-61:
  INTSAMPLES-61 Changes, use LinkedHashMap to demonstrate LRU concept
  • Loading branch information...
2 parents b6b7a14 + c97fb87 commit af7e6049ac34ac17008e032e557b95908553de64 Gunnar Hillert committed Mar 16, 2012
View
42 .../src/main/java/org/springframework/integration/samples/ftp/DynamicFtpChannelResolver.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2011 the original author or authors.
+ * Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,7 +16,9 @@
package org.springframework.integration.samples.ftp;
import java.util.HashMap;
+import java.util.LinkedHashMap;
import java.util.Map;
+import java.util.Map.Entry;
import java.util.Properties;
import org.springframework.context.ConfigurableApplicationContext;
@@ -30,12 +32,46 @@
* to send files to dynamic destinations.
*
* @author Gary Russell
+ * @author Amol Nayak
* @since 2.1
*
*/
public class DynamicFtpChannelResolver {
- private final Map<String, MessageChannel> channels = new HashMap<String, MessageChannel>();
+ //In production environment this value will be significantly higher
+ //This is just to demonstrate the concept of limiting the max number of
+ //Dynamically created application contexts we'll hold in memory when we execute
+ //the code from a junit
+ public static final int MAX_CACHE_SIZE = 2;
+
+ private final LinkedHashMap<String, MessageChannel> channels =
+ new LinkedHashMap<String, MessageChannel>() {
+
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ protected boolean removeEldestEntry(
+ Entry<String, MessageChannel> eldest) {
+ //This returning true means the least recently used
+ //channel and its application context will be closed and removed
+ boolean remove = size() > MAX_CACHE_SIZE;
+ if(remove) {
+ MessageChannel channel = eldest.getValue();
+ ConfigurableApplicationContext ctx = contexts.get(channel);
+ if(ctx != null) { //shouldn't be null ideally
+ ctx.close();
+ contexts.remove(channel);
+ }
+ }
+ return remove;
+ }
+
+ };
+
+ private final Map<MessageChannel, ConfigurableApplicationContext> contexts =
+ new HashMap<MessageChannel, ConfigurableApplicationContext>();
+
+
/**
* Resolve a customer to a channel, where each customer gets a private
@@ -63,6 +99,8 @@ private synchronized MessageChannel createNewCustomerChannel(String customer) {
ctx.refresh();
channel = ctx.getBean("toFtpChannel", MessageChannel.class);
this.channels.put(customer, channel);
+ //Will works as the same reference is presented always
+ this.contexts.put(channel, ctx);
}
return channel;
}
View
25 ...est/java/org/springframework/integration/samples/ftp/FtpOutboundChannelAdapterSample.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2011 the original author or authors.
+ * Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -31,6 +31,7 @@
/**
* @author Gary Russell
+ * @author Amol Nayak
*
*/
public class FtpOutboundChannelAdapterSample {
@@ -66,5 +67,27 @@ public void runDemo() throws Exception{
assertTrue(e.getCause().getCause() instanceof UnknownHostException);
assertEquals("host.for.cust2", e.getCause().getCause().getMessage());
}
+
+ // send to a different customer; again, check the log to see a new ac is built
+ //and the first one created (cust1) should be closed and removed as per the max cache size restriction
+ message = MessageBuilder.withPayload(file)
+ .setHeader("customer", "cust3").build();
+ try {
+ channel.send(message);
+ } catch (MessageHandlingException e) {
+ assertTrue(e.getCause().getCause() instanceof UnknownHostException);
+ assertEquals("host.for.cust3", e.getCause().getCause().getMessage());
+ }
+
+ //send to cust1 again, since this one has been invalidated before, we should
+ //see a new ac created (with ac of cust2 destroyed and removed)
+ message = MessageBuilder.withPayload(file)
+ .setHeader("customer", "cust1").build();
+ try {
+ channel.send(message);
+ } catch (MessageHandlingException e) {
+ assertTrue(e.getCause().getCause() instanceof UnknownHostException);
+ assertEquals("host.for.cust1", e.getCause().getCause().getMessage());
+ }
}
}
View
1 pom.xml
@@ -13,6 +13,7 @@
<modules>
<module>basic</module>
<module>intermediate</module>
+ <module>advanced</module>
<module>applications</module>
</modules>

0 comments on commit af7e604

Please sign in to comment.