public static void main(String[] args) throws Throwable { ServiceHost host = ServiceHost.create(); host.start(); host.startDefaultCoreServicesSynchronously(); // A stateless service that enumerates all the // factory services started on the Service host. host.startService(new RootNamespaceService()); // start an example factory for folks that want to experiment with service instances host.startFactory(ExampleService.class, ExampleService::createFactory); }
public static void main(String[] args) throws Throwable { ServiceHost host = ServiceHost.create(); host.start(); host.startDefaultCoreServicesSynchronously(); // Start the root namespace factory: this will respond to the root URI (/) and list all // the factory services. host.startService(new RootNamespaceService()); host.startFactory(MySubscriptionService.class, MySubscriptionService::createFactory); // subscription callback Consumer<Operation> target = (notifyOp) -> { System.out.println("notification: " + notifyOp); notifyOp.complete(); }; Operation createSub = Operation.createPost( UriUtils.buildUri(host, MySubscriptionService.FACTORY_LINK)) .setReferer(host.getUri()); // start subscription when factory became available // For reliable subscription, use "startReliableSubscriptionService()" instead of "startSubscriptionService()" host.registerForServiceAvailability((o, e) -> { host.startSubscriptionService(createSub, target); }, MySubscriptionService.FACTORY_LINK); }
static ServiceHost createAndStartHost(String... args) throws Throwable { ServiceHost host = ServiceHost.create(args); host.start(); host.startDefaultCoreServicesSynchronously(); host.startService(new RootNamespaceService()); host.startFactory(new UpgradeDemoEmployeeService()); // In order to support upgrading your Xenon application, you need to start the MigrationTaskService host.startFactory(new MigrationTaskService()); // Added for 'add-logging' project. host.log(Level.INFO, "Starting host [add-logging] with args these args:%n[id=%s]%n[port=%d]%n[sandbox=%s]%n[peerNodes=%s]", host.getId(), host.getPort(), host.getStorageSandbox(), host.getInitialPeerHosts()); return host; }
public static void main(String[] args) throws Throwable { ServiceHost host = ServiceHost.create(); host.start(); host.startDefaultCoreServicesSynchronously(); host.startFactory(TaskDemoService.class, TaskDemoService::createFactory); host.startFactory(ExampleService.class, ExampleService::createFactory); }
/** * Build the URI for the auth provider host */ public static URI buildAuthProviderHostUri(ServiceHost host, String serviceLink) { URI uri = host.getStateNoCloning().authProviderHostURI; URI baseUri = uri != null ? uri : host.getUri(); return UriUtils.extendUri(baseUri, serviceLink); }
public static void main(String[] args) throws Throwable { ServiceHost host = ServiceHost.create("--sandbox=/tmp/xenon-demo"); host.start(); host.startDefaultCoreServicesSynchronously(); host.startService(new DemoStatelessService()); initialState.stringValue = "A string value"; Operation post = Operation.createPost(host, DemoStatefulService.SELF_LINK).setBody(initialState); host.startService(post, new DemoStatefulService()); host.startFactoryServicesSynchronously(new DemoFactoryService()); .setReferer(host.getUri()) .setCompletion((o, e) -> { if (e != null) { host.log(Level.SEVERE, "Post failed: %s", Utils.toString(e)); return; host.log(Level.INFO, "Post completed with code %d and state %s", o.getStatusCode(), Utils.toJsonHtml(o.getBody(DemoStatefulService.State.class))); }); host.sendRequest(post);
@Test public void create() throws Throwable { ServiceHost h = ServiceHost.create("--port=0"); try { h.start(); h.startDefaultCoreServicesSynchronously(); // Start the example service factory h.startFactory(ExampleService.class, ExampleService::createFactory); boolean[] isReady = new boolean[1]; h.registerForServiceAvailability((o, e) -> { isReady[0] = true; }, ExampleService.FACTORY_LINK); Duration timeout = Duration.of(ServiceHost.ServiceHostState.DEFAULT_MAINTENANCE_INTERVAL_MICROS * 5, ChronoUnit.MICROS); TestContext.waitFor(timeout, () -> { return isReady[0]; }, "ExampleService did not start"); // verify ExampleService exists TestRequestSender sender = new TestRequestSender(h); Operation get = Operation.createGet(h, ExampleService.FACTORY_LINK); sender.sendAndWait(get); } finally { if (h != null) { h.unregisterRuntimeShutdownHook(); h.stop(); } } }
private static void registerEventTopic(ServiceHost host, EventTopicState topic) { host.sendRequest(Operation.createPost(host, EventTopicService.FACTORY_LINK) .setReferer(host.getUri()) .setBody(topic) .addPragmaDirective( Operation.PRAGMA_DIRECTIVE_QUEUE_FOR_SERVICE_AVAILABILITY) .setCompletion((o, e) -> { if (e != null) { host.log(Level.SEVERE, String.format( "Unable to register '%s' topic. " + "Exception: %s", topic.name, e.getMessage())); } })); }
public static void startServices(ServiceHost host) { try { host.startFactory( PhotonModelAdaptersRegistryService.class, PhotonModelAdaptersRegistryFactoryService::new); host.startFactory( ResourceOperationSpecService.class, ResourceOperationSpecFactoryService::new); host.startService(new ResourceOperationService()); } catch (Exception e) { host.log(Level.WARNING, "Error on start adapter registry related services. %s", Utils.toString(e)); throw e; } } }
private void handleFailure() { this.host.log(Level.WARNING, this.failureMessage); if (this.completion != null) { this.completion.handle(new IllegalStateException(this.failureMessage)); } return; }
/** * Start a service using the specified operation and service */ public ServiceHost startService(Operation post, Service service) { return startService(post, service, null); }
private void stopServiceAndClearFromCache(Service s, ServiceDocument state) { // Issue DELETE to stop the service and clear it from cache Operation deleteExp = Operation.createDelete(this.host, s.getSelfLink()) .setBody(state) .disableFailureLogging(true) .addPragmaDirective(Operation.PRAGMA_DIRECTIVE_NO_INDEX_UPDATE) .addPragmaDirective(Operation.PRAGMA_DIRECTIVE_NO_FORWARDING) .setReferer(this.host.getUri()); this.host.sendRequest(deleteExp); }
private void cleanCache() { if (lastUsed + CACHED_DATA_MICROS < Utils.getSystemNowMicrosUtc()) { // expired, clean the reference cachedImages.clear(); lastUsed = 0; host.log(Level.INFO, "System image(s) removed from cache"); } else { // schedule next check host.schedule(this::cleanCache, CACHED_DATA_MICROS, TimeUnit.MICROSECONDS); } }
public static URI buildUri(ServiceHost host, String path, String query, String userInfo) { URI base = host.getUri(); return UriUtils.buildUri(base.getScheme(), base.getHost(), base.getPort(), path, query, userInfo); }
@Override public ServiceHost start() throws Throwable { super.start(); startDefaultCoreServicesSynchronously(); // Start a factory for gathering test statistics super.startService( Operation.createPost(UriUtils.buildUri(this, TestResultsFactoryService.class)), new TestResultsFactoryService()); return this; } }
public void stopHostAndPreserveState(ServiceHost host) { log("Stopping host %s", host.getUri()); // Do not delete the temporary directory with the lucene index. Notice that // we do not call host.tearDown(), which will delete disk state, we simply // stop the host and remove it from the peer node tracking tables host.stop(); this.peerHostIdToNodeState.remove(host.getId()); this.peerNodeGroups.remove(host.getUri()); this.localPeerHosts.remove(host.getUri()); }
private Service startDynamicService(Class<?> clazz) throws IllegalArgumentException, IllegalAccessException, InstantiationException { Service service = (Service) clazz.newInstance(); URI link = null; // If it's a service if (getSelfOrFactoryLink(clazz).getName().equals(UriUtils.FIELD_NAME_SELF_LINK)) { link = UriUtils.buildUri(getHost(), service.getClass()); getHost().startService( Operation.createPost(link), service); } else { // If it's a factory link = UriUtils.buildFactoryUri(getHost(), service.getClass()); getHost().startFactory(service); } logInfo("Started service " + link); return service; }
public void registerForServiceAvailability(CompletionHandler completion, boolean checkReplica, String... servicePaths) { registerForServiceAvailability(completion, ServiceUriPaths.DEFAULT_NODE_SELECTOR, checkReplica, servicePaths); }
/** * Schedules a task using the shared service executor */ public ScheduledFuture<?> schedule(Runnable task, long delay, TimeUnit unit) { return schedule(this.serviceScheduledExecutor, task, delay, unit); }
void doHealthCheck(ServiceHost host, String containerDescriptionLink) { host.sendRequest(Operation .createGet(host, containerDescriptionLink) .setReferer(UriUtils.buildUri(host, SERVICE_REFERRER_PATH)) .setCompletion((o, ex) -> { if (ex != null) { host.log(Level.SEVERE, "Failed to fetch %s : %s", containerDescriptionLink, Utils.toJson(ex)); } else { processContainerHealth(host, o.getBody(ContainerDescription.class)); } })); }