'Embedded Infinispan'에 해당되는 글 14건

  1. 2015/04/03 용비 04. Step-2. Putting stuff in the cache
  2. 2015/04/03 용비 03. Step-1. Initializing the CacheManager
  3. 2015/04/02 용비 02. Step-0. The Weather Application
  4. 2015/04/02 용비 01. Introduction

CacheManager가 실행되면, 우리는 cache 생성 및 데이터 저장을 시작할 수 있다. Infinispan내의 Cache들은 유일한 이름으로 구분되는 "named" Cache들이다. 하지만, "default" Cache라는 특별한 cache가 있는데, 특별한 이름 없이도 쉽게 얻을 수 있는 cache이다. 이제 초기 버전의 WeatherService를 확장하여 cache에 조회한 날씨 데이터를 저장할 것이다. 이것을 위해서 WeatherService와 구체적인 구현 사이에 추상 클래스(CachingWeatherService)를 생성할 것이다. 이 클래스에서는 값비싼 remote call wrapping하여 remote call결과를 cache하고, 값비싼 remote API를 호출하는 대신, 다시 요청하기 전에 wether 데이터가 이미 cache에 있는지 확인한다. 첫번째로 cache를 얻어온다.

 

Cache<String, LocationWeather> cache = cacheManager.getCache();


CacheManager
에 존재하는 cache가 없으면, default configuration을 사용하여 cache를 생성한다. Key Value generic type assignment type에서 유추할 수 있다.  날씨 정보를 얻어오기 위한 caching wrapper는 다음과 같다.

 

LocationWeather weather = cache.get(location);

if (weather == null) {

weather = fetchWeather(location);

cache.put(location, weather);

}

return weather;


Infinispan
 Cache java.util.Map 인터페이스를 구현했기 때문에, 위의 코드는 따로 설명이 필요 없다. (self explanatory) 이제 Cache가 존재한다. Application을 다시 실행시켜 보자.

 

 

git checkout -f step-2

mvn clean package exec:exec

 

 

[Output]

---- Fetching weather information ----
Rome, Italy - Temperature: 12.9° C, Conditions: light rain
Como, Italy - Temperature: 6.3° C, Conditions: Sky is Clear
Basel, Switzerland - Temperature: 0.8° C, Conditions: overcast clouds
Bern, Switzerland - Temperature: -1.6° C, Conditions: broken clouds
London, UK - Temperature: 1.8° C, Conditions: light rain
Newcastle, UK - Temperature: 2.6° C, Conditions: scattered clouds
Bucharest, Romania - Temperature: 9.3° C, Conditions: scattered clouds
Cluj-Napoca, Romania - Temperature: 6.4° C, Conditions: scattered clouds
Ottawa, Canada - Temperature: -7.0° C, Conditions: overcast clouds
Toronto, Canada - Temperature: -7.0° C, Conditions: broken clouds
Lisbon, Portugal - Temperature: 14.6° C, Conditions: overcast clouds
Porto, Portugal - Temperature: 12.2° C, Conditions: moderate rain
Raleigh, USA - Temperature: 3.9° C, Conditions: Sky is Clear
Washington, USA - Temperature: 3.4° C, Conditions: light rain
---- Fetched in 1634ms ----
---- Fetching weather information ----
Rome, Italy - Temperature: 12.9° C, Conditions: light rain
Como, Italy - Temperature: 6.3° C, Conditions: Sky is Clear
Basel, Switzerland - Temperature: 0.8° C, Conditions: overcast clouds
Bern, Switzerland - Temperature: -1.6° C, Conditions: broken clouds
London, UK - Temperature: 1.8° C, Conditions: light rain
Newcastle, UK - Temperature: 2.6° C, Conditions: scattered clouds
Bucharest, Romania - Temperature: 9.3° C, Conditions: scattered clouds
Cluj-Napoca, Romania - Temperature: 6.4° C, Conditions: scattered clouds
Ottawa, Canada - Temperature: -7.0° C, Conditions: overcast clouds
Toronto, Canada - Temperature: -7.0° C, Conditions: broken clouds
Lisbon, Portugal - Temperature: 14.6° C, Conditions: overcast clouds
Porto, Portugal - Temperature: 12.2° C, Conditions: moderate rain
Raleigh, USA - Temperature: 3.9° C, Conditions: Sky is Clear
Washington, USA - Temperature: 3.4° C, Conditions: light rain
---- Fetched in 2ms ----

 

