Re-add Java 8 support, update tdlib and tdlight

This commit is contained in:
Andrea Cavalli 2021-05-05 13:10:28 +02:00
parent a855b4fdba
commit 9dba2c7071
7 changed files with 165 additions and 19 deletions

View File

@ -9,7 +9,7 @@ A barebone java wrapper for TDLib (and TDLight)
This wrapper gives you direct access to TDLib API in Java. This wrapper gives you direct access to TDLib API in Java.
## Requirements ## Requirements
Java versions: Java 11, 12, 13, 14, 15, 16 Java versions: Java 8, 9, 10, 11, 12, 13, 14, 15, 16
Operating system: Linux, Windows, MacOS Operating system: Linux, Windows, MacOS

View File

@ -74,7 +74,7 @@ public class InternalClientManager implements AutoCloseable {
TdApi.Object event = clientEvents[i]; TdApi.Object event = clientEvents[i];
boolean mustPrintError = true; boolean mustPrintError = true;
if (event instanceof TdApi.Error) { if (event instanceof TdApi.Error) {
var errorEvent = (TdApi.Error) event; TdApi.Error errorEvent = (TdApi.Error) event;
if (Objects.equals("Request aborted", errorEvent.message)) { if (Objects.equals("Request aborted", errorEvent.message)) {
mustPrintError = false; mustPrintError = false;
} }

View File

@ -41,7 +41,7 @@ public class InternalReactiveClient implements ClientEventsHandler, ReactiveTele
this.clientManager = clientManager; this.clientManager = clientManager;
this.updateHandler = new Handler( this.updateHandler = new Handler(
updateItem -> { updateItem -> {
var item = ReactiveItem.ofUpdate(updateItem); ReactiveItem item = ReactiveItem.ofUpdate(updateItem);
if (subscriber != null && requested.getAndUpdate(n -> n == 0 ? 0 : (n - 1)) > 0) { if (subscriber != null && requested.getAndUpdate(n -> n == 0 ? 0 : (n - 1)) > 0) {
subscriber.onNext(item); subscriber.onNext(item);
} else { } else {
@ -49,7 +49,7 @@ public class InternalReactiveClient implements ClientEventsHandler, ReactiveTele
} }
}, },
updateEx -> { updateEx -> {
var item = ReactiveItem.ofUpdateException(updateEx); ReactiveItem item = ReactiveItem.ofUpdateException(updateEx);
if (subscriber != null && requested.getAndUpdate(n -> n == 0 ? 0 : (n - 1)) > 0) { if (subscriber != null && requested.getAndUpdate(n -> n == 0 ? 0 : (n - 1)) > 0) {
subscriber.onNext(item); subscriber.onNext(item);
} else { } else {
@ -128,7 +128,7 @@ public class InternalReactiveClient implements ClientEventsHandler, ReactiveTele
public void subscribe(Subscriber<? super ReactiveItem> subscriber) { public void subscribe(Subscriber<? super ReactiveItem> subscriber) {
AtomicBoolean alreadyCompleted = new AtomicBoolean(); AtomicBoolean alreadyCompleted = new AtomicBoolean();
if (updatesAlreadySubscribed.compareAndSet(false, true)) { if (updatesAlreadySubscribed.compareAndSet(false, true)) {
var subscription = new Subscription() { Subscription subscription = new Subscription() {
@Override @Override
public void request(long n) { public void request(long n) {
@ -228,7 +228,7 @@ public class InternalReactiveClient implements ClientEventsHandler, ReactiveTele
@Override @Override
public Publisher<TdApi.Object> send(Function query) { public Publisher<TdApi.Object> send(Function query) {
return subscriber -> { return subscriber -> {
var subscription = new Subscription() { Subscription subscription = new Subscription() {
private final AtomicBoolean alreadyRequested = new AtomicBoolean(false); private final AtomicBoolean alreadyRequested = new AtomicBoolean(false);
private volatile boolean cancelled = false; private volatile boolean cancelled = false;

View File

@ -2,6 +2,7 @@ package it.tdlight.common.internal;
import it.tdlight.common.EventsHandler; import it.tdlight.common.EventsHandler;
import it.tdlight.common.utils.IntSwapper; import it.tdlight.common.utils.IntSwapper;
import it.tdlight.common.utils.SpinWaitSupport;
import it.tdlight.jni.TdApi; import it.tdlight.jni.TdApi;
import it.tdlight.jni.TdApi.Object; import it.tdlight.jni.TdApi.Object;
import java.util.ArrayList; import java.util.ArrayList;
@ -51,17 +52,11 @@ public class ResponseReceiver extends Thread implements AutoCloseable {
public void run() { public void run() {
int[] sortIndex; int[] sortIndex;
try { try {
boolean useSpinWait = "amd64".equalsIgnoreCase(System.getProperty("os.arch"));
while (!closeRequested || !registeredClients.isEmpty()) { while (!closeRequested || !registeredClients.isEmpty()) {
int resultsCount = NativeClientAccess.receive(clientIds, eventIds, events, 2.0 /*seconds*/); int resultsCount = NativeClientAccess.receive(clientIds, eventIds, events, 2.0 /*seconds*/);
if (resultsCount <= 0) { if (resultsCount <= 0) {
if (useSpinWait) { SpinWaitSupport.onSpinWait();
Thread.onSpinWait();
} else {
//noinspection BusyWait
Thread.sleep(20);
}
continue; continue;
} }
@ -157,8 +152,6 @@ public class ResponseReceiver extends Thread implements AutoCloseable {
this.registeredClients.addAll(closedClients); this.registeredClients.addAll(closedClients);
} }
} }
} catch (InterruptedException ex) {
throw new IllegalStateException(ex);
} finally { } finally {
this.closeWait.countDown(); this.closeWait.countDown();
} }

View File

@ -0,0 +1,35 @@
package it.tdlight.common.utils;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
public class SpinWaitSupport {
private static final MethodHandle onSpinWaitHandle;
static {
MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodHandle handle;
try {
handle = lookup.findStatic(java.lang.Thread.class, "onSpinWait", MethodType.methodType(void.class));
} catch (Exception e) {
handle = null;
}
onSpinWaitHandle = handle;
}
private SpinWaitSupport() {
}
public static void onSpinWait() {
if (onSpinWaitHandle != null) {
try {
onSpinWaitHandle.invokeExact();
} catch (Throwable throwable) {
// This can't happen
}
}
}
}

View File

@ -8,7 +8,7 @@
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<revision>0-SNAPSHOT</revision> <revision>0-SNAPSHOT</revision>
<natives-version>3.3.28</natives-version> <natives-version>3.3.37</natives-version>
</properties> </properties>
<repositories> <repositories>
<repository> <repository>
@ -161,17 +161,76 @@
</execution> </execution>
</executions> </executions>
</plugin> </plugin>
<!-- ensure the project is compiling with JDK 9+ -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.0.0-M3</version>
<executions>
<execution>
<id>enforce-jdk9</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireJavaVersion>
<version>[1.9,)</version>
<message>JDK 9+ is required for compilation</message>
</requireJavaVersion>
</rules>
</configuration>
</execution>
</executions>
</plugin>
<!-- compile sources -->
<plugin> <plugin>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version> <version>3.8.1</version>
<configuration> <configuration>
<encoding>UTF-8</encoding> <encoding>UTF-8</encoding>
<release>11</release> <release>8</release>
<useIncrementalCompilation>false</useIncrementalCompilation> <useIncrementalCompilation>false</useIncrementalCompilation>
<excludes> <excludes>
<exclude>it/tdlight/tdlight/ClientManager.java</exclude> <exclude>it/tdlight/tdlight/ClientManager.java</exclude>
</excludes> </excludes>
</configuration> </configuration>
<executions>
<!-- disable default phase due to fixed id and position in lifecycle -->
<execution>
<id>default-compile</id>
<phase>none</phase>
<!-- specify source/target for IDE integration -->
<configuration>
<release>9</release>
</configuration>
</execution>
<!-- compile sources with Java 9 to generate and validate module-info.java -->
<execution>
<id>java-9-module-compile</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<release>9</release>
</configuration>
</execution>
<!-- recompile sources as Java 8 to overwrite Java 9 class files, except module-info.java -->
<execution>
<id>java-8-compile</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<!-- specify JDK 9+ release flag to ensure no classes/methods later than Java 8 are used accidentally -->
<release>8</release>
<!-- exclude module-info.java from the compilation, as it is unsupported by Java 8 -->
<excludes>
<exclude>module-info.java</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin> </plugin>
<plugin> <plugin>
<artifactId>maven-jar-plugin</artifactId> <artifactId>maven-jar-plugin</artifactId>

View File

@ -8,7 +8,7 @@
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<revision>0-SNAPSHOT</revision> <revision>0-SNAPSHOT</revision>
<natives-version>3.3.28</natives-version> <natives-version>3.3.37</natives-version>
</properties> </properties>
<repositories> <repositories>
<repository> <repository>
@ -161,17 +161,76 @@
</execution> </execution>
</executions> </executions>
</plugin> </plugin>
<!-- ensure the project is compiling with JDK 9+ -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.0.0-M3</version>
<executions>
<execution>
<id>enforce-jdk9</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireJavaVersion>
<version>[1.9,)</version>
<message>JDK 9+ is required for compilation</message>
</requireJavaVersion>
</rules>
</configuration>
</execution>
</executions>
</plugin>
<!-- compile sources -->
<plugin> <plugin>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version> <version>3.8.1</version>
<configuration> <configuration>
<encoding>UTF-8</encoding> <encoding>UTF-8</encoding>
<release>11</release> <release>8</release>
<useIncrementalCompilation>false</useIncrementalCompilation> <useIncrementalCompilation>false</useIncrementalCompilation>
<excludes> <excludes>
<exclude>it/tdlight/tdlib/ClientManager.java</exclude> <exclude>it/tdlight/tdlib/ClientManager.java</exclude>
</excludes> </excludes>
</configuration> </configuration>
<executions>
<!-- disable default phase due to fixed id and position in lifecycle -->
<execution>
<id>default-compile</id>
<phase>none</phase>
<!-- specify source/target for IDE integration -->
<configuration>
<release>9</release>
</configuration>
</execution>
<!-- compile sources with Java 9 to generate and validate module-info.java -->
<execution>
<id>java-9-module-compile</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<release>9</release>
</configuration>
</execution>
<!-- recompile sources as Java 8 to overwrite Java 9 class files, except module-info.java -->
<execution>
<id>java-8-compile</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<!-- specify JDK 9+ release flag to ensure no classes/methods later than Java 8 are used accidentally -->
<release>8</release>
<!-- exclude module-info.java from the compilation, as it is unsupported by Java 8 -->
<excludes>
<exclude>module-info.java</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin> </plugin>
<plugin> <plugin>
<groupId>org.codehaus.mojo</groupId> <groupId>org.codehaus.mojo</groupId>