Skip to content

Commit

Permalink
Merge pull request spring-projects#37 from amolnayak311/INTSAMPLES-61
Browse files Browse the repository at this point in the history
* amolnayak311-INTSAMPLES-61:
  INTSAMPLES-61 Changes, use LinkedHashMap to demonstrate LRU concept
  • Loading branch information
ghillert committed Mar 16, 2012
2 parents b6b7a14 + c97fb87 commit af7e604
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -31,6 +31,7 @@

/**
* @author Gary Russell
* @author Amol Nayak
*
*/
public class FtpOutboundChannelAdapterSample {
Expand Down Expand Up @@ -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());
}
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<modules>
<module>basic</module>
<module>intermediate</module>
<module>advanced</module>
<module>applications</module>
</modules>

Expand Down

0 comments on commit af7e604

Please sign in to comment.