두번째 날씨 조회 부분에서는 굉장히 빠른 것을 볼 수 있다. 위에서 Cache에 저장된 데이터는 Cache에서 삭제하기 전에는 없어지지 않는다. 다음 단계에서는 어떻게 생명주기를 갖는 데이터를 추가하는지를 살펴볼 것이다.

받은 트랙백이 없고, 댓글이 없습니다.

댓글+트랙백 RSS :: http://www.yongbi.net/rss/response/669

앞서 작성한 간단한 application infinispan을 추가하고, CacheManager를 초기화해보자. Infinispan 추가는pom.xml 파일에 다음 dependency를 추가하면 된다.

 

<dependency>

<groupId>org.infinispan</groupId>

<artifactId>infinispan-embedded</artifactId>

<version>7.1.0.Final</version>

</dependency>

 

이제 CacheManager를 추가할 수 있다. CacheManager는 다음과 같은 기능을 제공한다.

 

·         Cache container 역할. Cache lifecycle 관리

·         Global configuration, common data structure, resource 관리 (ex. Thread pools)

·         Clustering 관리

 

CacheManager는 무거운 component이다. Application 내에서 CacheManager를 초기화하고 싶다면 다음과 같이 간단하게 초기화할 수 있다.

 

CacheManager cacheManager = new DefaultCacheManager();

 

이것으로 Default local (ex. Non-clustered) CacheManager가 생성될 것이다. CacheManager는 적합한 처리가 필요한 약간의 resource를 포함하고 있기 때문에, 더 이상 불필요할 때는 정지시켜야할 필요가 있다. Stop() method를 호출하면 CacheManager가 정지된다.

 

cacheManager.stop();


CacheManager
를 정지하면 CacheManager를 통해서 얻었던 모든 resource들은 더 이상 사용할 수 없다.

 

이번 단계의 application을 컴파일하고 실행하려면 다음과 같이 하면 된다.

 

git checkout step-1

mvn clean package exec:exec

 

전혀 변화된 것이 없음을 보게 될 것이다. 이것은 우리가 CacheManager를 통해서 아무런 작업도 수행하지 않았기 때문이다. CacheManager에 대한 훌륭한 사용법은 다음 단계에서 살펴볼 것이다.

받은 트랙백이 없고, 댓글이 없습니다.

댓글+트랙백 RSS :: http://www.yongbi.net/rss/response/668

프로젝트를 check out 한다. 터미널을 실행하고, 다음 command를 실행한다.

git clone https://github.com/infinispan/infinispan-embedded-tutorial.git

cd infinispan-embedded-tutorial

 

위의 command를 통해서 git repository에 있는 프로젝트를 local copy하고, 현재 디렉토리를 변경하여 프로젝트의 root 디렉토리에 위치할 수 있다. 이제 Step-0으로 이동해보자.

 

git checkout -f step-0

 

이러한 초기 과정은 우리가 Weather application을 구현한 maven java project를 위한 것이다.  application은 다양한 도시의 현재 날씨 데이터를 얻기 위해 OpenWeatherMap (openweathermap.org) API를 이용한다. 인터넷 연결이 안되거나 문제가 있다면 random weather service를 이용할 수 있도록 pom.xml 파일내에 있는 주석을 해제하라. 이 초기 과정은 dependency가 전혀 없고, JDK logging을 사용한다. 다음 이미지는 프로젝트의 구성도를 보여준다.

사용자 삽입 이미지

컴파일하고 실행해 보자

mvn clean package

 

Maven command-line으로 실행한 2개의 goal (clean, package)을 순서대로 실행할 것이다.  첫번째 goal (clean)은 이전에 생성된 파일을 지우고, 두번째 goal (package)은 프로젝트를 컴파일하고 unit test를 실행하고,컴파일한 code jar 파일로 package할 것이다. 이제 우리는 code를 실행해 볼 수 있다. WeatherApp Classmain() method WeatherService를 초기화하고 list에 있는 도시의 현재 온도를 두 번 읽어온다. 그리고 list에 있는 도시의 데이터를 모두 얻어오는데 걸린 경과 시간을 출력한다. 현재 단계에서는 cache가 없으므로, application은 매번 데이터를 얻어와야 하기 때문에 정말 느리다.

 

mvn exec:exec

 

