diff --git a/spring-cloud-cloudfoundry-discovery/src/main/java/org/springframework/cloud/cloudfoundry/discovery/CloudFoundryDiscoveryClientConfiguration.java b/spring-cloud-cloudfoundry-discovery/src/main/java/org/springframework/cloud/cloudfoundry/discovery/CloudFoundryDiscoveryClientConfiguration.java index 669673b..272f033 100644 --- a/spring-cloud-cloudfoundry-discovery/src/main/java/org/springframework/cloud/cloudfoundry/discovery/CloudFoundryDiscoveryClientConfiguration.java +++ b/spring-cloud-cloudfoundry-discovery/src/main/java/org/springframework/cloud/cloudfoundry/discovery/CloudFoundryDiscoveryClientConfiguration.java @@ -75,4 +75,9 @@ public class CloudFoundryDiscoveryClientConfiguration { return new CloudFoundryDiscoveryClient(cloudFoundryClient, environment); } + @Bean + public CloudFoundryHeartbeatSender cloudFoundryHeartbeatSender(CloudFoundryDiscoveryClient client) { + return new CloudFoundryHeartbeatSender(client); + } + } \ No newline at end of file diff --git a/spring-cloud-cloudfoundry-discovery/src/main/java/org/springframework/cloud/cloudfoundry/discovery/CloudFoundryDiscoveryProperties.java b/spring-cloud-cloudfoundry-discovery/src/main/java/org/springframework/cloud/cloudfoundry/discovery/CloudFoundryDiscoveryProperties.java index 69ae0c9..fc1936f 100644 --- a/spring-cloud-cloudfoundry-discovery/src/main/java/org/springframework/cloud/cloudfoundry/discovery/CloudFoundryDiscoveryProperties.java +++ b/spring-cloud-cloudfoundry-discovery/src/main/java/org/springframework/cloud/cloudfoundry/discovery/CloudFoundryDiscoveryProperties.java @@ -56,6 +56,12 @@ public class CloudFoundryDiscoveryProperties { */ private boolean enabled = true; + /** + * Frequency in milliseconds of poll for heart beat. The client will poll on this + * frequency and broadcast a list of service ids. + */ + private long heartbeatFrequency = 5000; + public boolean isEnabled() { return this.enabled; } @@ -103,4 +109,12 @@ public class CloudFoundryDiscoveryProperties { public void setSpace(String space) { this.space = space; } + + public long getHeartbeatFrequency() { + return this.heartbeatFrequency; + } + + public void setHeartbeatFrequency(long heartbeatFrequency) { + this.heartbeatFrequency = heartbeatFrequency; + } } \ No newline at end of file diff --git a/spring-cloud-cloudfoundry-discovery/src/main/java/org/springframework/cloud/cloudfoundry/discovery/CloudFoundryHeartbeatSender.java b/spring-cloud-cloudfoundry-discovery/src/main/java/org/springframework/cloud/cloudfoundry/discovery/CloudFoundryHeartbeatSender.java new file mode 100644 index 0000000..d229074 --- /dev/null +++ b/spring-cloud-cloudfoundry-discovery/src/main/java/org/springframework/cloud/cloudfoundry/discovery/CloudFoundryHeartbeatSender.java @@ -0,0 +1,57 @@ +/* + * Copyright 2015 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.cloudfoundry.discovery; + +import java.util.List; + +import org.springframework.cloud.client.discovery.event.HeartbeatEvent; +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.context.ApplicationEventPublisherAware; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +/** + * Publishes a {@link HeartbeatEvent} listing the available services as its state + * indicator. If consumers detect a change it means there is a change in the catalog. + * + * @author Dave Syer + * + */ +@Component +public class CloudFoundryHeartbeatSender implements ApplicationEventPublisherAware { + + private final CloudFoundryDiscoveryClient client; + private ApplicationEventPublisher publisher; + + public CloudFoundryHeartbeatSender(CloudFoundryDiscoveryClient client) { + this.client = client; + } + + @Scheduled(fixedDelayString = "${spring.cloud.cloudfoundry.discovery.heartbeatFrequency:5000}") + public void poll() { + if (this.publisher != null) { + List services = this.client.getServices(); + this.publisher.publishEvent(new HeartbeatEvent(this.client, services)); + } + } + + @Override + public void setApplicationEventPublisher(ApplicationEventPublisher publisher) { + this.publisher = publisher; + } + +}