Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions java-server/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
.kotlin/
bin/
build/
ditto-service.err.log
ditto-service.exe
ditto-service.out.log
ditto-service.wrapper.log
10 changes: 5 additions & 5 deletions java-server/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,29 @@ spotbugs {

dependencies {
// ditto-java artifact includes the Java API for Ditto
implementation("com.ditto:ditto-java:5.0.0-preview.3")
implementation("com.ditto:ditto-java:5.0.0-java-rc.2")

// This will include binaries for all the supported platforms and architectures
implementation("com.ditto:ditto-binaries:5.0.0-preview.3")
implementation("com.ditto:ditto-binaries:5.0.0-java-rc.2")

// To reduce your module artifact's size, consider including just the necessary platforms and architectures
/*
// macOS Apple Silicon
implementation("com.ditto:ditto-binaries:5.0.0-preview.3") {
implementation("com.ditto:ditto-binaries:5.0.0-java-rc.2") {
capabilities {
requireCapability("com.ditto:ditto-binaries-macos-arm64")
}
}

// Windows x86_64
implementation("com.ditto:ditto-binaries:5.0.0-preview.3") {
implementation("com.ditto:ditto-binaries:5.0.0-java-rc.2") {
capabilities {
requireCapability("com.ditto:ditto-binaries-windows-x64")
}
}

// Linux x86_64
implementation("com.ditto:ditto-binaries:5.0.0-preview.3") {
implementation("com.ditto:ditto-binaries:5.0.0-java-rc.2") {
capabilities {
requireCapability("com.ditto:ditto-binaries-linux-x64")
}
Expand Down
24 changes: 24 additions & 0 deletions java-server/ditto-service.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<service>
<id>ditto-service</id>
<name>Ditto Java Quickstart App</name>
<description>Description of your application</description>

<executable>java</executable>
<arguments>-jar "%BASE%\build\libs\quickstart-java-0.0.1-SNAPSHOT.jar"</arguments>

<!-- Optional: Set working directory -->
<workingdirectory>%BASE%</workingdirectory>

<!-- JVM options -->
<env name="JAVA_OPTS" value="-Xmx512m -Xms256m"/>

<!-- Logging -->
<log mode="roll-by-size">
<sizeThreshold>10240</sizeThreshold>
<keepFiles>8</keepFiles>
</log>

<!-- Service restart behavior -->
<onfailure action="restart" delay="10 sec"/>
<onfailure action="restart" delay="20 sec"/>
</service>
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public class DittoService implements DisposableBean {
* Setup Ditto Config
* https://docs.ditto.live/sdk/latest/install-guides/java#integrating-and-initializing
*/

DittoConfig dittoConfig = new DittoConfig.Builder(DittoSecretsConfiguration.DITTO_APP_ID)
// .persistenceDirectory("/tmp/ditto-quickstart")
.serverConnect(DittoSecretsConfiguration.DITTO_AUTH_URL)
.build();

Expand All @@ -60,7 +60,7 @@ public class DittoService implements DisposableBean {
).thenRun(() -> { })
);

this.ditto.setDeviceName("Spring Java");
this.ditto.setDeviceName("Java");

this.ditto.updateTransportConfig(config -> {
config.connect(connect -> {
Expand Down Expand Up @@ -99,10 +99,10 @@ public Flux<Boolean> getSyncState() {
}

public void toggleSync() {
boolean currentSyncState = mutableSyncStatePublisher.asFlux().blockFirst();
try {
boolean currentSyncState = mutableSyncStatePublisher.asFlux().blockFirst();
setSyncStateIntoDittoStore(!currentSyncState);
} catch (DittoError e) {
} catch (DittoException e) {
throw new RuntimeException(e);
}
}
Expand Down Expand Up @@ -145,16 +145,20 @@ private DittoStoreObserver setupAndObserveSyncState() {
(result) -> {
List<? extends DittoQueryResultItem> items = result.getItems();
boolean newSyncState = false;
if (!items.isEmpty()) {
newSyncState = items.get(0).getValue()
.get(DITTO_SYNC_STATE_ID)
.asBoolean();
try {
if (!items.isEmpty()) {
newSyncState = items.get(0).getValue()
.get(DITTO_SYNC_STATE_ID)
.asBoolean();
}
} catch (DittoException e) {
System.err.println("Error: " + e);
}

if (newSyncState) {
try {
ditto.startSync();
} catch (DittoError e) {
} catch (DittoException e) {
throw new RuntimeException(e);
}
} else {
Expand All @@ -163,12 +167,12 @@ private DittoStoreObserver setupAndObserveSyncState() {

mutableSyncStatePublisher.tryEmitNext(newSyncState);
});
} catch (DittoError e) {
} catch (DittoException e) {
throw new RuntimeException(e);
}
}

private void setSyncStateIntoDittoStore(boolean newState) throws DittoError {
private void setSyncStateIntoDittoStore(boolean newState) throws DittoException {
CompletionStage<DittoQueryResult> future = ditto.getStore().execute(
"UPDATE %s SET %s = :syncState".formatted(DITTO_SYNC_STATE_COLLECTION, DITTO_SYNC_STATE_ID),
DittoCborSerializable.buildDictionary()
Expand All @@ -178,7 +182,7 @@ private void setSyncStateIntoDittoStore(boolean newState) throws DittoError {

try {
future.toCompletableFuture().join().close();
} catch (DittoError e) {
} catch (DittoException e) {
throw new RuntimeException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ public void toggleTaskDone(@Nonnull String taskId) {
"UPDATE %s SET done = :done WHERE _id = :taskId".formatted(TASKS_COLLECTION_NAME),
DittoCborSerializable.Dictionary.buildDictionary()
.put("done", !isDone)
.put("taskId", taskId)
.put("taskId", taskId)
.build()
).toCompletableFuture().join();
} catch (Error e) {
} catch (Error | DittoException e) {
throw new RuntimeException(e);
}
}
Expand Down Expand Up @@ -115,23 +115,27 @@ public Flux<List<Task>> observeAll() {
}
try {
observer.close();
} catch (DittoError e) {
} catch (DittoException e) {
throw new RuntimeException(e);
}
});
} catch (DittoError e) {
} catch (DittoException e) {
emitter.error(e);
}
}, FluxSink.OverflowStrategy.LATEST);
}

private Task itemToTask(@Nonnull DittoQueryResultItem item) {
DittoCborSerializable.Dictionary value = item.getValue();
return new Task(
value.get("_id").asString(),
value.get("title").asString(),
value.get("done").asBoolean(),
value.get("deleted").asBoolean()
);
try {
return new Task(
value.get("_id").asString(),
value.get("title").asString(),
value.get("done").asBoolean(),
value.get("deleted").asBoolean()
);
} catch (DittoException e) {
throw new RuntimeException(e);
}
}
}
Loading