@Test public void testAnnounceCacheControl() throws Exception { httpClient.setProcessor((request -> mockResponse() .status(ACCEPTED) .header("Cache-Control", "max-age=75") .build())); Duration duration = client.announce(announcements).get(); assertEquals(duration, new Duration(75, TimeUnit.SECONDS)); }
@Override public ListenableFuture<Duration> announce(Set<ServiceAnnouncement> services) { requireNonNull(services, "services is null"); Builder builder; final boolean servicesEmpty = services.isEmpty(); if (servicesEmpty) { builder = prepareDelete(); } else { Announcement announcement = new Announcement(nodeInfo.getEnvironment(), nodeInfo.getNodeId(), nodeInfo.getPool(), nodeInfo.getLocation(), services); builder = preparePut() .setHeader("Content-Type", MEDIA_TYPE_JSON.toString()) .setBodySource(jsonBodyGenerator(announcementCodec, announcement)); } Request request = builder .setUri(URI.create("v1/announcement/" + nodeInfo.getNodeId())) .setHeader("User-Agent", nodeInfo.getNodeId()) .build(); return httpClient.executeAsync(request, new DiscoveryResponseHandler<Duration>("Announcement") { @Override public Duration handle(Request request, Response response) throws DiscoveryException { int statusCode = response.getStatusCode(); if (statusCode == 404 && servicesEmpty) { statusCode = 200; } if (!isSuccess(statusCode)) { throw new DiscoveryException(String.format("Announcement failed with status code %s: %s", statusCode, getBodyForError(response))); } return extractMaxAge(response); } }); }
HttpDiscoveryAnnouncementClient implements DiscoveryAnnouncementClient { @Override public ListenableFuture<Duration> announce(Set<ServiceAnnouncement> services) { requireNonNull(services, "services is null"); Builder builder; final boolean servicesEmpty = services.isEmpty(); if (servicesEmpty) { builder = prepareDelete(); } else { Announcement announcement = new Announcement(nodeInfo.getEnvironment(), nodeInfo.getNodeId(), nodeInfo.getPool(), nodeInfo.getLocation(), services); builder = preparePut() .setHeader("Content-Type", MEDIA_TYPE_JSON.toString()) .setBodySource(jsonBodyGenerator(announcementCodec, announcement)); } Request request = builder .setUri(URI.create("v1/announcement/" + nodeInfo.getNodeId())) .setHeader("User-Agent", nodeInfo.getNodeId()) .build(); return httpClient.executeAsync(request, new DiscoveryResponseHandler<Duration>("Announcement") { @Override public Duration handle(Request request, Response response) throws DiscoveryException { int statusCode = response.getStatusCode(); if (statusCode == 404 && servicesEmpty) { statusCode = 200; } if (!isSuccess(statusCode)) { throw new DiscoveryException(String.format("Announcement failed with status code %s: %s", statusCode, getBodyForError(response))); } return extractMaxAge(response); } }); } }
HttpDiscoveryAnnouncementClient implements DiscoveryAnnouncementClient { @Override public ListenableFuture<Duration> announce(Set<ServiceAnnouncement> services) { requireNonNull(services, "services is null"); Builder builder; final boolean servicesEmpty = services.isEmpty(); if (servicesEmpty) { builder = prepareDelete(); } else { Announcement announcement = new Announcement(nodeInfo.getEnvironment(), nodeInfo.getNodeId(), nodeInfo.getPool(), nodeInfo.getLocation(), services); builder = preparePut() .setHeader("Content-Type", MEDIA_TYPE_JSON.toString()) .setBodySource(jsonBodyGenerator(announcementCodec, announcement)); } Request request = builder .setUri(URI.create("v1/announcement/" + nodeInfo.getNodeId())) .setHeader("User-Agent", nodeInfo.getNodeId()) .build(); return httpClient.executeAsync(request, new DiscoveryResponseHandler<Duration>("Announcement") { @Override public Duration handle(Request request, Response response) throws DiscoveryException { int statusCode = response.getStatusCode(); if (statusCode == 404 && servicesEmpty) { statusCode = 200; } if (!isSuccess(statusCode)) { throw new DiscoveryException(String.format("Announcement failed with status code %s: %s", statusCode, getBodyForError(response))); } return extractMaxAge(response); } }); } @Inject  HttpDiscoveryAnnouncementClient(NodeInfo nodeInfo,
            JsonCodec<Announcement> announcementCodec,
            @ForDiscoveryClient HttpClient httpClient); }
