Update TamagotchiService.java
This commit is contained in:
parent
15cb385d35
commit
551b9b30e8
1 changed files with 5 additions and 15 deletions
|
@ -8,12 +8,15 @@ import java.util.Set;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import com.sap.tamagotchi.model.Device;
|
import com.sap.tamagotchi.model.Device;
|
||||||
import com.sap.tamagotchi.publisher.PublisherService;
|
import com.sap.tamagotchi.publisher.PublisherService;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
|
@EnableScheduling
|
||||||
public class TamagotchiService {
|
public class TamagotchiService {
|
||||||
|
|
||||||
private static final long DEVICE_EVENT_PROCESSOR_SCHEDULE = 5_000;
|
private static final long DEVICE_EVENT_PROCESSOR_SCHEDULE = 5_000;
|
||||||
|
@ -28,7 +31,6 @@ public class TamagotchiService {
|
||||||
public TamagotchiService(PublisherService publisherService, Logger logger) {
|
public TamagotchiService(PublisherService publisherService, Logger logger) {
|
||||||
this.publisherService = publisherService;
|
this.publisherService = publisherService;
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
startDeviceEventProcessor();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Device getDevice(String deviceId) {
|
public Device getDevice(String deviceId) {
|
||||||
|
@ -49,8 +51,8 @@ public class TamagotchiService {
|
||||||
return device;
|
return device;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Scheduled(fixedDelay = DEVICE_EVENT_PROCESSOR_SCHEDULE)
|
||||||
private void processDeviceEvents() {
|
private void processDeviceEvents() {
|
||||||
|
|
||||||
deviceRegistry
|
deviceRegistry
|
||||||
.values()
|
.values()
|
||||||
.parallelStream()
|
.parallelStream()
|
||||||
|
@ -60,21 +62,9 @@ public class TamagotchiService {
|
||||||
try {
|
try {
|
||||||
publisherService.publish(message);
|
publisherService.publish(message);
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
|
logger.error("processing device events failed: {}", ex.getMessage());
|
||||||
throw new RuntimeException(ex);
|
throw new RuntimeException(ex);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void startDeviceEventProcessor() {
|
|
||||||
new Thread(() -> {
|
|
||||||
try {
|
|
||||||
while (true) {
|
|
||||||
processDeviceEvents();
|
|
||||||
Thread.sleep(DEVICE_EVENT_PROCESSOR_SCHEDULE);
|
|
||||||
}
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
logger.error("DeviceEventProcessor failed: {}", e.getMessage());
|
|
||||||
}
|
|
||||||
}).start();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue