Skip to content

Commit

Permalink
RSWW-134 set up rabbitmq communication for hotels
Browse files Browse the repository at this point in the history
  • Loading branch information
Danzigerrr committed Jun 3, 2024
1 parent 5b85468 commit a23ac73
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

import cloud.project.datagenerator.hotels.domain.Hotel;
import cloud.project.datagenerator.hotels.domain.Room;
import cloud.project.datagenerator.hotels.rabbitmq.QueuesConfig;
import cloud.project.datagenerator.hotels.rabbitmq.json.JsonConverter;
import cloud.project.datagenerator.hotels.rabbitmq.requests.RoomUpdateRequest;
import cloud.project.datagenerator.hotels.repositories.HotelRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

Expand All @@ -14,9 +18,15 @@
@Component
@RequiredArgsConstructor
public class HotelsDataGenerator {
enum DataUpdateType {
CREATE,
UPDATE,
DELETE
}

private final HotelRepository hotelRepository;
private final Random random = new Random();
private final RabbitTemplate rabbitTemplate;

@Scheduled(fixedDelay = 5000, initialDelay = 5000)
public void generateRandomHotelData() {
Expand All @@ -41,7 +51,7 @@ private void createNewRoom() {
Hotel randomHotel = getRandomHotel();
if (randomHotel == null) return;

Room newRoom = Room.builder()
Room room = Room.builder()
.id(UUID.randomUUID())
.hotel(randomHotel)
.name("NewRoom " + random.nextInt(100))
Expand All @@ -50,37 +60,37 @@ private void createNewRoom() {
.description("This is a new room generated by Data Generator module")
.build();

System.out.println("Creating new room: " + newRoom);

// Todo: send the room using rabbitmq
System.out.println("Creating new room: " + room);

updateHotelDataInHotelModules(DataUpdateType.CREATE, room);
}

private void updateRandomRoom() {
Hotel randomHotel = getRandomHotel();
if (randomHotel == null) return;

Room randomRoom = randomHotel.getRooms().get(random.nextInt(randomHotel.getRooms().size()));
int currentGuestCapacity = randomRoom.getGuestCapacity();
Room room = randomHotel.getRooms().get(random.nextInt(randomHotel.getRooms().size()));
int currentGuestCapacity = room.getGuestCapacity();
int newGuestCapacity = random.nextInt(1, currentGuestCapacity + 10);

System.out.println("Updating room " + randomRoom.getName() + " in hotel " + randomHotel.getName() +
System.out.println("Updating room " + room.getName() + " in hotel " + randomHotel.getName() +
" - old guest capacity: " + currentGuestCapacity + " new guest capacity: " + newGuestCapacity);

// Todo: send the room using rabbitmq
randomRoom.setGuestCapacity(newGuestCapacity);
room.setGuestCapacity(newGuestCapacity);

updateHotelDataInHotelModules(DataUpdateType.UPDATE, room);

}

private void deleteRandomRoom() {
Hotel randomHotel = getRandomHotel();
if (randomHotel == null) return;

Room randomRoom = randomHotel.getRooms().get(random.nextInt(randomHotel.getRooms().size()));
Room room = randomHotel.getRooms().get(random.nextInt(randomHotel.getRooms().size()));

System.out.println("Deleting room: " + randomRoom);
// Todo: send the room using rabbitmq
System.out.println("Deleting room: " + room);

updateHotelDataInHotelModules(DataUpdateType.DELETE, room);
}

private Hotel getRandomHotel() {
Expand All @@ -106,4 +116,23 @@ private Hotel getRandomHotel() {
return randomHotel;
}

public void updateHotelDataInHotelModules(DataUpdateType updateType, Room room) {
RoomUpdateRequest roomUpdateRequest = RoomUpdateRequest.builder()
.updateType(String.valueOf(updateType))
.id(room.getId())
.hotelId(room.getHotel().getId())
.name(room.getName())
.guestCapacity(room.getGuestCapacity())
.pricePerAdult(room.getPricePerAdult())
.description(room.getDescription())
.build();


String roomUpdateRequestJson = JsonConverter.convert(roomUpdateRequest);

System.out.println("roomUpdateRequestJson: " + roomUpdateRequestJson);

rabbitTemplate.convertAndSend(QueuesConfig.EXCHANGE_HOTEL_FANOUT_UPDATE_DATA, "", roomUpdateRequestJson);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cloud.project.datagenerator.hotels.rabbitmq;

import org.springframework.amqp.core.FanoutExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class QueuesConfig {
public static final String EXCHANGE_HOTEL_FANOUT_UPDATE_DATA = "data.generate.hotels.exchange";

@Bean
public FanoutExchange updateHotelDataFanoutExchange() {
return new FanoutExchange(EXCHANGE_HOTEL_FANOUT_UPDATE_DATA);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cloud.project.datagenerator.hotels.rabbitmq.json;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

public class JsonConverter {
public static String convert(Object obj) {
ObjectMapper mapper = new ObjectMapper().registerModule(new JavaTimeModule());
String json;
try {
json = mapper.writeValueAsString(obj);

} catch (JsonProcessingException e) {
e.printStackTrace();
throw new RuntimeException("Json failed to convert to JSON.");
}
return json;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cloud.project.datagenerator.hotels.rabbitmq.requests;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.UUID;


@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class RoomUpdateRequest {

private String updateType;

private UUID id;

private UUID hotelId;

private String name;

private int guestCapacity;

private float pricePerAdult;

private String description;

// List of room reservations for each room is empty
}

0 comments on commit a23ac73

Please sign in to comment.