HttpDiscoveryAnnouncementClient implements DiscoveryAnnouncementClient { @Override public ListenableFuture<Duration> announce(Set<ServiceAnnouncement> services) { requireNonNull(services, "services is null"); Builder builder; final boolean servicesEmpty = services.isEmpty(); if (servicesEmpty) { builder = prepareDelete(); } else { Announcement announcement = new Announcement(nodeInfo.getEnvironment(), nodeInfo.getNodeId(), nodeInfo.getPool(), nodeInfo.getLocation(), services); builder = preparePut() .setHeader("Content-Type", MEDIA_TYPE_JSON.toString()) .setBodySource(jsonBodyGenerator(announcementCodec, announcement)); } Request request = builder .setUri(URI.create("v1/announcement/" + nodeInfo.getNodeId())) .setHeader("User-Agent", nodeInfo.getNodeId()) .build(); return httpClient.executeAsync(request, new DiscoveryResponseHandler<Duration>("Announcement") { @Override public Duration handle(Request request, Response response) throws DiscoveryException { int statusCode = response.getStatusCode(); if (statusCode == 404 && servicesEmpty) { statusCode = 200; } if (!isSuccess(statusCode)) { throw new DiscoveryException(String.format("Announcement failed with status code %s: %s", statusCode, getBodyForError(response))); } return extractMaxAge(response); } }); } @Inject  HttpDiscoveryAnnouncementClient(NodeInfo nodeInfo,
            JsonCodec<Announcement> announcementCodec,
            @ForDiscoveryClient HttpClient httpClient); @Override ListenableFuture<Duration> announce(Set<ServiceAnnouncement> services); @Override ListenableFuture<Void> unannounce(); }
HttpDiscoveryAnnouncementClient implements DiscoveryAnnouncementClient { @Override public ListenableFuture<Duration> announce(Set<ServiceAnnouncement> services) { requireNonNull(services, "services is null"); Builder builder; final boolean servicesEmpty = services.isEmpty(); if (servicesEmpty) { builder = prepareDelete(); } else { Announcement announcement = new Announcement(nodeInfo.getEnvironment(), nodeInfo.getNodeId(), nodeInfo.getPool(), nodeInfo.getLocation(), services); builder = preparePut() .setHeader("Content-Type", MEDIA_TYPE_JSON.toString()) .setBodySource(jsonBodyGenerator(announcementCodec, announcement)); } Request request = builder .setUri(URI.create("v1/announcement/" + nodeInfo.getNodeId())) .setHeader("User-Agent", nodeInfo.getNodeId()) .build(); return httpClient.executeAsync(request, new DiscoveryResponseHandler<Duration>("Announcement") { @Override public Duration handle(Request request, Response response) throws DiscoveryException { int statusCode = response.getStatusCode(); if (statusCode == 404 && servicesEmpty) { statusCode = 200; } if (!isSuccess(statusCode)) { throw new DiscoveryException(String.format("Announcement failed with status code %s: %s", statusCode, getBodyForError(response))); } return extractMaxAge(response); } }); } @Inject  HttpDiscoveryAnnouncementClient(NodeInfo nodeInfo,
            JsonCodec<Announcement> announcementCodec,
            @ForDiscoveryClient HttpClient httpClient); @Override ListenableFuture<Duration> announce(Set<ServiceAnnouncement> services); @Override ListenableFuture<Void> unannounce();  }