Hi Christian,
Tank you for your quick answer and I’m sorry for my late reply.

In the meanwhile I figure out how to achieve my goal and the solution was easy 
as I could imagine: instead of perform all the operations in the Java class 
constructor I pass the XQuery function to be executed by using a method 
annotated with the ContextDependent annotation, then “saving" a reference to 
the current Context taken from the queryContext variable that now is present 
and not null. Then, in the callback Java method, I create a new QueryContext 
object from the saved Context instance and the function invocation works fine.

Regarding the isolation of the function itself, my tests seem to work as 
expected by passing inline code but in general, at runtime, we plan to use it 
by referring the function from a specific query module (e.g. myns:myfunction#5) 
and also this scenario is working correctly and I think we can rely on a 
correct function execution in this way, or am I wrong?

Thank you again and regards,

    Mauro


> Il giorno 18 giu 2025, alle ore 17:27, Christian Grün <[email protected]> ha 
> scritto:
> 
> Hi Mauro,
> 
> It is generally tricky to invoke XQuery functions outside the original 
> context. Function items are difficult to isolate, as they may contain 
> references to other parts of the containing code. This is also the reason why 
> the Job Module (which allows asynchronous execution of code) requires XQuery 
> code to be specified as a strings or a file reference [1].
> 
> Without promising anything, I may be able to give more suggestions if you 
> provide me with a self-contained and minimized version of your use case.
> 
> Best,
> Christian
> 
> [1] https://docs.basex.org/main/Job_Functions
> 
> Von: Mauro Mugnaini
> Gesendet: Dienstag, 17. Juni 2025 18:34
> Bis: BaseX
> Betreff: [basex-talk] Execute an Query function from Java code 
> 
> Hi all,
> I’m trying to invoke an XQuery function (instance of FItem) from a Java class 
> that acts a callback listener for a Java client adapter.
> 
> I’m able to instantiate and register it in a query module by using the Java 
> bindings, passing the reference to the XQuery code to invoke) but at the 
> moment that the function is invoked aQueryContext instance is needed (or at 
> least the current Context to create it if I understood correctly) of the 
> running BaseX service (an HTTP server in my case). I tried to find a way to 
> do that taking a look of the Jobs module and similar packages in the source 
> code but without success.
> 
> In the “Java" examples it is shown how to do this for code invoked during a 
> query execution in a class that is a subclass of the QueryModule and by using 
> the ContextDependent annotation that do the job automatically. It is not my 
> case since the code is not directly call during a query execution but 
> asynchronously in a separate thread.
> 
> Finally my questions: It is something that is possibile to do? Is there some 
> singleton I can access or other classes I can use to achieve that?
> 
> 
> Thanks and regards.
> 
>     Mauro

Attachment: signature.asc
Description: Message signed with OpenPGP

Reply via email to