Spring Dynamic Modules


Spring Dynamic Modules

Zanim zaczniesz czytać ten wpis polecam Ci zapoznać się z artykułem https://javaleader.pl/2019/12/20/apache-servicemix-wprowadzenie/. Spring Dynamic Modules łączy technologię Spring z platformą OSGi. Spring Dynamic Modules udostępnia komponenty Spring’owe jako komponenty OSGi (widoczne dla innych komponentów OSGi). Do dzieła! Tworzymy nowy projekt Apache Maven:

Moduł – Producer – plik pom.xml:

<dependencies>
    <dependency>
        <groupId>org.springframework.osgi</groupId>
        <artifactId>spring-osgi</artifactId>
        <version>1.2.1</version>
        <type>pom</type>
    </dependency>
 
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>7.0</version>
    </dependency>
 
</dependencies>
 
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.0.2</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
 
        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <extensions>true</extensions>
            <configuration>
                <instructions>
                    <Bundle-SymbolicName>OSGiDmHelloWorldProvider</Bundle-SymbolicName>
                    <Export-Package>pl.javaleader.ws</Export-Package>
                    <Bundle-Vendor>Baptiste Wicht</Bundle-Vendor>
                </instructions>
            </configuration>
        </plugin>
    </plugins>
</build>

Interfejs – HelloWorldService :

public interface HelloWorldService {
    void hello();
}

implementacja – HelloWorldServiceImpl :

public class HelloWorldServiceImpl implements HelloWorldService {
    public void hello(){
        System.out.println("Hello World !");
    }
}

Plik ./resources/META-INF/spring/provider-context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xmlns:osgi="http://www.springframework.org/schema/osgi"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/osgi
            http://www.springframework.org/schema/osgi/spring-osgi.xsd
            http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
 
    <bean id="helloWorldService" class="pl.javaleader.ws.HelloWorldServiceImpl"/>
 
    <osgi:service ref="helloWorldService" interface="pl.javaleader.ws.HelloWorldService"/>
 
    <jaxws:endpoint
            id = "calcService"
            implementor = "pl.javaleader.ws.CalculatorImpl"
            address = "/calcService" />
 
</beans>

Moduł – Consumer – plik pom.xml:

<dependencies>
    <dependency>
        <groupId>pl.javaleader</groupId>
        <artifactId>javaleader-calc-osgi</artifactId>
        <version>1.0.1-SNAPSHOT</version>
    </dependency>
 
    <dependency>
        <groupId>org.springframework.osgi</groupId>
        <artifactId>spring-osgi</artifactId>
        <version>1.2.1</version>
        <type>pom</type>
    </dependency>
 
</dependencies>
 
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.0.2</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
 
        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <extensions>true</extensions>
            <configuration>
                <instructions>
                    <Bundle-SymbolicName>OSGiDmHelloWorldConsumer</Bundle-SymbolicName>
                    <Bundle-Vendor>Baptiste Wicht</Bundle-Vendor>
                </instructions>
            </configuration>
        </plugin>
    </plugins>
</build>

Plik ./resources/META-INF/spring/provider-context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:osgi="http://www.springframework.org/schema/osgi"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/osgi
            http://www.springframework.org/schema/osgi/spring-osgi.xsd">
 
    <bean id="consumer" class="pl.javaleader.HelloWorldConsumer" init-method="startTimer" destroy-method="stopTimer"
          lazy-init="false" >
        <constructor-arg ref="eventService"/>
    </bean>
 
    <osgi:reference id="eventService" interface="pl.javaleader.ws.HelloWorldService"/>
</beans>

Klasa HelloWorldConsumer:

public class HelloWorldConsumer implements ActionListener {
 
    private final Timer timer = new Timer(1000, this);
    private final HelloWorldService service;
 
    public HelloWorldConsumer(HelloWorldService service) {
        super();
 
        this.service = service;
    }
 
    public void startTimer(){
        timer.start();
    }
 
    public void stopTimer() {
        timer.stop();
    }
 
    @Override
    public void actionPerformed(ActionEvent e) {
        service.hello();
    }
}

instalujemy obydwa moduły w lokalnym repozytorium mavena:

mvn install

uruchamiamy Apache ServiceMix i instalujemy moduł producenta:

install mvn:pl.javaleader/javaleader-calc-osgi/1.0.0-SNAPSHOT

moduł startujemy!

start 342

gdzie 342 to nadany numer Bundle.

instalujemy moduł konsumenta:

install mvn:OSGiDmHelloWorldConsumer/OSGiDmHelloWorldConsumer/1.0.0-SNAPSHOT

moduł startujemy:

start 343

gdzie 343 to nadany numer Bundle.

wynik:

jenkins@root>Hello World !
Hello World !
Hello World !
Hello World !
Hello World !
Hello World !

Zobacz kod na GitHubie i zapisz się na bezpłatny newsletter!

.


Leave a comment

Your email address will not be published.


*