所以我花了好几天时间,但它仍然不适合我。
我正在发布一个更简单的示例,同时尝试按照教程[http://felix.apache.org/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/describing-components/event-admin-handlers.html]
进行操作任何帮助它的工作将非常感激。项目编译并运行。但发布者不发送任何数据。当我用户m_publisher.sendData(数据)时;从run(),starting()和停止()的输出到控制台也停止显示。
我做错了什么?
MyPubComponent.java
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;
@Component
@Instantiate
public class MyPubComponent implements Runnable{
@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();
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);
}
}
metadata.xml中
<?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>
的pom.xml
<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>
MySubComponent.java
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");
}
}
metadata.xml中
<?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