[output]

---- Fetching weather information ----
Rome, Italy - Temperature: 12.9° C, Conditions: light rain
Como, Italy - Temperature: 6.3° C, Conditions: Sky is Clear
Basel, Switzerland - Temperature: 0.8° C, Conditions: overcast clouds
Bern, Switzerland - Temperature: -1.6° C, Conditions: broken clouds
London, UK - Temperature: 1.8° C, Conditions: light rain
Newcastle, UK - Temperature: 2.6° C, Conditions: scattered clouds
Bucharest, Romania - Temperature: 9.3° C, Conditions: scattered clouds
Cluj-Napoca, Romania - Temperature: 6.4° C, Conditions: scattered clouds
Ottawa, Canada - Temperature: -7.0° C, Conditions: overcast clouds
Toronto, Canada - Temperature: -7.0° C, Conditions: broken clouds
Lisbon, Portugal - Temperature: 14.6° C, Conditions: overcast clouds
Porto, Portugal - Temperature: 12.2° C, Conditions: moderate rain
Raleigh, USA - Temperature: 3.9° C, Conditions: Sky is Clear
Washington, USA - Temperature: 3.4° C, Conditions: light rain
---- Fetched in 2210ms ----
---- Fetching weather information ----
Rome, Italy - Temperature: 12.9° C, Conditions: light rain
Como, Italy - Temperature: 6.3° C, Conditions: Sky is Clear
Basel, Switzerland - Temperature: 0.8° C, Conditions: overcast clouds
Bern, Switzerland - Temperature: -1.6° C, Conditions: broken clouds
London, UK - Temperature: 1.8° C, Conditions: light rain
Newcastle, UK - Temperature: 2.6° C, Conditions: scattered clouds
Bucharest, Romania - Temperature: 9.3° C, Conditions: scattered clouds
Cluj-Napoca, Romania - Temperature: 6.4° C, Conditions: scattered clouds
Ottawa, Canada - Temperature: -7.0° C, Conditions: overcast clouds
Toronto, Canada - Temperature: -7.0° C, Conditions: broken clouds
Lisbon, Portugal - Temperature: 14.6° C, Conditions: overcast clouds
Porto, Portugal - Temperature: 12.2° C, Conditions: moderate rain
Raleigh, USA - Temperature: 3.9° C, Conditions: Sky is Clear
Washington, USA - Temperature: 3.4° C, Conditions: light rain
---- Fetched in 1820ms ----

 

 

다음 단계에서 수행하는 작업은 더 흥미로울 것이다.


받은 트랙백이 없고, 댓글이 없습니다.

댓글+트랙백 RSS :: http://www.yongbi.net/rss/response/667

Embedded Infinispan Java Application Tutorial (Infinispan 7.1.1.Final)

Introduction


Java Application infinispan embed하는 방법을 배우고 싶다면 올바로 찾아온 것이 맞다. turorial local-only instance 시작해서 fully clustered application 이르기까지 infinispan-based application 작성하는데 필요한 모든 단계들에 대해서 Guide 것이다. 작성할 application 빠른 정보 제공을 위해 특정 도시의 현재 날씨 정보를 가져와서 cache 저장할 것이다. 또한 cache 저장된 데이터의 유효기간을 어떻게 설정하는지, 정보들이 유효기간에 맞춰 제거되는지를 보여줄 것이다. 그리고 Multiple node 대한 Cluster 구성은 어떻게 하는지, cluster 내에서 event들에 대해서 어떻게 반응하는지를 설명할 것이다. 끝으로, 국가별 평균 온도의 계산을 통해 강력한 map/reduce 기능을 보여줄 것이다.


tutorial 위해서는 다음 환경이 필요하다.

  • JDK 1.7 이상
  • Maven 3.2 이상
  • IDE (ex. Eclipse) or Text Editor
  • Git

tutorial command-line 통해 다양한 command 실행할 있다는 것을 가정하고 작성되었다. Tutorial 있는 code GitHub 통해 Step별로 프로젝트로 제공된다. 따라서, 여러분들은 Step에서 다음 Step으로 진행 , 코드의 변경 사항을 쉽게 파악할 있다.


, 그럼 이제 첫번째 Step으로 이동해 보자.

받은 트랙백이 없고, 댓글이 없습니다.

댓글+트랙백 RSS :: http://www.yongbi.net/rss/response/666