| 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/ |