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