-
Notifications
You must be signed in to change notification settings - Fork 8
/
service.typedevent.html
6137 lines (6037 loc) · 741 KB
/
service.typedevent.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>157 Typed Event Service Specification - OSGi Compendium 8</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.78.1" />
<link rel="home" href="index.html" title="OSGi Compendium" />
<link rel="up" href="index.html" title="OSGi Compendium" />
<link rel="prev" href="tr157softwaremodules.html" title="155 TR-157 Amendment 3 Software Module Guidelines" />
<link rel="next" href="service.log.stream.html" title="158 Log Stream Provider Service Specification" />
<meta name="Section-title" content="157 Typed Event Service Specification" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="shortcut icon" href="images/favicon.png" type="image/x-icon" />
<link rel="stylesheet" type="text/css" href="css/custom.css" />
<link rel="stylesheet" type="text/css" href="css/github.css" />
<link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=Ubuntu:regular,bold&subset=Latin" /><script type="text/javascript" src="js/highlight.pack.js"></script><script type="text/javascript" src="js/main.js"></script></head>
<body>
<div id="fullbody">
<div id="header">
<div class="menu-top-container"></div>
<div id="shadow-block"><a class="logo" href="index.html"><img src="images/OSGi.svg" alt="OSGi Working Group Documentation" /><h1>OSGi Compendium Release 8</h1></a></div>
</div>
<div id="mobile-menu-icon">⋮</div>
<div id="column-two">
<div id="content">
<div id="scrollable">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr>
<td width="20%" align="left"><a accesskey="p" href="tr157softwaremodules.html">Prev</a>
</td>
<th width="60%" align="center"> </th>
<td width="20%" align="right"> <a accesskey="n" href="service.log.stream.html">Next</a></td>
</tr>
</table>
<hr />
</div>
<div class="chapter">
<div xmlns="" class="titlepage">
<div>
<div>
<h1 xmlns="http://www.w3.org/1999/xhtml" class="title"><a xmlns="" class="anchor" id="service.typedevent"></a><span xmlns="" class="number">157</span> Typed Event Service Specification
</h1>
</div>
<div>
<p xmlns="http://www.w3.org/1999/xhtml" class="releaseinfo"><a xmlns="" class="xref" href="service.typedevent.html#org.osgi.service.typedevent" title="157.9 org.osgi.service.typedevent">Version 1.0</a></p>
</div>
</div>
</div>
<div class="section">
<div xmlns="" class="titlepage">
<div>
<div>
<h2 xmlns="http://www.w3.org/1999/xhtml" class="title" style="clear: both"><a xmlns="" class="anchor" id="d0e151838"></a><span xmlns="" class="number">157.1</span> Introduction
</h2>
</div>
</div>
</div>
<p>Eventing systems are a common part of software programs, used to
distribute information between parts of an application. To address this,
the <a xmlns="" class="xref" href="service.event.html" title="113 Event Admin Service Specification"><em xmlns="http://www.w3.org/1999/xhtml">Event Admin Service Specification</em></a> was created as one of the earliest
specifications defined by the OSGi Compendium. The design and usage of the
Event Admin specification, however, makes certain trade-offs that do not
fit well with modern application design:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
<p><span class="emphasis"><em>Type Safety</em></span> - Events are sent and received
as opaque maps of key-value pairs. The “schema” of an event is
therefore ill-defined and relies on “magic strings” being used
correctly to locate data, and on careful handling of data values with
unknown types.
</p>
</li>
<li class="listitem">
<p><span class="emphasis"><em>Unhandled Events</em></span> - Events that are sent but
have no interested Event Consumers are silently discarded. There is no
way to know that an event was not handled, short of disrupting the
system by registering a handler for <span class="emphasis"><em>all</em></span>
events.
</p>
</li>
<li class="listitem">
<p><span class="emphasis"><em>Observability</em></span> - There is no simple,
non-invasive way to monitor the flow of events through the system. The
ability to monitor and profile applications using Event Admin is
therefore relatively limited.
</p>
</li>
</ul>
</div>
<p>Adding these features to the original <a xmlns="" class="xref" href="service.event.html" title="113 Event Admin Service Specification"><em xmlns="http://www.w3.org/1999/xhtml">Event Admin Service Specification</em></a> specification is not feasible without breaking
backward compatibility for clients. Therefore this specification exists to
provide an alternative eventing model which supports these different
requirements by making different design trade-offs.
</p>
<div class="section">
<div xmlns="" class="titlepage">
<div>
<div>
<h3 xmlns="http://www.w3.org/1999/xhtml" class="title"><a xmlns="" class="anchor" id="d0e151868"></a><span xmlns="" class="number">157.1.1</span> Essentials
</h3>
</div>
</div>
</div>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
<p><span class="emphasis"><em>Event</em></span> - A set of data created by an Event
Source, encapsulated as an object and delivered to one or more Event
Consumers.
</p>
</li>
<li class="listitem">
<p><span class="emphasis"><em>Event Schema</em></span> - A definition of the
expected data layout within an event, including the names of data
fields and the types of data that they contain.
</p>
</li>
<li class="listitem">
<p><span class="emphasis"><em>Event Topic</em></span> - A String identifying the
<span class="emphasis"><em>topic</em></span> of an Event, effectively defining the
Event Schema and the purpose of the event.
</p>
</li>
<li class="listitem">
<p><span class="emphasis"><em>Event Source</em></span> - A software component which
creates and sends events.
</p>
</li>
<li class="listitem">
<p><span class="emphasis"><em>Event Consumer</em></span> - A software component
which receives events.
</p>
</li>
<li class="listitem">
<p><span class="emphasis"><em>DTO</em></span> - A Data Transfer Object as per the
OSGi DTO Specification.
</p>
</li>
<li class="listitem">
<p><span class="emphasis"><em>Event Bus</em></span> - A software component used by
an Event Source and responsible for delivering Events to Event
Consumers.
</p>
</li>
</ul>
</div>
<div class="figure"><a xmlns="" class="anchor" id="d0e151910"></a><p class="title"><strong>Figure <span xmlns="" class="number">157</span>.1 Class and Service overview</strong></p>
<div class="figure-contents">
<div class="mediaobject" align="center"><img src="images/157-typed-event-classes.png" align="middle" width="630" height="360" alt="Class and Service overview" /></div>
</div>
</div><br class="figure-break" /></div>
<div class="section">
<div xmlns="" class="titlepage">
<div>
<div>
<h3 xmlns="http://www.w3.org/1999/xhtml" class="title"><a xmlns="" class="anchor" id="d0e151916"></a><span xmlns="" class="number">157.1.2</span> Entities
</h3>
</div>
</div>
</div>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
<p><span class="emphasis"><em>Typed Event Bus</em></span> - A service registered by
the Typed Event implementation that can be passed an Event object
and that will distribute that event to any suitable Event Handler
Services.
</p>
</li>
<li class="listitem">
<p><span class="emphasis"><em>Event Handler</em></span> - A service registered by
an Event Consumer suitable for receiving Event data from the Typed
Event Bus.
</p>
</li>
</ul>
</div>
</div>
</div>
<div class="section">
<div xmlns="" class="titlepage">
<div>
<div>
<h2 xmlns="http://www.w3.org/1999/xhtml" class="title" style="clear: both"><a xmlns="" class="anchor" id="d0e151930"></a><span xmlns="" class="number">157.2</span> Events
</h2>
</div>
</div>
</div>
<p>In this specification an Event is a set of string keys associated
with data values. The defined set of allowable keys and permitted value
types for the keys in an Event is known as the Event Schema. Both the
Event Source and Event Consumers must agree on a schema, or set of
compatible schemas, in order for events to be consumed correctly.
</p>
<div class="section">
<div xmlns="" class="titlepage">
<div>
<div>
<h3 xmlns="http://www.w3.org/1999/xhtml" class="title"><a xmlns="" class="anchor" id="d0e151935"></a><span xmlns="" class="number">157.2.1</span> Type Safe Events
</h3>
</div>
</div>
</div>
<p>A Type Safe Event is one in which the Event Schema is defined as a
Java class. Using a Java class provides a formal definition of the
schema - event data uses field names in the class as the keys, and each
field definition defines the permitted type of the value.
</p>
<p>Type Safe Event classes are expected to conform to OSGi DTO rules.
The architecture of OSGi DTOs is described in <a xmlns="" class="xref" href="introduction.html#intro.core.release" title="1.2.1 OSGi Core Release 8">OSGi Core Release 8</a>. All methods, all
static fields, and any non public instance fields of an event object
must be ignored by the Typed Event Service when processing the Event
data.
</p>
<p>Some implementations of the Typed Event Service may support Type
Safe Event classes that do not conform to the DTO rules, transforming
them as needed in an implementation specific way. This is permitted by
this specification, however consumers which rely on this behaviour may
not be portable between different implementations of this
specification.
</p>
<div class="section">
<div xmlns="" class="titlepage">
<div>
<div>
<h4 xmlns="http://www.w3.org/1999/xhtml" class="title"><a xmlns="" class="anchor" id="d0e151946"></a><span xmlns="" class="number">157.2.1.1</span> Nested Data Structures
</h4>
</div>
</div>
</div>
<p>OSGi DTOs are permitted to have data values which are also DTOs,
allowing nested data structures to be created. This is also allowed
for Type Safe Events, but with the same restriction that the event
data must be a tree. There is no restriction on the depth of nesting
permitted.
</p>
</div>
</div>
<div class="section">
<div xmlns="" class="titlepage">
<div>
<div>
<h3 xmlns="http://www.w3.org/1999/xhtml" class="title"><a xmlns="" class="anchor" id="d0e151951"></a><span xmlns="" class="number">157.2.2</span> Untyped Events
</h3>
</div>
</div>
</div>
<p>An Untyped Event is one in which there is no Java class defining
the Event Schema. In this case the event data is defined using a
<code class="code">Map</code> type with <code class="code">String</code> keys and values limited
to types acceptable as fields in a DTO, excepting:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
<p>DTO types - an untyped event may not have DTOs inside it as
these form part of a typed schema.
</p>
</li>
<li class="listitem">
<p>Maps are only permitted if they follow the rules for Untyped
events, that is having <code class="code">String</code> keys and DTO restricted
value types excluding DTOs.
</p>
</li>
</ul>
</div>
<p>Untyped Event instances are capable of representing exactly the
same data as present in a Type Safe Event instance, and are also subject
to the same restrictions, that is the data must be a tree. Nested data
should be included as sub-maps within the event map, and these sub-maps
may in turn contain nested data.
</p>
</div>
<div class="section">
<div xmlns="" class="titlepage">
<div>
<div>
<h3 xmlns="http://www.w3.org/1999/xhtml" class="title"><a xmlns="" class="anchor" id="d0e151974"></a><span xmlns="" class="number">157.2.3</span> Non Standard Type Safe Events
</h3>
</div>
</div>
</div>
<p>Some Event schemas may be represented by an existing type which
does not match the OSGi DTO rules. In this case there are two main
options:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
<p>Create a DTO representation of the event schema, and convert
from the existing type into the DTO representation in code.
</p>
</li>
<li class="listitem">
<p>Convert the event data into an Untyped Event representation
using nested Maps.
</p>
</li>
</ul>
</div>
<p>For example, the following code demonstrates how an object
following the JavaBeans pattern can be converted into a DTO type or an
untyped map:
</p><pre xmlns="" class="programlisting"><code>public class ExampleJavaBean {
private String message;
public String getMessage() { return message; }
public void setMessage(String message) { this.message = message; }
}
public class ExampleEvent {
public String message;
}
@Component
public class ExampleEventSource {
private ExampleEvent createEventFromJavaBean(ExampleJavaBean bean) {
return Converters.standardConverter().convert(bean)
.to(ExampleEvent.class);
}
private Map<String, Object> createMapFromJavaBean(ExampleJavaBean bean) {
return Converters.standardConverter().convert(bean)
.to(new TypeReference<Map<String, Object>>(){});
}
}</code></pre></div>
<div class="section">
<div xmlns="" class="titlepage">
<div>
<div>
<h3 xmlns="http://www.w3.org/1999/xhtml" class="title"><a xmlns="" class="anchor" id="d0e151990"></a><span xmlns="" class="number">157.2.4</span> Event Mutability and Thread Safety
</h3>
</div>
</div>
</div>
<p>The Typed Event Service is inherently multi-threaded. Events may
be published from multiple threads, and event data may be delivered to
consumers on multiple threads. Event Sources and Event Consumers must
therefore assume that event data is shared between threads from the
moment that it is first passed to the <code class="code">TypedEventBus</code>.
</p>
<div class="section">
<div xmlns="" class="titlepage">
<div>
<div>
<h4 xmlns="http://www.w3.org/1999/xhtml" class="title"><a xmlns="" class="anchor" id="d0e151998"></a><span xmlns="" class="number">157.2.4.1</span> Typed Event Mutability
</h4>
</div>
</div>
</div>
<p>Typed Events, and in particular DTO types, provide a simple yet
powerful mechanism for defining an Event Schema in a type-safe way.
However their use of mutable public fields means that they are
potentially dangerous when shared between threads. Event Sources and
Event Consumers should assume that their event instances are shared
between threads and therefore not mutate the event data after
publication or receipt.
</p>
<p>If an Event Handler does need to make changes to an incoming
event then it must copy the event data into a new DTO instance. Note
that any nested DTO values in the event data must also be copied if
they are to be mutated.
</p>
</div>
<div class="section">
<div xmlns="" class="titlepage">
<div>
<div>
<h4 xmlns="http://www.w3.org/1999/xhtml" class="title"><a xmlns="" class="anchor" id="d0e152005"></a><span xmlns="" class="number">157.2.4.2</span> Untyped Event Mutability
</h4>
</div>
</div>
</div>
<p>When an event source publishes untyped event data, it passes a
Map instance to the Typed Event Bus. The Typed Event Bus is not
required to take a copy of this Map, and therefore the event source
must not change the Map, or any data structures within the Map, after
the call to <a xmlns="" class="xref" href="service.typedevent.html#org.osgi.service.typedevent.TypedEventBus.deliverUntyped-String-Map-" title="157.9.3.3 public void deliverUntyped(String topic, Map<String, ?> event)">deliverUntyped(String,Map)</a>.
</p>
<p>Untyped Events are delivered as implementations of the Map
interface. Bundles consuming untyped events should not rely on the
event object being any particular implementation of Map, and should
treat the event object as immutable. The Typed Event Bus
implementation may make copies of the event data, or enforce the
immutability of the map, before passing the event data to an Event
Handler.
</p>
</div>
</div>
</div>
<div class="section">
<div xmlns="" class="titlepage">
<div>
<div>
<h2 xmlns="http://www.w3.org/1999/xhtml" class="title" style="clear: both"><a xmlns="" class="anchor" id="d0e152014"></a><span xmlns="" class="number">157.3</span> Publishing Events
</h2>
</div>
</div>
</div>
<p>To publish an event, the Event Source must retrieve the Typed Event
Bus service from the OSGi service registry. The Event Source then creates
an event object and calls one of the Typed Event Bus service's methods to
publish the event. Event publication is asynchronous, meaning that when a
call to the Typed Event Bus returns there is no guarantee that all, or
even any, listeners have been notified.
</p>
<div class="section">
<div xmlns="" class="titlepage">
<div>
<div>
<h3 xmlns="http://www.w3.org/1999/xhtml" class="title"><a xmlns="" class="anchor" id="typedevent.event.topics"></a><span xmlns="" class="number">157.3.1</span> Event Topics
</h3>
</div>
</div>
</div>
<p>Events are always published to a topic. The topic of an event
defines the <span class="emphasis"><em>schema</em></span> of the event. Topics exist in
order to give Event Consumers information about the schema of the event,
and the opportunity to register for just the events they are interested
in. When a topic is designed, its name should not include any other
information, such as the publisher of the event or the data associated
with the event, those parts are intended to be stored in the event
properties.
</p>
<p>The topic therefore serves as a first-level filter for determining
which handlers should receive the event. Typed Event service
implementations use the structure of the topic to optimize the
dispatching of the events to the handlers. The following example code
demonstrates how to send an event to a topic.
</p><pre xmlns="" class="programlisting"><code>public class ExampleEvent {
public String message;
}
@Component
public class ExampleEventSource {
@Reference
TypedEventBus bus;
public void sendEvent() {
ExampleEvent event = new ExampleEvent();
event.message = "The time is " + LocalDateTime.now();
bus.deliver("org/osgi/example/ExampleEvent", event);
}
}</code></pre><p>Topics are arranged in a hierarchical namespace. Each level is
defined by a token and levels are separated by solidi (<code class="code">'/'
\u002F</code>). More precisely, the topic must conform to the following
grammar:
</p><pre xmlns="" class="programlisting"><code>// For further information see General Syntax Definitions in Core
topictoken :: ( jletterordigit | '-' ) +
topic ::= topictoken ( '/' topictoken ) *</code></pre><p>Topics should be designed to become more specific when going from
left to right. Consumers can provide a prefix that matches a topic,
using the preferred order allows a handler to minimize the number of
prefixes it needs to register.
</p>
<p>Topics are case-sensitive. As a convention, topics should follow
the reverse domain name scheme used by Java packages to guarantee
uniqueness. The separator must be a solidus (<code class="code">'/' \u002F</code>)
instead of the full stop (<code class="code">'.' \u002E</code>).
</p>
<p>This specification uses the convention
<code class="code">fully/qualified/package/ClassName/ACTION</code>. If necessary, a
pseudo-class-name is used.
</p>
</div>
<div class="section">
<div xmlns="" class="titlepage">
<div>
<div>
<h3 xmlns="http://www.w3.org/1999/xhtml" class="title"><a xmlns="" class="anchor" id="d0e152053"></a><span xmlns="" class="number">157.3.2</span> Automatically Generated Topics
</h3>
</div>
</div>
</div>
<p>In many cases the name of a topic contains no information other
than defining the schema of the events sent on that topic. Therefore,
when publishing a Typed Event to the Typed Event Bus, the Typed Event
implementation is able to automatically generate a topic name based on
the the type of the event object being published.
</p>
<p>For the <a xmlns="" class="xref" href="service.typedevent.html#org.osgi.service.typedevent.TypedEventBus.deliver-Object-" title="157.9.3.1 public void deliver(Object event)">deliver(Object)</a> method on the Typed Event Bus where no topic
string is provided, the implementation must create a topic string using
the fully qualified class name of the event object. To convert the class
name into a valid topic the full stop <code class="code">.</code> separators must be
converted into solidus <code class="code">/</code> separators. A non-normative
example implementation follows:
</p><pre xmlns="" class="programlisting"><code>public void deliver(Object event) {
String topicName = event.getClass().getName().replace('.', '/');
this.deliver(topicName, event);
}</code></pre><p>The following example demonstrates how an Event Source can make
use of an automatically generated topic name.
</p><pre xmlns="" class="programlisting"><code>package org.osgi.example;
public class ExampleEvent {
public String message;
}
@Component
public class ExampleEventSource {
@Reference
TypedEventBus bus;
public void sendEvent() {
ExampleEvent event = new ExampleEvent();
event.message = "The time is " + LocalDateTime.now();
// This event will be delivered to the
// topic "org/osgi/example/ExampleEvent"
bus.deliver(event);
}
}</code></pre></div>
<div class="section">
<div xmlns="" class="titlepage">
<div>
<div>
<h3 xmlns="http://www.w3.org/1999/xhtml" class="title"><a xmlns="" class="anchor" id="d0e152074"></a><span xmlns="" class="number">157.3.3</span> Thread Safety
</h3>
</div>
</div>
</div>
<p>The <code class="code">TypedEventBus</code> implementation must be thread safe
and allow for simultaneous event publication from multiple threads. For
any given source thread, events must be delivered in the same order as
they were published by that thread. Events published by different
threads, however, may be delivered in a different order from the one in
which they were published.
</p>
<p>For example, if thread <span class="emphasis"><em>A</em></span> publishes events
<span class="emphasis"><em>1</em></span>, <span class="emphasis"><em>2</em></span> and
<span class="emphasis"><em>3</em></span>, while thread <span class="emphasis"><em>B</em></span> publishes
events <span class="emphasis"><em>4</em></span>, <span class="emphasis"><em>5</em></span> and
<span class="emphasis"><em>6</em></span>, then the events may be delivered:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
<p><span class="emphasis"><em>1</em></span>, <span class="emphasis"><em>2</em></span>,
<span class="emphasis"><em>3</em></span>, <span class="emphasis"><em>4</em></span>,
<span class="emphasis"><em>5</em></span>, <span class="emphasis"><em>6</em></span></p>
</li>
<li class="listitem">
<p><span class="emphasis"><em>4</em></span>, <span class="emphasis"><em>1</em></span>,
<span class="emphasis"><em>2</em></span>, <span class="emphasis"><em>5</em></span>,
<span class="emphasis"><em>6</em></span>, <span class="emphasis"><em>3</em></span></p>
</li>
<li class="listitem">
<p>and so on</p>
</li>
</ul>
</div>
<p>but events will never be delivered <span class="emphasis"><em>1</em></span>,
<span class="emphasis"><em>2</em></span>, <span class="emphasis"><em>6</em></span>, <span class="emphasis"><em>4</em></span>,
<span class="emphasis"><em>5</em></span>, <span class="emphasis"><em>3</em></span></p>
</div>
</div>
<div class="section">
<div xmlns="" class="titlepage">
<div>
<div>
<h2 xmlns="http://www.w3.org/1999/xhtml" class="title" style="clear: both"><a xmlns="" class="anchor" id="d0e152169"></a><span xmlns="" class="number">157.4</span> Receiving Events
</h2>
</div>
</div>
</div>
<p>Event Consumers can receive events by registering an appropriate
Event Handler service in the Service Registry. This is a <a xmlns="" class="xref" href="service.typedevent.html#org.osgi.service.typedevent.TypedEventHandler" title="157.9.5 public interface TypedEventHandler<T>">TypedEventHandler</a> to receive events as type-safe objects, or an
<a xmlns="" class="xref" href="service.typedevent.html#org.osgi.service.typedevent.UntypedEventHandler" title="157.9.7 public interface UntypedEventHandler">UntypedEventHandler</a> to receive events as untyped Map
structures.
</p>
<p>Published events are then delivered, using the whiteboard pattern,
to any Event Handler service which has registered interest in the topic to
which the event was published.
</p>
<div class="section">
<div xmlns="" class="titlepage">
<div>
<div>
<h3 xmlns="http://www.w3.org/1999/xhtml" class="title"><a xmlns="" class="anchor" id="d0e152180"></a><span xmlns="" class="number">157.4.1</span> Receiving Typed Events
</h3>
</div>
</div>
</div>
<p>Typed Events are received by registering a
<code class="code">TypedEventHandler</code> implementation. This service has a single
method <code class="code">notify</code> which receives the String topic name and
Object event data. The TypedEventHandler implementation must be
registered as a service in the service registry using the
TypedEventHandler interface.
</p>
<p>The TypedEventHandler interface is parameterized, and so it is
expected that the implementation reifies the type parameter into a
specific type. In this case the Typed Event implementation must adapt
the Event object into the type defined by the TypedEventHandler
implementation. Implementations of this specification are free to choose
their own adaptation mechanism, however it must guarantee at least the
same functionality as <a xmlns="" class="xref" href="util.converter.html" title="707 Converter Specification"><em xmlns="http://www.w3.org/1999/xhtml">Converter Specification</em></a>.
</p>
<p>A simple example of receiving a typed event follows:</p><pre xmlns="" class="programlisting"><code>public class ExampleEvent {
public String message;
}
@Component
public class ExampleTypedConsumer implements TypedEventHandler<ExampleEvent> {
@Override
public void notify(String topic, ExampleEvent event) {
System.out.println("Received event: " + event.message);
}
}</code></pre><p>If the TypedEventHandler implementation is unable to reify the
type, or the required type is more specific than the reified type, then
the Typed Event Handler must be registered with the
<code class="code">event.type</code> service property. This property has a string
value containing the fully-qualified type name of the type that the
Typed Event Handler expects to receive. This type must be loaded by the
Typed Event implementation using the classloader of the bundle which
registered the Typed Event Handler service. The loaded type must then be
used as the target type when converting events. For example:
</p><pre xmlns="" class="programlisting"><code>public class ExampleEvent {
public String message;
}
public class SpecialisedExampleEvent extends ExampleEvent {
public int sequenceId = Integer.MIN_VALUE;
}
@Component
@EventType(SpecialisedExampleEvent.class)
public class ExampleTypedConsumer implements TypedEventHandler<ExampleEvent> {
@Override
public void notify(String topic, ExampleEvent event) {
System.out.println("Received event: " + event.message);
// The event will always be of type SpecialisedExampleEvent
System.out.println("Event sequence id was " +
((SpecialisedExampleEvent) event).sequenceId);
}
}</code></pre><p>By default the reified type of the TypedEventHandler will be used
as the target topic for the Event Handler. If the
<code class="code">event.type</code> property is set then this is used as the default
topic instead of the reified type. To use a specific named topic the
Typed Event Handler service may be registered with an
<code class="code">event.topics</code> service property specifying the topic(s) as a
String+ value.
</p><pre xmlns="" class="programlisting"><code>public class ExampleEvent {
public String message;
}
@Component
@EventTopics({"foo", "foo/bar"})
public class ExampleTypedConsumer implements TypedEventHandler<ExampleEvent> {
@Override
public void notify(String topic, ExampleEvent event) {
System.out.println("Event received on topic: " + topic +
" with message: " + event.message);
}
}</code></pre></div>
<div class="section">
<div xmlns="" class="titlepage">
<div>
<div>
<h3 xmlns="http://www.w3.org/1999/xhtml" class="title"><a xmlns="" class="anchor" id="d0e152216"></a><span xmlns="" class="number">157.4.2</span> Receiving Untyped Events
</h3>
</div>
</div>
</div>
<p>Untyped Events are received by registering an
<code class="code">UntypedEventHandler</code> implementation. This service has a
single method notifyUntyped which receives the <code class="code">String</code> topic
name and <code class="code">Map</code> event data. The Untyped Event Handler
implementation must be registered as a service in the service registry
using the <code class="code">UntypedEventHandler</code> interface.
</p>
<p>When delivering an event to an Untyped Event Handler the Typed
Event Service must, if necessary, convert the event data to a nested map
structure.
</p>
<p>The <code class="code">event.topics</code> service property must be used when
registering an Untyped Event Hander service. If it is not, then no
events will be delivered to that Untyped Event Handler service.
</p><pre xmlns="" class="programlisting"><code>public class ExampleEvent {
public String message;
}
@Component
@EventTopics({"foo", "foo/bar"})
public class ExampleUntypedConsumer implements UntypedEventHandler {
@Override
public void notifyUntyped(String topic, Map<String,Object> event) {
System.out.println("Event received on topic: " + topic
+ " with message: " + event.get("message"));
}
}</code></pre></div>
<div class="section">
<div xmlns="" class="titlepage">
<div>
<div>
<h3 xmlns="http://www.w3.org/1999/xhtml" class="title"><a xmlns="" class="anchor" id="d0e152242"></a><span xmlns="" class="number">157.4.3</span> Wildcard Topics
</h3>
</div>
</div>
</div>
<p>The <code class="code">event.topics</code> property may contain one or more
wildcard topics. These are Strings which contain a topic name and append
“/*”. This value means that the Event Handler must be called for Events
sent to sub-topics of the named topic. For example the component:
</p><pre xmlns="" class="programlisting"><code>@Component
@EventTopics("foo/*")
public class ExampleUntypedConsumer implements UntypedEventHandler {
@Override
public void notifyUntyped(String topic, Map<String,Object> event) {
System.out.println("Event received on topic: " + topic
+ " with message: " + event.get("message"));
}
}</code></pre><p>would receive events sent to the topics <code class="code">foo/bar</code> and
<code class="code">foo/baz</code>, but not the topics <code class="code">foo</code> or
<code class="code">foobar/fizzbuzz</code>.
</p>
<p>The <code class="code">*</code> character in a wildcard topic must always
follow a solidus <code class="code">/</code> character, and must be the final
character in the topic string, meaning that topic names such as
<code class="code">foo*</code> and <code class="code">foo/*/bar</code> are not valid. The only
exception to this rule is that it is valid to use the topic name
<code class="code">*</code> to receive events on <span class="emphasis"><em>all</em></span> topics.
While it is valid to do so, using the topic <code class="code">*</code> is not
typically recommended. For a mechanism to monitor the events flowing
through the system see <a xmlns="" class="xref" href="service.typedevent.html#service.typedevent-monitor" title="157.6 Monitoring Events">Monitoring Events</a>.
</p>
</div>
<div class="section">
<div xmlns="" class="titlepage">
<div>
<div>
<h3 xmlns="http://www.w3.org/1999/xhtml" class="title"><a xmlns="" class="anchor" id="d0e152291"></a><span xmlns="" class="number">157.4.4</span> Unhandled Events
</h3>
</div>
</div>
</div>
<p>Unhandled Events are events sent by an Event Source but which have
no Event Handler service listening to their topic. Rather than these
events being discarded, the Typed Event implementation will search the
service registry for services implementing <a xmlns="" class="xref" href="service.typedevent.html#org.osgi.service.typedevent.UnhandledEventHandler" title="157.9.6 public interface UnhandledEventHandler">UnhandledEventHandler</a>.
</p>
<p>If any services are found then the Typed Event implementation will
call the <code class="code">notifyUnhandled</code> method passing the topic name and
event data to all of the registered Unhandled Event Handler
services.
</p><pre xmlns="" class="programlisting"><code>public class ExampleEvent {
public String message;
}
@Component
public class ExampleUnhandledConsumer implements UnhandledEventHandler {
@Override
public void notifyUnhandled(String topic, Map<String,Object> event) {
System.out.println("Unhandled Event received on topic: " + topic);
}
}</code></pre></div>
<div class="section">
<div xmlns="" class="titlepage">
<div>
<div>
<h3 xmlns="http://www.w3.org/1999/xhtml" class="title"><a xmlns="" class="anchor" id="d0e152305"></a><span xmlns="" class="number">157.4.5</span> Filtering Events
</h3>
</div>
</div>
</div>
<p>Sometimes the use of a topic is insufficient to restrict the
events received by an event consumer. In these cases the consumer can
further restrict the events that they receive by using a filter. The
filter is supplied using the <code class="code">event.filter</code> service property,
the value of which is an LDAP filter string. This filter is applied to
the event data, and only events which match the filter are delivered to
the event handler service.
</p>
<div class="section">
<div xmlns="" class="titlepage">
<div>
<div>
<h4 xmlns="http://www.w3.org/1999/xhtml" class="title"><a xmlns="" class="anchor" id="d0e152313"></a><span xmlns="" class="number">157.4.5.1</span> Nested Event Data
</h4>
</div>
</div>
</div>
<p>Complex events may contain nested data structures, such as DTOs,
as values in the event data. As LDAP filtering is only designed to
match against simple data this means that some event properties cannot
be filtered using the <code class="code">event.filter</code> property. The event
filter is therefore only suitable for use in matching top-level event
properties.
</p>
</div>
<div class="section">
<div xmlns="" class="titlepage">
<div>
<div>
<h4 xmlns="http://www.w3.org/1999/xhtml" class="title"><a xmlns="" class="anchor" id="d0e152321"></a><span xmlns="" class="number">157.4.5.2</span> Ignored Events
</h4>
</div>
</div>
</div>
<p>Note that the use of a filter is different from receiving an
event and choosing to ignore it based on its data. If an event fails
to match the filter supplied by an event handler service then it is
<span class="emphasis"><em>not delivered</em></span> to that event handler. This means
that the event data remains eligible to be sent to an
<code class="code">UnhandledEventHandler</code> unless another event handler does
receive it. An event that is received, but ignored, by an event
handler service <span class="emphasis"><em>does</em></span> count as having been
delivered, and so will never be sent to an
<code class="code">UnhandledEventHandler</code>.
</p>
</div>
</div>
<div class="section">
<div xmlns="" class="titlepage">
<div>
<div>
<h3 xmlns="http://www.w3.org/1999/xhtml" class="title"><a xmlns="" class="anchor" id="d0e152338"></a><span xmlns="" class="number">157.4.6</span> Failing Event Handlers
</h3>
</div>
</div>
</div>
<p>Event Handler implementations are called by the Typed Event Bus
implementation, and are expected:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
<p>Not to throw exceptions from their callback method</p>
</li>
<li class="listitem">
<p>To return quickly - any long running tasks should be moved to
another thread
</p>
</li>
</ul>
</div>
<p>If a Typed Event Bus implementation detects an Event Handler that
is behaving incorrectly, either by throwing exceptions, or by taking a
long time to process the event, or some other problem, then the
implementation may block further event delivery to that Event
Handler.
</p>
<p>If an Event Handler is blocked by the event implementation then
this situation must be logged. Also, if a blocked Event Handler service
is updated then the block must be removed by the implementation. If the
updated service continues to behave incorrectly then the block may be
reinstated.
</p>
</div>
<div class="section">
<div xmlns="" class="titlepage">
<div>
<div>
<h3 xmlns="http://www.w3.org/1999/xhtml" class="title"><a xmlns="" class="anchor" id="d0e152354"></a><span xmlns="" class="number">157.4.7</span> Event Handler Service Properties
</h3>
</div>
</div>
</div>
<p>The service properties that can be used to configure an Event
Handler service are outlined in the following table.
</p>
<div class="table"><a xmlns="" class="anchor" id="d0e152359"></a><p class="title"><strong>Table <span xmlns="" class="number">157</span>.1 Service properties applicable to Event Handler services</strong></p>
<div class="table-contents">
<table summary="Service properties applicable to Event Handler services" width="100%" style="border-collapse: collapse;border-top: 0.5pt solid ; border-bottom: 0.5pt solid ; border-left: 0.5pt solid ; border-right: 0.5pt solid ; ">
<colgroup>
<col />
<col />
<col />
</colgroup>
<thead>
<tr>
<th style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; ">Service Property Name</th>
<th style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; ">Type</th>
<th style="border-bottom: 0.5pt solid ; ">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; "><code class="code">event.topics</code></td>
<td style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; "><code class="code">String+</code></td>
<td style="border-bottom: 0.5pt solid ; ">
<p>Declares the topic pattern(s) for which the service
should be called. This service property is
<span class="emphasis"><em>required</em></span> for
<code class="code">UntypedEventHandler</code> services, but
<code class="code">TypedEventHandler</code> services may omit it if they are
only interested in the default topic name for their reified
type.
</p>
<p>See <a xmlns="" class="xref" href="service.typedevent.html#org.osgi.service.typedevent.TypedEventConstants.TYPED_EVENT_TOPICS" title="157.9.4.4 public static final String TYPED_EVENT_TOPICS = "event.topics"">TYPED_EVENT_TOPICS</a>.
</p>
</td>
</tr>
<tr>
<td style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; "><code class="code">event.type</code></td>
<td style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; "><code class="code">String</code></td>
<td style="border-bottom: 0.5pt solid ; ">
<p>Defines the target type into which events should be
converted before being passed to the Event Handler service. This
service property is <span class="emphasis"><em>forbidden</em></span> for
<code class="code">UntypedEventHandler</code> services, but
<code class="code">TypedEventHandler</code> services may use it if they wish
to further refine the type of data they wish to
receive.
</p>
<p>See <a xmlns="" class="xref" href="service.typedevent.html#org.osgi.service.typedevent.TypedEventConstants.TYPED_EVENT_TYPE" title="157.9.4.5 public static final String TYPED_EVENT_TYPE = "event.type"">TYPED_EVENT_TYPE</a>.
</p>
</td>
</tr>
<tr>
<td style="border-right: 0.5pt solid ; "><code class="code">event.filter</code></td>
<td style="border-right: 0.5pt solid ; "><code class="code">String</code></td>
<td style="">
<p>Defines an LDAP filter which should be tested
against the properties in the event data. Only events which pass
the filter will be passed to the the Event Handler service. Ths
service property is permitted for both
<code class="code">TypedEventHandler</code> and
<code class="code">UntypedEventHandler</code> services.
</p>
<p>See <a xmlns="" class="xref" href="service.typedevent.html#org.osgi.service.typedevent.TypedEventConstants.TYPED_EVENT_FILTER" title="157.9.4.1 public static final String TYPED_EVENT_FILTER = "event.filter"">TYPED_EVENT_FILTER</a>.
</p>
</td>
</tr>
</tbody>
</table>
</div>
</div><br class="table-break" /></div>
<div class="section">
<div xmlns="" class="titlepage">
<div>
<div>
<h3 xmlns="http://www.w3.org/1999/xhtml" class="title"><a xmlns="" class="anchor" id="d0e152441"></a><span xmlns="" class="number">157.4.8</span> Error Handling
</h3>
</div>
</div>
</div>
<p>There are several possible error scenarios for Event
Handlers:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
<p><code class="code">TypedEventHandler</code> - If the target event type is
not discoverable, that is there is no reified type information, nor
is there an <code class="code">event.type</code> property, then the target type
for the event is not known. In this situation there is no way for
the Typed Event implementation to correctly target an event schema,
and the <code class="code">TypedEventHandler</code> must be ignored. The
implementation must write a message to the log indicating which
service is being ignored.
</p>
</li>
<li class="listitem">
<p><code class="code">TypedEventHandler</code> - If the target event type is
discoverable but cannot be loaded using the class loader of the
bundle which registered the Typed Event Handler service then there
is no way for the Typed Event implementation to correctly target an
event schema, and the Event Handler must be ignored. The
implementation must write a message to the log indicating which
service is being ignored.
</p>
</li>
<li class="listitem">
<p>All Handler Types - If the event data cannot be adapted to the
target type, that is the incoming data cannot be transformed due to
badly mismatched property names or values, then that specific Event
cannot be submitted to the Handler. The Typed Event implementation
must write a message to the log indicating which service failed to
receive the event. If this error occurs repeatedly then the Typed
Event implementation may choose to deny list and ignore the Event
Handler service. Deny listing decisions must be written to the
log.
</p>
</li>
<li class="listitem">
<p>All Handler Types - If the <code class="code">event.topics</code> property
contains one or more invalid values then the Event Handler service
must be ignored. The implementation must write a message to the log
indicating which service is being ignored.
</p>
</li>
</ul>
</div>
</div>
</div>
<div class="section">
<div xmlns="" class="titlepage">
<div>