Elffuss BraidHub

kikocis/java-driver public

3.1.x · 6.9 KiBRenderedRawPermalink
1## Frequently Asked Questions - OSGi
2
3### How to use the Java driver in an OSGi environment?
4
5We have complete examples demonstrating usage of the driver in an [OSGi]
6environment; please refer to our [OSGi examples repository].
7
8
9### How to override Guava's version?
10
11The driver is compatible and tested with all versions of Guava in the range
12`[16.0.1,20)`.
13
14If using Maven, you can force a more specific version by re-declaring
15the Guava dependency in your project, e.g.:
16
17 <dependency>
18 <groupId>com.google.guava</groupId>
19 <artifactId>guava</artifactId>
20 <version>19.0</version>
21 </dependency>
22
23Make sure that your project's manifest is importing the right version
24of Guava's packages, e.g. for 19.0:
25
26 Import-Package: com.google.common.base;version="[19.0,20)"
27
28
29### How to enable compression?
30
31First, read our [manual page on compression](../../manual/compression/)
32to understand how to enable compression for the Java driver.
33
34OSGi projects can use both Snappy or LZ4 compression algorithms.
35
36For Snappy, include the following Maven dependency:
37
38 <dependency>
39 <groupId>org.xerial.snappy</groupId>
40 <artifactId>snappy-java</artifactId>
41 <version>1.1.2.6</version>
42 </dependency>
43
44For LZ4, include the following Maven dependency:
45
46 <dependency>
47 <groupId>net.jpountz.lz4</groupId>
48 <artifactId>lz4</artifactId>
49 <version>1.3.0</version>
50 </dependency>
51
52**IMPORTANT**: versions of LZ4 library below 1.3.0 cannot be used
53because they are _not_ valid OSGi bundles.
54
55Because compression libraries are _optional runtime dependencies_,
56most manifest generation tools (such as [BND] and the [Maven bundle plugin])
57will not reference these libraries in your project's manifest.
58This is correct, but could be a problem for some OSGi provisioning tools,
59and notably for [Tycho], because it does not consider such
60dependencies when computing the target platform.
61
62If you are facing provisioning issues related to compression libraries,
63you might need to either add them explicitly to your OSGi runtime,
64or explicitly reference them in your project's manifest.
65With the [Maven bundle plugin], this second option can be achieved with the following
66[BND] `Import-Package` instruction (the example below is for
67LZ4, but the same applies to Snappy as well):
68
69 <Import-Package>net.jpountz.lz4,*</Import-Package>
70
71With Tycho, another option is to explicitly declare an "extra requirement"
72to the compression library in the target platform definition:
73
74 <plugin>
75 <groupId>org.eclipse.tycho</groupId>
76 <artifactId>target-platform-configuration</artifactId>
77 <version>0.25.0</version>
78 <configuration>
79 <dependency-resolution>
80 <extraRequirements>
81 <requirement>
82 <id>lz4-java</id>
83 <versionRange>1.3.0</versionRange>
84 <type>eclipse-plugin</type>
85 </requirement>
86 </extraRequirements>
87 </dependency-resolution>
88 ...
89 </configuration>
90 </plugin>
91
92Note that the requirement id is its bundle symbolic name,
93_not_ its Maven artifact id.
94
95
96### How to use the driver shaded jar?
97
98The driver [shaded jar](../../manual/shaded_jar/) can be used
99in any OSGi application, although the same limitations explained in
100the manual apply.
101
102
103### How to get proper logs?
104
105The driver uses [SLF4j] for [logging](../../manual/logging/).
106
107You OSGi runtime should therefore include the SLF4J API bundle, and
108one valid implementation bundle, such as [Logback].
109
110For Maven-based projects, this can be achieved with the following
111dependencies:
112
113 <dependency>
114 <groupId>org.slf4j</groupId>
115 <artifactId>slf4j-api</artifactId>
116 <version>1.7.12</version>
117 </dependency>
118
119 <dependency>
120 <groupId>ch.qos.logback</groupId>
121 <artifactId>logback-classic</artifactId>
122 <version>1.1.2</version>
123 <scope>runtime</scope>
124 </dependency>
125
126Some OSGi containers might require additional configuration.
127Please consult their documentation for further details.
128
129
130### I'm getting the error: "Unable to access `sun.misc.Unsafe`"
131
132Some of the driver dependencies try to access non-API packages
133such as `sun.misc`, which is usually not available in an OSGi container.
134
135Most of these libraries are smart enough to check if this package
136is available or not before actually trying to use it.
137
138However, the [metrics library](../../manual/metrics/) used by the driver
139attempts to load `sun.misc.Unsafe` without
140probing for it first, which may lead to bundle activation errors.
141
142[JAVA-1203] has been created to track this issue, which
143should be fixed in the next major release of the library.
144
145There are a few workarounds:
146
147- Add `sun.misc` to the list of system packages (i.e., packages
148exported by the system bundle), or to the list of boot delegation
149packages (i.e., defer loading of `sun.misc` to the boot classloader).
150This is the best option as it benefits to other libraries as well.
151With Felix, this can be achieved with one of the following start options:
152
153 ```
154 --bootDelegation=sun.misc
155 --systemPackages=sun.misc
156 ```
157
158- Disable metrics altogether:
159
160 ```java
161 Cluster cluster = Cluster.builder()
162 .addContactPoints(...)
163 .withoutMetrics()
164 .build();
165 ```
166
167### I'm getting the error: "Could not load JNR C Library"
168
169The driver is able to perform native system calls through JNR in some cases,
170for example to achieve microsecond resolution when
171[generating timestamps](../../manual/query_timestamps/).
172
173Unfortunately, some of the JNR artifacts available from Maven
174are not valid OSGi bundles and cannot be used in OSGi applications.
175
176[JAVA-1127] has been created to track this issue, and there
177is currently no simple workaround.
178
179Note that if you use Maven and include any JNR dependency
180in your pom, _these will be silently ignored by Pax Exam when
181running integration tests_, and most likely, your tests will
182fail with provisioning errors.
183
184Because native calls are not available,
185it is also normal to see the following log lines when starting the driver:
186
187 INFO - Could not load JNR C Library, native system calls through this library will not be available
188 INFO - Using java.lang.System clock to generate timestamps.
189
190
191[OSGi]:https://www.osgi.org
192[Felix]:https://cwiki.apache.org/confluence/display/FELIX/Index
193[JAVA-1203]:https://datastax-oss.atlassian.net/browse/JAVA-1203
194[JAVA-1127]:https://datastax-oss.atlassian.net/browse/JAVA-1127
195[BND]:http://www.aqute.biz/Bnd
196[Maven bundle plugin]:https://cwiki.apache.org/confluence/display/FELIX/Apache+Felix+Maven+Bundle+Plugin+%28BND%29
197[OSGi examples repository]:https://github.com/datastax/java-driver-examples-osgi
198[without metrics]:http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/Cluster.Builder.html#withoutMetrics--
199[SLF4J]:http://www.slf4j.org/
200[Logback]:http://logback.qos.ch/
201[Tycho]:https://eclipse.org/tycho/