Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions shenyu-client-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
</dependency>

<dependency>
<groupId>com.github.oshi</groupId>
<artifactId>oshi-core</artifactId>
<version>6.10.0</version>
</dependency>

</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.shenyu.client.core.shutdown.ShutdownHookManager;
import org.apache.shenyu.client.core.type.DataType;
import org.apache.shenyu.client.core.utils.ShenyuThreadFactory;
import org.apache.shenyu.client.core.utils.SystemInfoUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
Expand All @@ -43,26 +44,28 @@
* The type Shenyu client uri executor subscriber.
*/
public class ShenyuClientURIExecutorSubscriber implements ExecutorTypeSubscriber<URIRegisterDTO> {

private static final Logger LOG = LoggerFactory.getLogger(ShenyuClientURIExecutorSubscriber.class);


private static final Logger LOG = LoggerFactory.getLogger(
ShenyuClientURIExecutorSubscriber.class);

private static final List<URIRegisterDTO> URIS = Lists.newArrayList();

private final ShenyuClientRegisterRepository shenyuClientRegisterRepository;

private final ScheduledThreadPoolExecutor executor;

/**
* Instantiates a new Shenyu client uri executor subscriber.
*
* @param shenyuClientRegisterRepository the shenyu client register repository
*/
public ShenyuClientURIExecutorSubscriber(final ShenyuClientRegisterRepository shenyuClientRegisterRepository) {
public ShenyuClientURIExecutorSubscriber(
final ShenyuClientRegisterRepository shenyuClientRegisterRepository) {
this.shenyuClientRegisterRepository = shenyuClientRegisterRepository;
// executor for send heartbeat
ThreadFactory requestFactory = ShenyuThreadFactory.create("heartbeat-reporter", true);
executor = new ScheduledThreadPoolExecutor(1, requestFactory);

executor.scheduleAtFixedRate(() -> {
try {
URIS.forEach(this::sendHeartbeat);
Expand All @@ -71,12 +74,12 @@ public ShenyuClientURIExecutorSubscriber(final ShenyuClientRegisterRepository sh
}
}, 30, 30, TimeUnit.SECONDS);
}

@Override
public DataType getType() {
return DataType.URI;
}

@Override
public void executor(final Collection<URIRegisterDTO> dataList) {
for (URIRegisterDTO uriRegisterDTO : dataList) {
Expand Down Expand Up @@ -104,24 +107,25 @@ public void executor(final Collection<URIRegisterDTO> dataList) {
}
ShenyuClientShutdownHook.delayOtherHooks();
shenyuClientRegisterRepository.persistURI(uriRegisterDTO);

URIS.add(uriRegisterDTO);

ShutdownHookManager.get().addShutdownHook(new Thread(() -> {
final URIRegisterDTO offlineDTO = new URIRegisterDTO();
BeanUtils.copyProperties(uriRegisterDTO, offlineDTO);
offlineDTO.setEventType(EventType.OFFLINE);
shenyuClientRegisterRepository.offline(offlineDTO);

// shutdown heartbeat executor
if (!executor.isTerminated()) {
executor.shutdown();
}
}), 2);
}
}

private void sendHeartbeat(final URIRegisterDTO uriRegisterDTO) {
uriRegisterDTO.setInstanceInfo(SystemInfoUtils.getSystemInfo());
shenyuClientRegisterRepository.sendHeartbeat(uriRegisterDTO);
}
}
Loading
Loading