Search in sources :

Example 1 with PublishOptions

use of io.crossbar.autobahn.wamp.types.PublishOptions in project autobahn-java by crossbario.

the class CIService method producer.

private void producer(Session session) {
    RegisterOptions options = new RegisterOptions(null, "roundrobin");
    CompletableFuture<Registration> regFuture = session.register("io.crossbar.example.client2.stop_producing", this::stopProducing, options);
    regFuture.whenComplete((registration, throwable) -> {
        if (throwable == null) {
            System.out.println("----------------------------");
            System.out.println("procedure registered: io.crossbar.example.client2.add2");
        }
    });
    final int[] counter = { 0 };
    final PublishOptions publishOptions = new PublishOptions(true, true);
    ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
    executorService.scheduleAtFixedRate(() -> {
        CompletableFuture<Publication> pubFuture = session.publish("io.crossbar.example.client2.oncounter", publishOptions, counter[0]);
        pubFuture.whenComplete((publication, throwable) -> {
            if (throwable == null) {
                LOGGER.i("published to 'oncounter' with counter " + counter[0]);
                counter[0] += 1;
            } else {
                LOGGER.i(String.format("ERROR - pub failed: %s", throwable.getMessage()));
            }
        });
        if (!mProduce) {
            executorService.shutdown();
            try {
                POJOCalls();
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
                System.exit(1);
            }
        }
    }, 0, 2, TimeUnit.SECONDS);
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Registration(io.crossbar.autobahn.wamp.types.Registration) Publication(io.crossbar.autobahn.wamp.types.Publication) PublishOptions(io.crossbar.autobahn.wamp.types.PublishOptions) ExecutionException(java.util.concurrent.ExecutionException) RegisterOptions(io.crossbar.autobahn.wamp.types.RegisterOptions)

Example 2 with PublishOptions

use of io.crossbar.autobahn.wamp.types.PublishOptions in project autobahn-java by crossbario.

the class ExampleClient method onJoinCallback.

private void onJoinCallback(Session session, SessionDetails details) {
    CompletableFuture<Registration> regFuture = session.register(PROC_ADD2, this::add2);
    regFuture.thenAccept(reg -> LOGGER.info("Registered procedure: com.example.add2"));
    CompletableFuture<Subscription> subFuture = session.subscribe(TOPIC_COUNTER, this::onCounter);
    subFuture.thenAccept(subscription -> LOGGER.info(String.format("Subscribed to topic: %s", subscription.topic)));
    final int[] x = { 0 };
    final int[] counter = { 0 };
    final PublishOptions publishOptions = new PublishOptions(true, false);
    ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
    executorService.scheduleAtFixedRate(() -> {
        // here we CALL every second
        CompletableFuture<CallResult> f = session.call(PROC_ADD2, x[0], 3);
        f.whenComplete((callResult, throwable) -> {
            if (throwable == null) {
                LOGGER.info(String.format("Got result: %s, ", callResult.results.get(0)));
                x[0] += 1;
            } else {
                LOGGER.info(String.format("ERROR - call failed: %s", throwable.getMessage()));
            }
        });
        CompletableFuture<Publication> p = session.publish(TOPIC_COUNTER, publishOptions, counter[0], session.getID(), "Java");
        p.whenComplete((publication, throwable) -> {
            if (throwable == null) {
                LOGGER.info("published to 'oncounter' with counter " + counter[0]);
                counter[0] += 1;
            } else {
                LOGGER.info(String.format("ERROR - pub failed: %s", throwable.getMessage()));
            }
        });
    }, 0, 2, TimeUnit.SECONDS);
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) CallResult(io.crossbar.autobahn.wamp.types.CallResult) Registration(io.crossbar.autobahn.wamp.types.Registration) Publication(io.crossbar.autobahn.wamp.types.Publication) PublishOptions(io.crossbar.autobahn.wamp.types.PublishOptions) Subscription(io.crossbar.autobahn.wamp.types.Subscription)

Aggregations

Publication (io.crossbar.autobahn.wamp.types.Publication)2 PublishOptions (io.crossbar.autobahn.wamp.types.PublishOptions)2 Registration (io.crossbar.autobahn.wamp.types.Registration)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 CallResult (io.crossbar.autobahn.wamp.types.CallResult)1 RegisterOptions (io.crossbar.autobahn.wamp.types.RegisterOptions)1 Subscription (io.crossbar.autobahn.wamp.types.Subscription)1 ExecutionException (java.util.concurrent.ExecutionException)1