JGroups-Spring In Action

2024. 2. 26. 20:14프로그래밍

728x90

https://medium.com/altuure/jgroups-spring-in-action-bcee3ce0aa16

 

JGroups-Spring In Action

JGroups Basic and other jGroups posts Some Sample Code you may use for adding networking utilities to your Application ChannelBeanFactory.java Is a Spring BeanFactory to create and configure JGroups…

medium.com

 

 

우리의 애플리케이션(스프링)에 네트워킹 유틸리티를 추가하기 위해서 다음 두개의 JGroups 의 FactoryBeans  클래스 코드를 사용 할 수 있을 것입니다.

  1. ChannelBeanFactory : JChannel 인스턴스를 생성하기 위한 팩토리
  2. 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