Guest User

Untitled

a guest
Mar 19th, 2018
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.56 KB | None | 0 0
  1. package ipojo.osgi.example.pub;
  2. import org.apache.felix.ipojo.annotations.Component;
  3. import org.apache.felix.ipojo.annotations.Instantiate;
  4. import org.apache.felix.ipojo.annotations.Invalidate;
  5. import org.apache.felix.ipojo.annotations.Validate;
  6. import org.apache.felix.ipojo.handlers.event.Publishes;
  7. import org.apache.felix.ipojo.handlers.event.publisher.Publisher;
  8. import org.osgi.service.event.Event;
  9. //import org.slf4j.Logger;
  10. //import org.slf4j.LoggerFactory;
  11.  
  12. @Component
  13. @Instantiate
  14. public class MyPubComponent implements Runnable{
  15.  
  16. //private static Logger s_logger = LoggerFactory.getLogger(MyPubComponent.class);
  17.  
  18. @Publishes( // or @Publisher before the 1.7.0
  19. name="myPublisher",
  20. topics="mssg",
  21. dataKey="my.data",
  22. synchronous=true
  23. )
  24. private Publisher m_publisher;
  25.  
  26. @Validate
  27. public void starting() {
  28. Thread thread = new Thread(this);
  29. thread.start();
  30. //s_logger.info("EventPublisher started.");
  31. System.out.println("Publisher Started");
  32. }
  33.  
  34. @Invalidate
  35. public void stopping() {
  36. // s_logger.info("EventPublihser stoppted");
  37. System.out.println("Publisher Stopped");
  38.  
  39. }
  40.  
  41. @Override
  42. public void run() {
  43. String data = "hello world";
  44. System.out.println("Sending data...");
  45. m_publisher.sendData(data);
  46. }
  47. }
  48.  
  49. <?xml version="1.0" encoding="UTF-8"?>
  50. <ipojo
  51. xmlns:ev="org.apache.felix.ipojo.handlers.event">
  52. <component className="ipojo.osgi.example.pub.MyPubComponent">
  53. <ev:publisher
  54. name="myPublisher"
  55. field="m_publisher"
  56. topics="mssg"
  57. data-key="my.data"
  58. synchronous=true />
  59. </component>
  60. <instance component="ipojo.osgi.example.pub.MyPubComponent" name="dataPublisher"/>
  61. </ipojo>
  62.  
  63. <project>
  64. <modelVersion>4.0.0</modelVersion>
  65. <packaging>bundle</packaging>
  66. <groupId>ipojo.osgi.example</groupId>
  67. <artifactId>mypub</artifactId>
  68. <version>1.0.0</version>
  69. <name>Publisher</name>
  70.  
  71. <dependencies>
  72. <dependency>
  73. <groupId>org.apache.felix</groupId>
  74. <artifactId>org.apache.felix.ipojo.handler.eventadmin</artifactId>
  75. <version>1.8.0</version>
  76. <type>jar</type>
  77. </dependency>
  78. <dependency>
  79. <groupId>org.apache.felix</groupId>
  80. <artifactId>org.apache.felix.ipojo.annotations</artifactId>
  81. <version>1.8.4</version>
  82. <type>jar</type>
  83. </dependency>
  84. <dependency>
  85. <groupId>org.osgi</groupId>
  86. <artifactId>org.osgi.service.event</artifactId>
  87. <version>1.3.1</version>
  88. </dependency>
  89. <dependency>
  90. <groupId>org.osgi</groupId>
  91. <artifactId>org.osgi.core</artifactId>
  92. <version>4.2.0</version>
  93. </dependency>
  94. <dependency>
  95. <groupId>org.apache.felix</groupId>
  96. <artifactId>org.apache.felix.ipojo</artifactId>
  97. <version>1.12.1</version>
  98. </dependency>
  99. </dependencies>
  100.  
  101. <build>
  102. <plugins>
  103. <plugin>
  104. <groupId>org.apache.felix</groupId>
  105. <artifactId>maven-bundle-plugin</artifactId>
  106. <version>3.5.0</version>
  107. <extensions>true</extensions>
  108. <configuration>
  109. <instructions>
  110. <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
  111. <Private-Package>ipojo.osgi.example.mypub</Private-Package>
  112. </instructions>
  113. </configuration>
  114. </plugin>
  115. <plugin>
  116. <groupId>org.apache.felix</groupId>
  117. <artifactId>maven-ipojo-plugin</artifactId>
  118. <version>1.12.1</version>
  119. <executions>
  120. <execution>
  121. <goals>
  122. <goal>ipojo-bundle</goal>
  123. </goals>
  124. </execution>
  125. </executions>
  126. </plugin>
  127. </plugins>
  128. </build>
  129. </project>
  130.  
  131. package ipojo.osgi.example.sub;
  132.  
  133. import org.apache.felix.ipojo.annotations.Component;
  134. import org.apache.felix.ipojo.annotations.Instantiate;
  135. import org.apache.felix.ipojo.annotations.Validate;
  136. import org.apache.felix.ipojo.annotations.Invalidate;
  137. import org.apache.felix.ipojo.handlers.event.Subscriber;
  138.  
  139. @Component
  140. @Instantiate
  141. public class MySubComponent implements Runnable {
  142.  
  143. @Subscriber(
  144. name="mySubscriber",
  145. topics="mssg",
  146. dataKey="my.data",
  147. dataType="java.lang.String")
  148. private void handleData(String s) {
  149. // Event received
  150. // Do something with the event
  151. System.out.println("Recieved = "+s);
  152. }
  153.  
  154. @Validate
  155. public void start() {
  156. Thread thread = new Thread(this);
  157. thread.start();
  158. System.out.println("Reciever Started");
  159. }
  160.  
  161. @Invalidate
  162. public void stop() {
  163. System.out.println("Reciever Stopped");
  164. }
  165.  
  166. @Override
  167. public void run() {
  168. System.out.println("Reciever Running");
  169. }
  170. }
  171.  
  172. <?xml version="1.0" encoding="UTF-8"?>
  173. <ipojo
  174. xmlns:ev="org.apache.felix.ipojo.handlers.event">
  175. <component className="ipojo.osgi.example.sub.MySubComponent">
  176. <ev:subscriber
  177. name="mySubscriber"
  178. callback="handleData"
  179. topics="mssg"
  180. data-key="my.data"
  181. data-type="String"
  182. synchronours=true />
  183. </component>
  184. <instance component="ipojo.osgi.example.sub.MySubComponent" name="DataReciever"/>
  185. </ipojo>
  186.  
  187. g! Reciever Started
  188. Reciever Running
  189. lb
  190. START LEVEL 1
  191. ID|State |Level|Name
  192. 0|Active | 0|System Bundle (4.2.1)
  193. 1|Active | 1|Apache Felix Bundle Repository (1.6.6)
  194. 2|Active | 1|Apache Felix Gogo Command (0.12.0)
  195. 3|Active | 1|Apache Felix Gogo Runtime (0.10.0)
  196. 4|Active | 1|Apache Felix Gogo Shell (0.10.0)
  197. 5|Active | 1|Apache Felix iPOJO (1.12.1)
  198. 6|Active | 1|Apache Felix iPOJO Gogo Command (1.12.1)
  199. 7|Active | 1|Apache Felix iPOJO Event Admin Handler (1.8.0)
  200. 8|Active | 1|org.osgi:org.osgi.service.event (1.3.1.201505202024)
  201. 9|Active | 1|Subscriber (1.0.0)
  202. 10|Active | 1|Publisher (1.0.0)
  203. g
Add Comment
Please, Sign In to add comment