'EventAdmin'에 해당되는 글 2건

  1. 2010.10.19 SpringDM for EventAdmin
  2. 2010.10.18 Event Admin 서비스 사용
Programming/OSGi2010. 10. 19. 09:24
http://java.dzone.com/articles/osgi-event-admin-with-spring-d 소스코드를 참조했음

1. SpringDMd을 사용

           1-1. 이벤트 핸들러로 이벤트 받기

                     Manifest.mf Import-Package org.osgi.service.event 추가

                     1-1-1. 인터페이스

                                public interface EventHandler {

                                          void handleEvnet(Event event);

                                }

                     1-1-2. 구현체

                                public class 클래스명 implements EventHandler {

                                          //인터페이스 구현체

                                          public void handleEvent(Event event) {

                                                     System.out.println("\r\n[Event Received] Topic : " + event.getTopic());

                                                     for(String key : event.getPropertyNames()){

                                                                System.out.println("\t[Property] " + key + " : " + event.getProperty(key));

                                                     }

                                          }

                                }

                     1-1-3. spring.xml                     

                                <osgi:service id="EventAdminServiceBean" ref="구현EventAdminBean"           interface="org.osgi.service.event.EventHandler">

                                          <osgi:service-properties>

                                                     <entry key="event.topics" value="Topic주소" />

                                          </osgi:service-properties>

                                </osgi:service>

                               

                                <bean id="구현EventAdminBean" class="구현EventAdmin경로" />

          

           1-2. 이벤트 보내기

                     1-2-1. 구현체

                                public class 클래스명 {

                                          private EventAdmin eventAdmin;

 

                                          @ServiceReference

                                          public void setEventAdmin(EventAdmin eventAdmin) {

                                                     this.eventAdmin = eventAdmin;

                                          }

                                         

                                          private String EVENT_QUEUE;

 

                                          public void setEVENT_QUEUE(String EVENT_QUEUE) {

                                                     this.EVENT_QUEUE = EVENT_QUEUE;

                                          }

                                         

                                          public void sender(){

                                                     Properties props = new Properties();

                                                     props.put("key", "value");

                                                     //동기 전송: sendEvent, 비동기 전송: postEvent

                                                     eventAdmin.sendEvent(new Event(EVENT_QUEUE, props));

                                          }

                                }

                    

                     1-2-2. spring.xml

                                <bean id="클래스Bean" class="클래스 경로" init-method="실행 메서드">

                                          <property name="EVENT_QUEUE" value="Topic주소" />

                                </bean>

          

                                <bean class="org.springframework.osgi.extensions.annotation.ServiceReferenceInjectionBeanPostProcessor" />

          

                     1-2-3. event를 보내기 위해 spring-osgi-annotation jar파일 필요

                                Maven을 사용한다면 dependency를 추가 하거나 아니며 spring-osgi-annotation jar파일이 필요

                                                     <dependency>

                                                                <groupId>org.springframework.osgi</groupId>

                                                               <artifactId>org.springframework.osgi.extensions.annotation</artifactId>

                                                                <version>1.2.1</version>

                                                     </dependency>

Posted by Brian B. Lee
Programming/OSGi2010. 10. 18. 09:50

본 내용은 실전 OSGi&SpringDM(위키북스) 책의 일부 내용입니다

Event Admin 서비스 사용

1. 기본 계념

           화이트보드 패턴을 이용하여 이벤트 메시지를 전달 한다.

           1-1. Event Object 속성

                     //이벤트 필터링을 이름 같은 것(일반적으로 kr/bit/uhealth 와 같이 계층구조를 가짐)

                     String Topic

                     //이벤트에 대한 추가 속성(속성 종류는 charter5-p114 참조)

                     Hashtable properties

          

2. Event Admin 구현

           2-2. 이벤트 핸들러로 이벤트 받기

                     Manifest.mf Import-Package org.osgi.service.event 추가

                     2-2-1. 인터페이스

                                public interface EventHandler {

                                          void handleEvnet(Event event);

                                }

                      2-2-2. 구현체

                                public class Activator implements BundleActivator, EventHandler {

                                          //모든 이벤트 받음

                                          final static String[] topics = new String[]{"*"};

                                         

                                          public void start(BundleContext context) throws Exception {

                                                     Dictionary<String, Object> prop = new Hashtable<String,Object>();

                                                     prop.put(EventContants.EVENT_TOPIC, topics);

                                                    

                                                     context.registerService(EventHandler.class.getName(), this, prop);

                                          }

                                         

                                          public void stop(BundleContext context) throws Exception {

                                                     ...

                                          }

                                          

                                          //인터페이스 구현체

                                          public void handleEvent(Event event) {

                                                     System.out.println("\r\n[Event Received] Topic : " + event.getTopic());

                                                     for(String key : event.getPropertyNames()){

                                                                System.out.println("\t[Property] " + key + " : " + event.getProperty(key));

                                                     }

                                          }

                                }

                               

                     2-2-3. 이벤트 필터링

                                //모든 이벤트 받기

                                final static String[] topics = new String[]{"*"};

                                //세부항목 이벤트 받기

                                final static String[] topics = new String[]{"org/osgi/framework/ServiceEvent/*"};

                                //세부 선택항목 이벤트 받기

                                final static String[] topics = new String[]{"org/osgi/framework/ServiceEvent/REGISTERED", "org/osgi/framework/ServiceEvent/STARTED"};

                                //그 밖에 필터 방법 http://www.ietf.org/rfc/rfc2254.txt 참조

          

           2-3. 이벤트 보내기

                     ServiceTracker tracker = new ServiceTracker(context, EventAdmin.class.getName(), null);

                    

                     tracker.open();

                    

                     Properties props = new Properties();

                     props.put("key", "value");

                     EventAdmin ea = (EventAdmin) tracker.getService();

                     if(ea != null) {

                                //동기 전송: sendEvent, 비동기 전송: postEvent

                                ea.sendEvent(new Event("com/foo/bar", props));                          

                     }

3. EventAdmin을 구현한 jar파일

Maven을 사용한다면 dependency를 추가 하거나 아니며 EventAdmin을 구현 jar파일이 필요

<dependency>

<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.eventadmin</artifactId>
<version>1.0.0</version>

</dependency>

Posted by Brian B. Lee