Search in sources :

Example 1 with ConstantMonitor

use of services.moleculer.monitor.ConstantMonitor in project moleculer-java by moleculer-java.

the class EventbusTest method setUp.

// --- SET UP ---
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
protected void setUp() throws Exception {
    sr = new DefaultServiceRegistry();
    tr = new TestTransporter();
    ExecutorService ex = new ExecutorService() {

        @Override
        public void execute(Runnable command) {
            command.run();
        }

        @Override
        public <T> Future<T> submit(Runnable task, T result) {
            task.run();
            return CompletableFuture.completedFuture(result);
        }

        @Override
        public Future<?> submit(Runnable task) {
            task.run();
            return CompletableFuture.completedFuture(null);
        }

        @Override
        public <T> Future<T> submit(Callable<T> task) {
            try {
                return CompletableFuture.completedFuture(task.call());
            } catch (Exception e) {
                CompletableFuture future = CompletableFuture.completedFuture(null);
                future.completeExceptionally(e);
                return future;
            }
        }

        @Override
        public List<Runnable> shutdownNow() {
            return Collections.emptyList();
        }

        @Override
        public void shutdown() {
        }

        @Override
        public boolean isTerminated() {
            return false;
        }

        @Override
        public boolean isShutdown() {
            return false;
        }

        @Override
        public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
            return null;
        }

        @Override
        public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException {
            return null;
        }

        @Override
        public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException {
            return null;
        }

        @Override
        public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException {
            return null;
        }

        @Override
        public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
            return false;
        }
    };
    br = ServiceBroker.builder().monitor(new ConstantMonitor()).registry(sr).transporter(tr).nodeID("local").executor(ex).build();
    br.start();
}
Also used : DefaultServiceRegistry(services.moleculer.service.DefaultServiceRegistry) TestTransporter(services.moleculer.breaker.TestTransporter) CompletableFuture(java.util.concurrent.CompletableFuture) ExecutorService(java.util.concurrent.ExecutorService) Collection(java.util.Collection) TimeUnit(java.util.concurrent.TimeUnit) CompletableFuture(java.util.concurrent.CompletableFuture) Future(java.util.concurrent.Future) ConstantMonitor(services.moleculer.monitor.ConstantMonitor) Callable(java.util.concurrent.Callable) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with ConstantMonitor

use of services.moleculer.monitor.ConstantMonitor in project moleculer-java by moleculer-java.

the class CircuitBreakerTest method setUp.

// --- SET UP ---
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
protected void setUp() throws Exception {
    sr = new DefaultServiceRegistry();
    tr = new TestTransporter();
    cb = new DefaultCircuitBreaker();
    cb.setMaxErrors(3);
    cb.setEnabled(true);
    ExecutorService ex = new ExecutorService() {

        @Override
        public void execute(Runnable command) {
            command.run();
        }

        @Override
        public <T> Future<T> submit(Runnable task, T result) {
            task.run();
            return CompletableFuture.completedFuture(result);
        }

        @Override
        public Future<?> submit(Runnable task) {
            task.run();
            return CompletableFuture.completedFuture(null);
        }

        @Override
        public <T> Future<T> submit(Callable<T> task) {
            try {
                return CompletableFuture.completedFuture(task.call());
            } catch (Exception e) {
                CompletableFuture future = CompletableFuture.completedFuture(null);
                future.completeExceptionally(e);
                return future;
            }
        }

        @Override
        public List<Runnable> shutdownNow() {
            return Collections.emptyList();
        }

        @Override
        public void shutdown() {
        }

        @Override
        public boolean isTerminated() {
            return false;
        }

        @Override
        public boolean isShutdown() {
            return false;
        }

        @Override
        public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
            return null;
        }

        @Override
        public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException {
            return null;
        }

        @Override
        public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException {
            return null;
        }

        @Override
        public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException {
            return null;
        }

        @Override
        public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
            return false;
        }
    };
    br = ServiceBroker.builder().monitor(new ConstantMonitor()).registry(sr).transporter(tr).nodeID("local").breaker(cb).executor(ex).build();
    br.start();
    for (int i = 0; i < 10; i++) {
        Tree config = new Tree();
        config.put("nodeID", "node" + i);
        Tree actions = config.putMap("actions");
        LinkedHashMap<String, Object> action = new LinkedHashMap<>();
        action.put("name", "test.test");
        ((Map) actions.asObject()).put("test.test", action);
        sr.addActions(config);
    }
}
Also used : ConstantMonitor(services.moleculer.monitor.ConstantMonitor) Callable(java.util.concurrent.Callable) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) LinkedHashMap(java.util.LinkedHashMap) DefaultServiceRegistry(services.moleculer.service.DefaultServiceRegistry) CompletableFuture(java.util.concurrent.CompletableFuture) ExecutorService(java.util.concurrent.ExecutorService) Collection(java.util.Collection) TimeUnit(java.util.concurrent.TimeUnit) CompletableFuture(java.util.concurrent.CompletableFuture) Future(java.util.concurrent.Future) Tree(io.datatree.Tree) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

Collection (java.util.Collection)2 Callable (java.util.concurrent.Callable)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 ExecutionException (java.util.concurrent.ExecutionException)2 ExecutorService (java.util.concurrent.ExecutorService)2 Future (java.util.concurrent.Future)2 TimeUnit (java.util.concurrent.TimeUnit)2 TimeoutException (java.util.concurrent.TimeoutException)2 ConstantMonitor (services.moleculer.monitor.ConstantMonitor)2 DefaultServiceRegistry (services.moleculer.service.DefaultServiceRegistry)2 Tree (io.datatree.Tree)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 TestTransporter (services.moleculer.breaker.TestTransporter)1