-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: rabbitmq as a central bus transport + protobuf as payload
- Loading branch information
Showing
11 changed files
with
195 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,4 +25,5 @@ nbdist/ | |
.nb-gradle/ | ||
|
||
.DS_Store | ||
.env | ||
.env | ||
*/gen/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
door-service/src/main/java/org/crazycoder/door/service/DoorConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package org.crazycoder.door.service; | ||
|
||
import org.springframework.amqp.core.Binding; | ||
import org.springframework.amqp.core.BindingBuilder; | ||
import org.springframework.amqp.core.Queue; | ||
import org.springframework.amqp.core.TopicExchange; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class DoorConfig { | ||
|
||
@Value("${door.service.queue}") | ||
private String doorServiceQueue; | ||
@Value("${door.service.topic}") | ||
private String doorServiceTopic; | ||
|
||
@Bean | ||
Queue queue() { | ||
return new Queue(doorServiceQueue, false); | ||
} | ||
|
||
@Bean | ||
TopicExchange exchange() { | ||
return new TopicExchange(doorServiceTopic); | ||
} | ||
|
||
@Bean | ||
Binding binding(Queue queue, TopicExchange exchange) { | ||
return BindingBuilder.bind(queue).to(exchange).with(doorServiceQueue); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
door-service/src/main/java/org/crazycoder/door/service/DoorService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package org.crazycoder.door.service; | ||
|
||
import org.crazycoder.door.service.domain.DoorStatus; | ||
import org.crazycoder.door.service.domain.Heartbeat; | ||
import org.crazycoder.door.service.protobuf.DoorSensorData; | ||
import org.crazycoder.door.service.protobuf.Status; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.amqp.rabbit.core.RabbitTemplate; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
public class DoorService { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(DoorService.class); | ||
|
||
@Autowired | ||
private HeartbeatRepository heartbeatRepository; | ||
@Autowired | ||
private DoorStatusRepository doorStatusRepository; | ||
@Autowired | ||
private RabbitTemplate rabbitTemplate; | ||
@Value("${door.service.queue}") | ||
private String doorServiceQueue; | ||
@Value("${door.service.topic}") | ||
private String doorServiceTopic; | ||
|
||
public void saveDoorStatus(DoorStatus doorStatus) { | ||
LOG.info("Door status {}", doorStatus); | ||
doorStatusRepository.save(doorStatus); | ||
DoorSensorData doorSensorData = DoorSensorData.newBuilder() | ||
.setDeviceId(doorStatus.getDeviceId()) | ||
.setTimestamp(doorStatus.getTimestamp()) | ||
.setStatus(Status.valueOf(doorStatus.getStatus().name())) | ||
.build(); | ||
rabbitTemplate.convertAndSend(doorServiceTopic, doorServiceQueue, doorSensorData); | ||
} | ||
|
||
public void saveHeartbeat(Heartbeat heartbeat) { | ||
LOG.info("Door status heartbeat {}", heartbeat); | ||
heartbeatRepository.save(heartbeat); | ||
} | ||
|
||
public List<Heartbeat> getHeartbeat(String deviceId, int limit) { | ||
return heartbeatRepository.findByDeviceIdOrderByTimestampDesc(deviceId).stream() | ||
.limit(limit) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
public List<Heartbeat> getHeartbeats(int limit) { | ||
return heartbeatRepository.findAll().stream() | ||
.limit(limit) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
public List<DoorStatus> getDoorStatus(String deviceId, int limit) { | ||
return doorStatusRepository.findByDeviceIdOrderByTimestampDesc(deviceId).stream() | ||
.limit(limit) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
public List<DoorStatus> getDoorStatuses(int limit) { | ||
return doorStatusRepository.findAllByOrderByTimestampDesc().stream() | ||
.limit(limit) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
syntax = "proto3"; | ||
|
||
option java_package = "org.crazycoder.door.service.protobuf"; | ||
option java_multiple_files = true; | ||
|
||
message DoorSensorData { | ||
string deviceId = 1; | ||
int64 timestamp = 2; | ||
Status status = 3; | ||
} | ||
|
||
enum Status { | ||
OPENED = 0; | ||
CLOSED = 1; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
spring: | ||
jackson: | ||
serialization: | ||
WRITE_DATES_AS_TIMESTAMPS: false | ||
|
||
door.service: | ||
queue: "door.service.state" | ||
topic: "door.service" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters