-
Notifications
You must be signed in to change notification settings - Fork 36
/
injection.xml
104 lines (88 loc) · 4.01 KB
/
injection.xml
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
<?xml version="1.0"?>
<!--
Copyright (c) 2013, 2017 Oracle and/or its affiliates. All rights reserved.
This program and the accompanying materials are made available under the
terms of the Eclipse Public License v. 2.0, which is available at
http://www.eclipse.org/legal/epl-2.0.
This Source Code may also be made available under the following Secondary
Licenses when the conditions for such availability set forth in the
Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
version 2 with the GNU Classpath Exception, which is available at
https://www.gnu.org/software/classpath/license.html.
SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
-->
<!DOCTYPE chapter [<!ENTITY % ents SYSTEM "tyrus.ent" > %ents;]>
<chapter xmlns="http://docbook.org/ns/docbook"
version="5.0"
xml:lang="en"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xi="http://www.w3.org/2001/XInclude"
xmlns:xlink="http://www.w3.org/1999/xlink"
xsi:schemaLocation="http://docbook.org/ns/docbook http://docbook.org/xml/5.0/xsd/docbook.xsd
http://www.w3.org/1999/xlink http://www.w3.org/1999/xlink.xsd"
xml:id="injection">
<title>Injection Support</title>
<para>
As required in &ja4w;, Tyrus supports full field, method and constructor injection using <code>jakarta.inject.Inject</code>
into all websocket endpoint classes as well as the use of the interceptors on these classes.
Except this, Tyrus also supports some of the EJB annotations. Currently <code>jakarta.ejb.Stateful</code>,
<code>jakarta.ejb.Singleton</code> and <code>jakarta.ejb.Stateless</code> annotations are supported.
</para>
<section>
<title><code>jakarta.inject.Inject</code> sample</title>
<para>
The following example presents how to inject a bean to the <code>jakarta.websocket.server.ServerEndpoint</code>
annotated class using <code>jakarta.inject.Inject</code>. Class <code>InjectedSimpleBean</code> gets injected
into class <code>SimpleEndpoint</code> on line 15.
<example xml:id="cdi-simple-bean">
<title>Injecting bean into <code>jakarta.websocket.server.ServerEndpoint</code></title>
<programlisting language="java" linenumbering="numbered">public class InjectedSimpleBean {
private static final String TEXT = " (from your server)";
public String getText() {
return TEXT;
}
}
@ServerEndpoint(value = "/simple")
public class SimpleEndpoint {
private boolean postConstructCalled = false;
@Inject
InjectedSimpleBean bean;
@OnMessage
public String echo(String message) {
return String.format("%s%s", message, bean.getText());
}
}</programlisting>
</example>
</para>
</section>
<section>
<title>EJB sample</title>
<para>
The following sample presents how to turn <code>jakarta.websocket.server.ServerEndpoint</code> annotated class
into <code>jakarta.ejb.Singleton</code> and use interceptor on it.
<example xml:id="cdi-singleton-bean">
<title>Echo sample server endpoint.</title>
<programlisting language="java" linenumbering="numbered">@ServerEndpoint(value = "/singleton")
@Singleton
@Interceptors(LoggingInterceptor.class)
public class SingletonEndpoint {
int counter = 0;
public static boolean interceptorCalled = false;
@OnMessage
public String echo(String message) {
return interceptorCalled ? String.format("%s%s", message, counter++) : "LoggingInterceptor not called.";
}
}
public class LoggingInterceptor {
@AroundInvoke
public Object manageTransaction(InvocationContext ctx) throws Exception {
SingletonEndpoint.interceptorCalled = true;
Logger.getLogger(getClass().getName()).info("LOGGING.");
return ctx.proceed();
}
}
</programlisting>
</example>
</para>
</section>
</chapter>