2012-06-13 6 views
1

선언적 OSGi 서비스에 대한 질문이 있습니다.선언적 OSGi 서비스

public interface PrintService { 
    public void print(); 
    } 

및 구현 : 나는 다음과 같은 인터페이스가

public class PrintServiceImpl implements PrintService { 

     @Override 
     public void print() { 
     System.out.println("Hello from PrintServiceImpl!"); 
     } 
    } 

OSGI-INF/component.xml :

<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="service"> 
    <implementation class="service.PrintServiceImpl"/> 
    <service> 
     <provide interface="print.PrintService"/> 
    </service> 
</scr:component> 

MANIFEST.MF :

Service-Component: OSGI-INF/component.xml 

서비스를 설치하고 시작한 후 아무것도 일어나지 않습니다. 어떻게 활성화하고 "Hello from PrintServiceImpl!"을 콘솔에 출력 할 수 있습니까?

+0

관리 콘솔에서 신고 된 서비스를 볼 수 있습니까? 그것은 활동적입니까? –

+0

예 "ss"명령을 사용하며 활성화되어 있지만 "ls"명령은 "불만족"상태입니다. –

+1

'org.eclipse.equinox.ds' 번들이 있고 실행 구성에 추가 된 종속성이 있는지 확인하십시오. –

답변

2

print 메서드를 호출하는 이유는 무엇입니까? 이것은 서비스의 인터페이스의 일부이므로, 바인딩하고 호출하는 클라이언트가 될 때까지는 호출되지 않습니다.

콘솔에 services 명령을 입력하면 번들이 print.PrintService 서비스를 게시하고 있음을 확인할 수 있습니다. 즉, 구성 요소가 작동 중임을 의미합니다. 이것을 보지 못한다면 위의 주석에서 Tom Seidel이 제안한 SCR 번들과 같은 것을 누락했을 수 있습니다.

+0

인쇄 방법은 어떻게 호출 할 수 있습니까? –

+0

다른 번들의 다른 코드는 서비스를 가져와 메소드를 호출해야합니다. – Robin

+0

전화를 걸 클라이언트가없는 경우 왜 인쇄 방법을 작성 했습니까? –

1

아마도 정품 인증 방법으로 인쇄하고 싶습니까?

public class PrintServiceImpl implements PrintService { 
    protected final void activate() { 
     System.out.println("Hello from PrintServiceImpl!"); 
    } 
    protected final void deactivate() { 
     System.out.println("Goodbye from PrintServiceImpl!"); 
    } 
    ... 
} 

그렇지 않으면, 닐의 대답은 바로 하나입니다 : 당신은 ServiceTracker를하거나 <reference>를 통해 서비스를 사용하여 명시 적으로 인쇄() 메소드를 호출하는 클라이언트를합니다.

+0

답변 주셔서 감사합니다, 나는 액티베이터 XML 파일에서 활성화 메서드를 전달할 수있는 것으로 나타났습니다 :) –

+0

이것은 정품 인증 방법으로 임의의 메서드를 지정할 수 있다는 사실입니다. 코드의 유지 보수성을 위해 다른 이름을 지정하는 강력한 이전 버전과의 호환성이없는 경우가 아니면 "활성화"라는 이름을 사용하는 것이 좋습니다. activate() 메서드는 항상 다른 레거시 메서드를 호출 할 수 있습니다. –

3

번들이 시작될 때 서비스가 시작되도록 명시해야합니다. 그렇지 않은 경우, 지연이 발생 해 인스턴스가 생성되어 PrintService를 필요로하는 다른 서비스가없는 경우, 인스턴스화되지 않습니다.

enabledimmediate 속성을 사용하여 서비스 라이프 사이클 (번들 수명주기와 관련하여)을 제어 할 수 있습니다. enabled is trivial : 번들이 시작될 때 서비스를 사용할 수 있는지 여부를 나타냅니다. immediate은이 서비스가 게으른 지 여부를 나타냅니다. immediate="true"immediatly, immediate="false" 게으른 인스턴스를 적용하여 서비스를 시작합니다 예를 들어,

(즉, 대기 다른 서비스 요청 종속성으로이 서비스까지) 크리스가 언급 한 바와 같이 당신이 그것을 활성화 때 뭔가를 당신의 서비스를 원하는 경우

<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="PrintService" 
activate="activate" 
deactivate="deactivate" 
modified="modified" 
enabled="true" 
immediate="true"> 

는, 당신은 activate 라이프 사이클 방법에 몇 가지 코드를 추가해야합니다 (메소드의 이름이 그와 같은 구성 요소 설명에 지정 될 수 있습니다 위에서 명시 적으로 수행되었습니다.)

protected final void activate() { 
    print(); 
} 
1

위의 모든 대답이 맞습니다.지금 당신은 뭔가를 쓸 수 쉘에 뭔가를 인쇄하는 식으로 뭔가를해야합니다 :

public interface PrintService { 
    public void print(); 
} 

Implentation :

public class PrintServiceImpl implements PrintService { 

     @Override 
     public void print() { 
     System.out.println("Hello from PrintServiceImpl!"); 
     } 
} 

component.xml :

<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="service"> 
    <implementation class="service.PrintServiceImpl"/> 
    <service> 
     <provide interface="print.PrintService"/> 
    </service> 
</scr:component> 

그리고 지금 당신이 명령을 사용할 수 있습니다 인쇄 방법을 호출하려면 :

public class PrintCommand implements Command { 

    private PrintService printer; 

    public void setPrinter(PrintService printer) { 
     this.printer = printer; 
    } 

    public void unsetPrinter(PrintService printer) { 
     this.printer = null; 
    } 

    @Override 
    public void execute(String line, PrintStream arg1, PrintStream arg2) {  
     printer.print(); 
    } 

    @Override 
    public String getName() { 
     return "print"; 
    } 

    @Override 
    public String getShortDescription() { 
     return "just a printer"; 
    } 

    @Override 
    public String getUsage() { 
     return "print"; 
    } 

} 

그리고 당신의 component.xml는 새 항목이 있어야합니다

<component name="PrintCommand"> 
    <implementation class="PrintCommand"/> 
    <service> 
     <provide interface="org.apache.felix.shell.Command"/> 
    </service> 
    <reference 
     name="printer" 
     interface="PrintService" 
     bind="setPrinter" 
     unbind="unsetPrinter" 
     cardinality="1..1" 
     policy="static" 
    /> 
</component> 

명령 인터페이스 아파치 펠릭스 셸의 일부이며 지금 펠릭스를 실행하고 콘솔에 help를 입력 할 경우 등록 된 모든 명령을 볼 수 있습니다을하고있다 PrintCommand도 print입니다. 쉘에 print를 입력하면 텍스트가 표시됩니다.

0

혹시이 기능을 사용하셨습니까? Eclipse의 .ds 번들은 아직 시작되지 않았기 때문에 (아직) 등록되지 않았다. 수동으로 시작했을 때 서비스가 올바르게 등록되었습니다.

관련 문제