private String serviceId(Registration registration) { return registration.getName() + ":" + registration.getAddress() + ":" + registration.getPort(); } }
/** * Registers the client for native notifications with the specified tags * @param pnsHandle PNS specific identifier * @param tags Tags to use in the registration * @return The created registration * @throws Exception */ public Registration register(String pnsHandle, String... tags) throws Exception { if (isNullOrWhiteSpace(pnsHandle)) { throw new IllegalArgumentException("pnsHandle"); } Registration registration = PnsSpecificRegistrationFactory.getInstance().createNativeRegistration(mNotificationHubPath); registration.setPNSHandle(pnsHandle); registration.setName(Registration.DEFAULT_REGISTRATION_NAME); registration.addTags(tags); return registerInternal(registration); }
public synchronized void unregisterAll(String sourceKey, String name) { boolean removed = this.registrations.removeIf(e -> e.getSourceKey().equals(sourceKey) && e.getName().equals(name)); if (removed) { fireListeners(); } }
@Override public List<Server> getUpdatedListOfServers() { String tag = ( this.isSecure ? "https" : "http" ); return TopologyManager.INSTANCE.registrationsForService(this.appName, tag) .stream() .map( reg-> new Server( reg.getAddress(), reg.getPort() )) .collect(Collectors.toList()); } }
public void advertise(Registration registration) { if (this.advertisements.contains(registration)) { return; } AgentClient client = this.agentClientInjector.getValue(); com.orbitz.consul.model.agent.Registration consulReg = ImmutableRegistration.builder() .address(registration.getAddress()) .port(registration.getPort()) .id(serviceId(registration)) .name(registration.getName()) .addTags(registration.getTags().toArray(new String[]{})) .check(com.orbitz.consul.model.agent.Registration.RegCheck.ttl(3L)) .build(); client.register(consulReg); this.advertisements.add(registration); }
@Override public void notify(Map<HostAndPort, ServiceHealth> newValues) { Set<Registration> previousEntries = topologyManager.registrationsForService(this.name); Set<Registration> newEntries = newValues.values().stream() .map(e -> new Registration("consul", this.name, e.getService().getAddress(), e.getService().getPort()) .addTags(e.getService().getTags()) ) .collect(Collectors.toSet()); previousEntries.stream() .filter(e -> !newEntries.contains(e)) .forEach(e -> { this.topologyManager.unregister(e); }); newEntries.stream() .filter(e -> !previousEntries.contains(e)) .forEach(e -> { this.topologyManager.register(e); }); } }
@Override public synchronized Map<String, List<Entry>> asMap() { Map<String, List<Entry>> map = new HashMap<>(); for (Registration registration : this.registrations) { List<Entry> list = map.get(registration.getName()); if (list == null) { list = new ArrayList<>(); map.put(registration.getName(), list); } list.add(registration); } return map; }
@Override public void advertise(String name, SocketBinding binding, String...tags) { Registration registration = new Registration("consul", name, binding.getAddress().getHostAddress(), binding.getAbsolutePort(), tags ); this.advertiser.advertise(registration); }
public synchronized Set<Registration> registrationsForSourceKey(String sourceKey) { Set<Registration> result = Collections.unmodifiableSet( this.registrations.stream() .filter(e -> e.getSourceKey().equals(sourceKey)) .collect(Collectors.toSet()) ); return result; }
public synchronized void unregister(Registration registration) { boolean removed = this.registrations.removeIf(e -> e.equals(registration)); if (removed) { fireListeners(); } }
@Override public List<Server> getInitialListOfServers() { String tag = ( this.isSecure ? "https" : "http" ); return TopologyManager.INSTANCE.registrationsForService(this.appName, tag) .stream() .map( reg-> new Server( reg.getAddress(), reg.getPort() )) .collect(Collectors.toList()); }
private Set<Registration> registrationsForService(IService service) { Set<Registration> newEntries = new HashSet<>(); // Only expose the service's default port and anything running on the https port service.getPorts() .stream() .filter(servicePort -> servicePort.getPort() == service.getPort() || servicePort.getPort() == DEFAULT_HTTPS_PORT) .forEach(servicePort -> { Registration registration = new Registration(TOPOLOGY_SOURCE_KEY, service.getName(), service.getName(), servicePort.getPort()); if (servicePort.getPort() == DEFAULT_HTTPS_PORT) { registration.addTags(Collections.singletonList("https")); } else if (servicePort.getPort() == service.getPort()) { registration.addTags(Collections.singletonList("http")); } newEntries.add(registration); }); return newEntries; }
@Override public synchronized Map<String, List<Entry>> asMap() { Map<String, List<Entry>> map = new HashMap<>(); for (Registration registration : this.registrations) { List<Entry> list = map.get(registration.getName()); if (list == null) { list = new ArrayList<>(); map.put(registration.getName(), list); } list.add(registration); } return map; }
public synchronized void advertise(String name, SocketBinding binding, String... tags) throws Exception { Registration registration = new Registration(sourceKey(this.node), name, binding.getAddress().getHostAddress(), binding.getAbsolutePort(), tags); this.registrations.put(name + ":" + binding.getName(), registration); advertise(registration); }
public synchronized Set<Registration> registrationsForSourceKey(String sourceKey) { Set<Registration> result = Collections.unmodifiableSet( this.registrations.stream() .filter(e -> e.getSourceKey().equals(sourceKey)) .collect(Collectors.toSet()) ); return result; }
public synchronized void unregister(Registration registration) { boolean removed = this.registrations.removeIf(e -> e.equals(registration)); if (removed) { fireListeners(); } }
@Override public List<Server> getInitialListOfServers() { String tag = (this.isSecure ? "https" : "http"); return TopologyManager.INSTANCE.registrationsForService(this.appName, tag) .stream() .map(reg -> new Server(reg.getAddress(), reg.getPort())) .collect(Collectors.toList()); }
public void unadvertise(String name, String address, int port) { this.advertisements.removeIf( e->e.getName().equals( name) && e.getAddress().equals( address ) && e.getPort() == port ); }