Elffuss BraidHub

kikocis/java-driver public

JAVA-1201: Document OSGi. (#695)

Changeset4c920eab788b119fd898b71a00e078ee6585d22a346430fcf5b2e674195acc79
AuthorAlexandre Dutra <adutra@users.noreply.github.com> · 2016-07-20 23:27 +02:00
CommitterOlivier Michallat <olim7t@users.noreply.github.com> · 2016-07-20 14:27 -07:00
Parents6bedcdb0c472

Browse the files at this changeset

7 files changed

added driver-tests/osgi/.gitignore +2 −0
@@ -0,0 +1,2 @@
1+# Created by Pax plugin
2+runner
modified driver-tests/osgi/README.md +28 −12
@@ -1,34 +1,50 @@
1-# OSGi Example
1+# OSGi Tests
22
3-A simple test for the Java Driver in an OSGi environment.
3+A collection of simple tests for the Java Driver in an OSGi environment.
4+
5+This project is mainly intended as a tool for validating
6+that each new release of the driver is fully OSGi-compatible.
7+It is _not_ meant as an example application.
8+
9+If you are looking for examples demonstrating usage of the driver in an OSGi
10+environment, please refer to our [OSGi examples repository].
11+
12+[OSGi examples repository]:https://github.com/datastax/java-driver-examples-osgi
13+
14+## Usage
415
516 `MailboxService` is an OSGi service that uses Cassandra to
617 store messages that can be retrieved by email address.
718
8-## Usage
19+Before running the tests, there are some prerequisites that must be met:
20+
21+The tests in this module will try to locate and load the following artifacts:
922
10-To build the bundle and run tests, execute the following Maven goal:
23+1. The jars of 3 dependent modules:
24+ `driver-core`, `driver-mapping` and `driver-extras`.
25+ These are expected to be found in their respective `target` directory;
26+2. The test-jar for `driver-core`. Tests will attempt to load it from your local Maven repository.
27+
28+Therefore, before you can launch the tests, it is required to run `mvn install`
29+first _on the entire project_ to make sure all the above artifacts will be present.
30+
31+If the above prerequisites are met, then it is possible to build
32+this project and run its tests by executing the following Maven goal:
1133
1234 mvn verify -P short
1335
1436 The "short" profile needs to be activated since the tests run under
1537 this group.
1638
17-Note: tests will try to load the jars of 3 dependent modules:
18-`driver-core`, `driver-mapping` and `driver-extras`.
19-For this to succeed, you need to run `mvn package`
20-first for these modules and make sure the jars are present
21-in each module's `target/` subdirectory.
22-
2339 Once `mvn verify` completes, the bundle jar will be present in the `target/` directory.
2440
25-The project includes integration tests that verify the service can
41+The project includes integration tests that verify that the service can
2642 be activated and used in an OSGi container. It also verifies that
2743 the Java driver can be used in an OSGi container in the following
2844 configurations:
2945
3046 1. Default (default classifier with all dependencies)
31-2. Netty-Shaded (shaded classifier with all depedencies w/o Netty)
47+2. Netty-Shaded (shaded classifier with all dependencies w/o Netty)
3248 5. Guava 17
3349 6. Guava 18
3450 7. Guava 19
modified driver-tests/osgi/pom.xml +22 −0
@@ -248,6 +248,28 @@
248248 </executions>
249249 </plugin>
250250
251+ <!--
252+ To launch Pax Runner and check that all driver bundles are correctly provisioned,
253+ do the following:
254+ mvn pax:run
255+ Note: you MUST run 'mvn install' on the entire project before!
256+ -->
257+ <plugin>
258+ <groupId>org.ops4j</groupId>
259+ <artifactId>maven-pax-plugin</artifactId>
260+ <version>1.6.0</version>
261+ <configuration>
262+ <framework>felix</framework>
263+ <showWarnings>true</showWarnings>
264+ <provision>
265+ <param>--platform=felix</param>
266+ <param>--version=${felix.version}</param>
267+ <param>--log=debug</param>
268+ <param>--systemPackages=sun.misc</param>
269+ </provision>
270+ </configuration>
271+ </plugin>
272+
251273 </plugins>
252274
253275 </build>
modified faq/README.md +7 −2
@@ -82,7 +82,7 @@
8282 BatchStatement batch = new BatchStatement();
8383 batch.add(ps.bind(...));
8484 batch.add(ps.bind(...));
85-// here's a simple statement
85+// here's a simple statement
8686 batch.add(new SimpleStatement( "INSERT INTO contacts (email, firstname, lastname) VALUES (?, ?, ?)", ...));
8787 session.execute(batch);
8888 ```
@@ -157,4 +157,9 @@
157157
158158 See [Blobs.java] in the `driver-examples` module for some examples and explanations.
159159
160-[Blobs.java]: https://github.com/datastax/java-driver/tree/3.0/driver-examples/src/main/java/com/datastax/driver/examples/datatypes/Blobs.java
No newline at the end of the file
160+[Blobs.java]: https://github.com/datastax/java-driver/tree/3.0/driver-examples/src/main/java/com/datastax/driver/examples/datatypes/Blobs.java
161+
162+
163+### How do I use the driver in an OSGi application?
164+
165+Read our [OSGi-specific FAQ section](osgi/) to find out.
added faq/osgi/README.md +201 −0
@@ -0,0 +1,201 @@
1+## Frequently Asked Questions - OSGi
2+
3+### How to use the Java driver in an OSGi environment?
4+
5+We have complete examples demonstrating usage of the driver in an [OSGi]
6+environment; please refer to our [OSGi examples repository].
7+
8+
9+### How to override Guava's version?
10+
11+The driver is compatible and tested with all versions of Guava in the range
12+`[16.0.1,20)`.
13+
14+If using Maven, you can force a more specific version by re-declaring
15+the 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+
23+Make sure that your project's manifest is importing the right version
24+of 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+
31+First, read our [manual page on compression](../../manual/compression/)
32+to understand how to enable compression for the Java driver.
33+
34+OSGi projects can use both Snappy or LZ4 compression algorithms.
35+
36+For 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+
44+For 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
53+because they are _not_ valid OSGi bundles.
54+
55+Because compression libraries are _optional runtime dependencies_,
56+most manifest generation tools (such as [BND] and the [Maven bundle plugin])
57+will not reference these libraries in your project's manifest.
58+This is correct, but could be a problem for some OSGi provisioning tools,
59+and notably for [Tycho], because it does not consider such
60+dependencies when computing the target platform.
61+
62+If you are facing provisioning issues related to compression libraries,
63+you might need to either add them explicitly to your OSGi runtime,
64+or explicitly reference them in your project's manifest.
65+With the [Maven bundle plugin], this second option can be achieved with the following
66+[BND] `Import-Package` instruction (the example below is for
67+LZ4, but the same applies to Snappy as well):
68+
69+ <Import-Package>net.jpountz.lz4,*</Import-Package>
70+
71+With Tycho, another option is to explicitly declare an "extra requirement"
72+to 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+
92+Note 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+
98+The driver [shaded jar](../../manual/shaded_jar/) can be used
99+in any OSGi application, although the same limitations explained in
100+the manual apply.
101+
102+
103+### How to get proper logs?
104+
105+The driver uses [SLF4j] for [logging](../../manual/logging/).
106+
107+You OSGi runtime should therefore include the SLF4J API bundle, and
108+one valid implementation bundle, such as [Logback].
109+
110+For Maven-based projects, this can be achieved with the following
111+dependencies:
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+
126+Some OSGi containers might require additional configuration.
127+Please consult their documentation for further details.
128+
129+
130+### I'm getting the error: "Unable to access `sun.misc.Unsafe`"
131+
132+Some of the driver dependencies try to access non-API packages
133+such as `sun.misc`, which is usually not available in an OSGi container.
134+
135+Most of these libraries are smart enough to check if this package
136+is available or not before actually trying to use it.
137+
138+However, the [metrics library](../../manual/metrics/) used by the driver
139+attempts to load `sun.misc.Unsafe` without
140+probing for it first, which may lead to bundle activation errors.
141+
142+[JAVA-1203] has been created to track this issue, which
143+should be fixed in the next major release of the library.
144+
145+There are a few workarounds:
146+
147+- Add `sun.misc` to the list of system packages (i.e., packages
148+exported by the system bundle), or to the list of boot delegation
149+packages (i.e., defer loading of `sun.misc` to the boot classloader).
150+This is the best option as it benefits to other libraries as well.
151+With 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+
169+The driver is able to perform native system calls through JNR in some cases,
170+for example to achieve microsecond resolution when
171+[generating timestamps](../../manual/query_timestamps/).
172+
173+Unfortunately, some of the JNR artifacts available from Maven
174+are not valid OSGi bundles and cannot be used in OSGi applications.
175+
176+[JAVA-1127] has been created to track this issue, and there
177+is currently no simple workaround.
178+
179+Note that if you use Maven and include any JNR dependency
180+in your pom, _these will be silently ignored by Pax Exam when
181+running integration tests_, and most likely, your tests will
182+fail with provisioning errors.
183+
184+Because native calls are not available,
185+it 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/
added manual/osgi/README.md +20 −0
@@ -0,0 +1,20 @@
1+# OSGi
2+
3+The driver is available as an [OSGi] bundle. More specifically,
4+the following Maven artifacts are actually valid OSGi bundles:
5+
6+- `driver-core`
7+- `driver-mapping`
8+- `driver-extras`
9+
10+## Including the driver in your OSGi application
11+
12+We have complete examples demonstrating usage of the driver in an OSGi
13+environment; please refer to our [OSGi examples repository].
14+
15+[OSGi examples repository]:https://github.com/datastax/java-driver-examples-osgi
16+
17+## Troubleshooting OSGi applications
18+
19+Please read our [OSGi-specific FAQ section](../../faq/osgi/) to find tips
20+for troubleshooting OSGi applications.
modified manual/shaded_jar/README.md +23 −0
@@ -53,3 +53,26 @@
5353 </exclusions>
5454 </dependency>
5555 ```
56+
57+### Limitations
58+
59+When using the shaded jar, it is not possible to configure the Netty layer.
60+
61+Extending [NettyOptions] is not an option because `NettyOptions` API
62+exposes Netty classes, and as such it should only be extended
63+by clients using the non-shaded version of driver.
64+Attempting to extend `NettyOptions` with shaded Netty classes is not supported,
65+and in particular for OSGi applications,
66+it is likely that such a configuration would lead to compile and/or runtime errors.
67+
68+Also, with shaded Netty classes, it is not possible to benefit
69+from [Netty native transports].
70+
71+It is therefore normal to see the following log line when the driver starts and
72+detects that shaded Netty classes are being used:
73+
74+ Detected shaded Netty classes in the classpath; native epoll transport will not work properly, defaulting to NIO.
75+
76+
77+[NettyOptions]:http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/NettyOptions.html
78+[Netty native transports]:http://netty.io/wiki/native-transports.html