javahashmapautobahnmethod-referencecrossbar

Registering remote procedures with autobahn java


The standard way of registering procedures in autobahn-java is:

CompletableFuture<Registration> order_to_produce = session.register(prefix + "order_to_produce", this::order_to_produce);
order_to_produce.thenAccept(registration -> LOGGER.info("Registered procedure: " + registration.procedure));

Since we have a lot of functions to register I was thinking to put registration names and functions to a HashMap but I am unable to reference any function with method reference.

I would like to make something like this:

    Map<String, Runnable> functions = new HashMap<>();
    functions.put("order_to_produce", this::order_to_produce);


    for (Map.Entry<String, Runnable> function : functions.entrySet())
    {
        CompletableFuture<Registration> registerProcedure = session.register(prefix + function.getKey(), function.getValue());
        registerProcedure.thenAccept(registration -> LOGGER.info("Registered procedure: " + registration.procedure));

        // or using reflection ?
        // 
        CompletableFuture<Registration> registerProcedure = session.register(prefix + function.getKey(), obj.getClass().getMethod(function.getKey()));
        registerProcedure.thenAccept(registration -> LOGGER.info("Registered procedure: " + registration.procedure));
    }

Is there a way to do this ?

In python I could do that with ease on backend component.

functions = {'backend.add_service': self.add_service,
             'backend.online_services': self.services_online}

for functionToRegister in functions.keys():
    yield self.register(functions[functionToRegister], functionToRegister, options=self.REGISTER_OPTIONS)

Regards, Marko.


Solution

  • I'm still interested in original question but for now I made it so:

    LinkedList<CompletableFuture<Registration>> functions = new LinkedList<>();
    functions.add(session.register(prefix + "order_to_produce", this::order_to_produce));
    
    for (CompletableFuture<Registration> function : functions) {
        function.thenAccept(registration -> LOGGER.info("Registered procedure: " + registration.procedure));
    }