Mercurial > hg > orthanc-book
changeset 1193:67813246f03e
added java samples for key-value stores and queues
| author | Sebastien Jodogne <s.jodogne@gmail.com> |
|---|---|
| date | Thu, 21 Aug 2025 15:48:18 +0200 |
| parents | 211d3cf87e6d |
| children | c717ee80e8d9 |
| files | Sphinx/source/plugins/java.rst Sphinx/source/plugins/java/KeyValueStore.java Sphinx/source/plugins/java/Queue.java |
| diffstat | 3 files changed, 103 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/Sphinx/source/plugins/java.rst Sat Aug 16 13:19:02 2025 +0200 +++ b/Sphinx/source/plugins/java.rst Thu Aug 21 15:48:18 2025 +0200 @@ -77,7 +77,7 @@ $ mkdir BuildPlugin $ cd BuildPlugin - $ cmake ../Plugin -DCMAKE_BUILD_TYPE=Release + $ cmake ../Plugin -DCMAKE_BUILD_TYPE=Release -DUSE_SYSTEM_ORTHANC_SDK=OFF $ make This requires the `JNI (Java Native Interface) @@ -120,7 +120,7 @@ $ mkdir BuildJavaSDK $ cd BuildJavaSDK - $ cmake ../JavaSDK + $ cmake ../JavaSDK -DUSE_SYSTEM_ORTHANC_SDK=OFF $ make This requires a JDK to be installed on your computer. This generates @@ -195,7 +195,7 @@ LoadLibrary(C:\Program Files\Orthanc Server\Plugins\OrthancJava.dll) failed: Error 126 -This very likely means that OrthancJava.dll can not find the ``jvm.dll``. Check your system ``Path``. +This very likely means that OrthancJava.dll can not find the ``jvm.dll``. Check your system ``Path`` environment variable. If you get the following errors:: @@ -229,6 +229,27 @@ :language: java +Using the key-value store (new in 2.0) +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Starting with release 2.0, Java applications can access the key-value +NoSQL database that is built in Orthanc >= 1.12.8: + +.. literalinclude:: java/KeyValueStore.java + :language: java + + +Using the queues (new in 2.0) +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Starting with release 2.0, Java applications can access the queue +facility that is built in Orthanc >= 1.12.8: + +.. literalinclude:: java/Queue.java + :language: java + + + Additional samples ^^^^^^^^^^^^^^^^^^
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Sphinx/source/plugins/java/KeyValueStore.java Thu Aug 21 15:48:18 2025 +0200 @@ -0,0 +1,33 @@ +import be.uclouvain.orthanc.Callbacks; +import be.uclouvain.orthanc.ChangeType; +import be.uclouvain.orthanc.Functions; +import be.uclouvain.orthanc.ResourceType; +import be.uclouvain.orthanc.KeysValuesIterator; + +public class KeyValueStore { + static { + // Wait for Orthanc to start so that the database becomes available + Callbacks.register(new Callbacks.OnChange() { + @Override + public void call(ChangeType changeType, ResourceType resourceType, String resourceId) { + if (changeType == ChangeType.ORTHANC_STARTED) { + Functions.storeKeyValue("my_store", "hello", "world".getBytes()); + System.out.println(new String(Functions.getKeyValue("my_store", "hello"))); // "hello" + + Functions.storeKeyValue("my_store", "hello2", "world2".getBytes()); + Functions.storeKeyValue("my_store", "hello3", "world3".getBytes()); + Functions.storeKeyValue("other_store", "hello4", "world4".getBytes()); + + // Loop over the keys "hello", "hello2", and "hello3" + KeysValuesIterator iterator = KeysValuesIterator.createKeysValuesIterator("my_store"); + while (iterator.next()) { + System.out.println(iterator.getKey() + " => " + new String(iterator.getValue())); + } + + Functions.deleteKeyValue("my_store", "hello"); + System.out.println(Functions.getKeyValue("my_store", "hello")); // "null" + } + } + }); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Sphinx/source/plugins/java/Queue.java Thu Aug 21 15:48:18 2025 +0200 @@ -0,0 +1,46 @@ +import be.uclouvain.orthanc.Callbacks; +import be.uclouvain.orthanc.QueueOrigin; +import be.uclouvain.orthanc.ChangeType; +import be.uclouvain.orthanc.Functions; +import be.uclouvain.orthanc.ResourceType; + +public class Queue { + static { + // Wait for Orthanc to start so that the database becomes available + Callbacks.register(new Callbacks.OnChange() { + @Override + public void call(ChangeType changeType, ResourceType resourceType, String resourceId) { + if (changeType == ChangeType.ORTHANC_STARTED) { + System.out.println(Functions.getQueueSize("my_queue")); // 0 + + for (int i = 0; i < 5; i++) { + Functions.enqueueValue("my_queue", ("hello " + i).getBytes()); + Functions.enqueueValue("my_queue_2", ("hello " + i).getBytes()); + } + + System.out.println(Functions.getQueueSize("my_queue")); // 5 + + // The following loop will print "hello 0", "hello 1", "hello 2", "hello 3", and "hello 4" + for (;;) { + byte[] value = Functions.dequeueValue("my_queue", QueueOrigin.FRONT); + if (value == null) { + break; + } else { + System.out.println(new String(value)); + } + } + + // The following loop will print "hello 4", "hello 3", "hello 2", "hello 1", and "hello 0" + for (;;) { + byte[] value = Functions.dequeueValue("my_queue_2", QueueOrigin.BACK); + if (value == null) { + break; + } else { + System.out.println(new String(value)); + } + } + } + } + }); + } +}
