728x90
https://medium.com/altuure/jgroups-spring-in-action-bcee3ce0aa16
우리의 애플리케이션(스프링)에 네트워킹 유틸리티를 추가하기 위해서 다음 두개의 JGroups 의 FactoryBeans 클래스 코드를 사용 할 수 있을 것입니다.
- ChannelBeanFactory : JChannel 인스턴스를 생성하기 위한 팩토리
- ReplicatedHashMapFactory : ReplicatedHashMapFactory 인스턴스
pom.xml에 jgroups 의존성을 추가 합니다.
jgroups
jgroups
2.5.2
ChannelBeanFactory.java
스프링 환경 구성으로 JGroups Channel 환경 구성을 생성 할 수 있도록 해 주는 Spring BeanFactory 클래스
public class ChannelBeanFactory extends AbstractFactoryBean implements DisposableBean {
private String jgroupsconfig;
private String cluster_name;
private JChannelFactory factory;
public Class getObjectType() {
return Channel.class;
}
protected Object createInstance() throws Exception {
if (factory == null)
this.factory = new JChannelFactory(jgroupsconfig);
Channel jChannel = factory.createChannel();
jChannel.connect(cluster_name);
return jChannel;
}
}
JGroups의 분산형 Map 구현 물을 위한 멋진 FactoryBean 하나 입니다.
public class ReplicatedHashMapFactory extends AbstractFactoryBean {
String channelName = "Altuure Cluster";
private String clusterOptions = null;
private boolean distributable = false;
private int timeout = 5000;
protected Object createInstance() throws Exception {
if (!distributable)
return new HashMap();
JChannel jChannel;
ReplicatedHashMap hashtable;
if (clusterOptions == null) {
jChannel = new JChannel();
jChannel.connect(channelName);
hashtable = new ReplicatedHashMap(jChannel, false, timeout);
hashtable.setBlockingUpdates(true);
} else {
jChannel = new JChannel(Thread.currentThread().getContextClassLoader().getResource(clusterOptions));
jChannel.connect(channelName);
hashtable = new ReplicatedHashMap(jChannel, false, timeout);
hashtable.setBlockingUpdates(true);
}
hashtable.start(timeout);
return hashtable;
}
protected void destroyInstance(Object instance) throws Exception {
if (instance instanceof ReplicatedHashMap) {
ReplicatedHashMap o = (ReplicatedHashMap) instance;
o.getChannel().close();
o.stop();
}
}
}
그리고 샘플 환경구성과 Test Case
public class ReplicatedHashMapTest extends TestCase {
private static final String XML = "com/altuure/spring/jgroups/ReplicatedHashMapTest.xml";
public static final int APPCOUNT = 6;
private ClassPathXmlApplicationContext[] applicationContexts = null;
protected void setUp() throws Exception {
super.setUp();
applicationContexts = new ClassPathXmlApplicationContext[APPCOUNT];
for (int i = 0; i < applicationContexts.length; i++) {
applicationContexts[i] = new ClassPathXmlApplicationContext(XML);
}
}
public void testAll() {
Map map = (Map) applicationContexts[0].getBean("com.altuure.spring.jgroups.ReplicatedHashMapFactory");
map.put("test1-Key", "test1-Value");// put
map.put("test2-Key", "test2-Value"); // put & update
map.put("test2-Key", "test2-Value2");
map.put("test3-Key", "test3-Value"); // put & remove
map.remove("test3-Key");
for (int i = 0; i < applicationContexts.length; i++) {
ClassPathXmlApplicationContext applicationContext = applicationContexts[i];
Map mapInstance = (Map) applicationContexts[i]
.getBean("com.altuure.spring.jgroups.ReplicatedHashMapFactory");
assertEquals("test1-Value", map.get("test1-Key"));
assertEquals("test2-Value2", map.get("test2-Key"));
assertNull(map.get("test3-Key"));
}
}
protected void tearDown() throws Exception {
super.tearDown();
for (int i = 0; i < applicationContexts.length; i++) {
applicationContexts[i].close();
}
}
}
728x90
'프로그래밍' 카테고리의 다른 글
Spring Boot AOP (0) | 2024.02.27 |
---|---|
JDBC_PING을 사용하여 클러스터 노드를 검색하는 방법 (0) | 2024.02.23 |
신뢰성 있는 메시지 교환을 위한 JGroups (0) | 2024.02.22 |
Spring MVC 내 HandlerInterceptors vs. Filters (0) | 2024.02.16 |
Maven과 AspectJ - 완벽 환경설정 관련 (0) | 2024.02.15 |