Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ipojo.osgi.example.pub;
- import org.apache.felix.ipojo.annotations.Component;
- import org.apache.felix.ipojo.annotations.Instantiate;
- import org.apache.felix.ipojo.annotations.Invalidate;
- import org.apache.felix.ipojo.annotations.Validate;
- import org.apache.felix.ipojo.handlers.event.Publishes;
- import org.apache.felix.ipojo.handlers.event.publisher.Publisher;
- import org.osgi.service.event.Event;
- //import org.slf4j.Logger;
- //import org.slf4j.LoggerFactory;
- @Component
- @Instantiate
- public class MyPubComponent implements Runnable{
- //private static Logger s_logger = LoggerFactory.getLogger(MyPubComponent.class);
- @Publishes( // or @Publisher before the 1.7.0
- name="myPublisher",
- topics="mssg",
- dataKey="my.data",
- synchronous=true
- )
- private Publisher m_publisher;
- @Validate
- public void starting() {
- Thread thread = new Thread(this);
- thread.start();
- //s_logger.info("EventPublisher started.");
- System.out.println("Publisher Started");
- }
- @Invalidate
- public void stopping() {
- // s_logger.info("EventPublihser stoppted");
- System.out.println("Publisher Stopped");
- }
- @Override
- public void run() {
- String data = "hello world";
- System.out.println("Sending data...");
- m_publisher.sendData(data);
- }
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <ipojo
- xmlns:ev="org.apache.felix.ipojo.handlers.event">
- <component className="ipojo.osgi.example.pub.MyPubComponent">
- <ev:publisher
- name="myPublisher"
- field="m_publisher"
- topics="mssg"
- data-key="my.data"
- synchronous=true />
- </component>
- <instance component="ipojo.osgi.example.pub.MyPubComponent" name="dataPublisher"/>
- </ipojo>
- <project>
- <modelVersion>4.0.0</modelVersion>
- <packaging>bundle</packaging>
- <groupId>ipojo.osgi.example</groupId>
- <artifactId>mypub</artifactId>
- <version>1.0.0</version>
- <name>Publisher</name>
- <dependencies>
- <dependency>
- <groupId>org.apache.felix</groupId>
- <artifactId>org.apache.felix.ipojo.handler.eventadmin</artifactId>
- <version>1.8.0</version>
- <type>jar</type>
- </dependency>
- <dependency>
- <groupId>org.apache.felix</groupId>
- <artifactId>org.apache.felix.ipojo.annotations</artifactId>
- <version>1.8.4</version>
- <type>jar</type>
- </dependency>
- <dependency>
- <groupId>org.osgi</groupId>
- <artifactId>org.osgi.service.event</artifactId>
- <version>1.3.1</version>
- </dependency>
- <dependency>
- <groupId>org.osgi</groupId>
- <artifactId>org.osgi.core</artifactId>
- <version>4.2.0</version>
- </dependency>
- <dependency>
- <groupId>org.apache.felix</groupId>
- <artifactId>org.apache.felix.ipojo</artifactId>
- <version>1.12.1</version>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.felix</groupId>
- <artifactId>maven-bundle-plugin</artifactId>
- <version>3.5.0</version>
- <extensions>true</extensions>
- <configuration>
- <instructions>
- <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
- <Private-Package>ipojo.osgi.example.mypub</Private-Package>
- </instructions>
- </configuration>
- </plugin>
- <plugin>
- <groupId>org.apache.felix</groupId>
- <artifactId>maven-ipojo-plugin</artifactId>
- <version>1.12.1</version>
- <executions>
- <execution>
- <goals>
- <goal>ipojo-bundle</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
- </project>
- package ipojo.osgi.example.sub;
- import org.apache.felix.ipojo.annotations.Component;
- import org.apache.felix.ipojo.annotations.Instantiate;
- import org.apache.felix.ipojo.annotations.Validate;
- import org.apache.felix.ipojo.annotations.Invalidate;
- import org.apache.felix.ipojo.handlers.event.Subscriber;
- @Component
- @Instantiate
- public class MySubComponent implements Runnable {
- @Subscriber(
- name="mySubscriber",
- topics="mssg",
- dataKey="my.data",
- dataType="java.lang.String")
- private void handleData(String s) {
- // Event received
- // Do something with the event
- System.out.println("Recieved = "+s);
- }
- @Validate
- public void start() {
- Thread thread = new Thread(this);
- thread.start();
- System.out.println("Reciever Started");
- }
- @Invalidate
- public void stop() {
- System.out.println("Reciever Stopped");
- }
- @Override
- public void run() {
- System.out.println("Reciever Running");
- }
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <ipojo
- xmlns:ev="org.apache.felix.ipojo.handlers.event">
- <component className="ipojo.osgi.example.sub.MySubComponent">
- <ev:subscriber
- name="mySubscriber"
- callback="handleData"
- topics="mssg"
- data-key="my.data"
- data-type="String"
- synchronours=true />
- </component>
- <instance component="ipojo.osgi.example.sub.MySubComponent" name="DataReciever"/>
- </ipojo>
- g! Reciever Started
- Reciever Running
- lb
- START LEVEL 1
- ID|State |Level|Name
- 0|Active | 0|System Bundle (4.2.1)
- 1|Active | 1|Apache Felix Bundle Repository (1.6.6)
- 2|Active | 1|Apache Felix Gogo Command (0.12.0)
- 3|Active | 1|Apache Felix Gogo Runtime (0.10.0)
- 4|Active | 1|Apache Felix Gogo Shell (0.10.0)
- 5|Active | 1|Apache Felix iPOJO (1.12.1)
- 6|Active | 1|Apache Felix iPOJO Gogo Command (1.12.1)
- 7|Active | 1|Apache Felix iPOJO Event Admin Handler (1.8.0)
- 8|Active | 1|org.osgi:org.osgi.service.event (1.3.1.201505202024)
- 9|Active | 1|Subscriber (1.0.0)
- 10|Active | 1|Publisher (1.0.0)
- g
Add Comment
Please, Sign In to add comment