@Override public Task<Void> callRandomB() { if (Math.random() > 0.5d) return Task.done(); // some variance to the calls String id = Integer.toString((int) (Math.random() * 10)); ExampleB b = Actor.getReference(ExampleB.class, id); b.someWork().join(); return Task.done(); }
public Task<Void> stop() { try { server.stop(); } catch (Exception e) { logger.error("Error stopping jetty", e); throw new UncheckedException(e); } return Task.done(); }
@Override public Task write(final HandlerContext ctx, final Object msg) throws Exception { if (!(msg instanceof Pair)) { return ctx.write(msg); } Pair<NodeAddress, byte[]> message = (Pair<NodeAddress, byte[]>) msg; clusterPeer.sendMessage(message.getLeft(), message.getRight()); return Task.done(); }
@Override public Task<Integer> local(int i1, int i2) { // Start Transaction await( transaction( () -> { state().incrementBalance(i1); if (i1 < 0) { throw new IllegalArgumentException("i1: " + i1); } return Task.done(); })); await( transaction( () -> { state().incrementBalance(i2); if (i2 < 0) { throw new IllegalArgumentException("i2: " + i2); } return Task.done(); })); return Task.fromValue(state().balance); // End Transaction }
private Task<Void> onActivate(HandlerContext ctx, final Invocation invocation) { // this must run serialized by the remote reference key. LocalObjects.LocalObjectEntry<Object> entry = objects.findLocalObjectByReference(invocation.getToReference()); if (entry == null) { objects.registerLocalObject(invocation.getToReference(), null); entry = objects.findLocalObjectByReference(invocation.getToReference()); } // queues the invocation final LocalObjects.LocalObjectEntry<Object> theEntry = entry; final Task result = entry.run(target -> performInvocation(ctx, invocation, theEntry, target)); if (invocation.getCompletion() != null) { InternalUtils.linkFuturesOnError(result, invocation.getCompletion()); } // yielding since we blocked the entry before running on activate (serialized execution) return Task.done(); }
@Override public Task<Integer> remote(int i1, Disjunct other, int i2) { // Start Transaction await( transaction( () -> { state().incrementBalance(i1); if (i1 < 0) { // the nested transaction was completed // this will force the parent to cancel the nested throw new IllegalArgumentException("i1: " + i1); } return Task.done(); })); final int r1 = await(other.singleLevel(i2)); return Task.fromValue(state().balance + r1); // End Transaction }
@Override public Task<Void> cleanup() { return Task.done(); }
@SuppressWarnings({"unchecked", "rawtypes"}) public Task<Void> start() { // Ensuring that jersey will use singletons from the orbit container. ServiceLocator locator = Injections.createLocator(); DynamicConfigurationService dcs = locator.getService(DynamicConfigurationService.class); DynamicConfiguration dc = dcs.createDynamicConfiguration(); final List<Class<?>> classes = new ArrayList<>(providers); if (container != null) { classes.addAll(container.getClasses()); for (final Class<?> c : container.getClasses()) { if (c.isAnnotationPresent(Singleton.class)) { Injections.addBinding( Injections.newFactoryBinder( new Factory() { @Override public Object provide() { return container.get(c); } @Override public void dispose(final Object instance) {} }) .to(c), dc); } } } dc.commit(); final ResourceConfig resourceConfig = new ResourceConfig(); // installing jax-rs classes known by the orbit container. for (final Class c : classes) { if (c.isAnnotationPresent(javax.ws.rs.Path.class) || c.isAnnotationPresent(javax.ws.rs.ext.Provider.class)) { resourceConfig.register(c); } } final WebAppContext webAppContext = new WebAppContext(); final ProtectionDomain protectionDomain = EmbeddedHttpServer.class.getProtectionDomain(); final URL location = protectionDomain.getCodeSource().getLocation(); logger.info(location.toExternalForm()); webAppContext.setInitParameter("useFileMappedBuffer", "false"); webAppContext.setWar(location.toExternalForm()); // this sets the default service locator to one that bridges to the orbit container. webAppContext.getServletContext().setAttribute(ServletProperties.SERVICE_LOCATOR, locator); webAppContext.setContextPath("/*"); webAppContext.addServlet(new ServletHolder(new ServletContainer(resourceConfig)), "/*"); final ContextHandler resourceContext = new ContextHandler(); ResourceHandler resourceHandler = new ResourceHandler(); resourceHandler.setDirectoriesListed(true); resourceHandler.setWelcomeFiles(new String[] {"index.html"}); resourceHandler.setBaseResource(Resource.newClassPathResource("/web")); resourceContext.setHandler(resourceHandler); resourceContext.setInitParameter("useFileMappedBuffer", "false"); final ContextHandlerCollection contexts = new ContextHandlerCollection(); contexts.setHandlers( new Handler[] { wrapHandlerWithMetrics(resourceContext, "resourceContext"), wrapHandlerWithMetrics(webAppContext, "webAppContext") }); server = new Server(port); server.setHandler(contexts); try { /// Initialize javax.websocket layer final ServerContainer serverContainer = WebSocketServerContainerInitializer.configureContext(webAppContext); for (Class c : classes) { if (c.isAnnotationPresent(ServerEndpoint.class)) { final ServerEndpoint annotation = (ServerEndpoint) c.getAnnotation(ServerEndpoint.class); final ServerEndpointConfig serverEndpointConfig = ServerEndpointConfig.Builder.create(c, annotation.value()) .configurator( new ServerEndpointConfig.Configurator() { @Override public <T> T getEndpointInstance(final Class<T> endpointClass) throws InstantiationException { return container.get(endpointClass); } }) .build(); serverContainer.addEndpoint(serverEndpointConfig); } } } catch (Exception e) { logger.error("Error starting jetty", e); throw new UncheckedException(e); } try { server.start(); } catch (Exception e) { logger.error("Error starting jetty", e); throw new UncheckedException(e); } return Task.done(); }
@Override public Task<Void> addPlayerToMap(SomePlayer player) { state().playerMap.put(player.hashCode(), player); await(writeState()); return Task.done(); }