Asynchronous data transfer using Redis

Redis offers asynchronous data transfer between an application using a publisher/subscriber model, which enables one application to publish to a channel and another application to subscribe to that channel and perform actions when an event is received. The publishers need not know about the subscribers, and vice versa in this model, which enables loose coupling and high scalability. 

In the case of these two microservices, the Taxi Microservice needs to know when a Taxi Booking is accepted in the Taxi Booking Microservice so that it can update the status of a Taxi. For this reason, the Taxi Booking microservice will publish a Taxi Booking Accepted Event, and the Taxi Microservice will subscribe to it.

The following code snippet in the TaxiBookingService.accept function is responsible for publishing that event to the Redis Pub/Sub Channel:

try {
redisTemplate.convertAndSend(RedisConfig.ACCEPTED_EVENT_CHANNEL, objectMapper.writeValueAsString(acceptedEventDTO));
} catch (JsonProcessingException e) {
LOGGER.error("Error while sending message to Channel {}", RedisConfig.ACCEPTED_EVENT_CHANNEL, e);
}

The following bean configuration snippet in the Taxi Microservice in SpringBoot2TaxiServiceApplication is required to set up the Subscriber as a listener:

@Bean
public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, TaxiBookingAcceptedEventMessageListener taxiBookingAcceptedEventMessageListener) {
RedisMessageListenerContainer container = new
RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.addMessageListener(taxiBookingAcceptedEventMessageListener, new
PatternTopic(RedisConfig.ACCEPTED_EVENT_CHANNEL));
return container;
}

The following Listener implementation is required to take action when a Taxi Booking Accepted Event is sent from the Taxi Booking Microservice to the Taxi Microservice:

@Component
public class TaxiBookingAcceptedEventMessageListener implements MessageListener {

private static final Logger LOGGER =
LoggerFactory.getLogger(TaxiBookingAcceptedEventMessageListener.class);

private final TaxiService taxiService;
private final ObjectMapper objectMapper = new ObjectMapper();

public TaxiBookingAcceptedEventMessageListener(TaxiService taxiService) {
this.taxiService = taxiService;
}

@Override
public void onMessage(Message message, @Nullable byte[] bytes) {
try {
TaxiBookingAcceptedEventDTO taxiBookingAcceptedEventDTO =
objectMapper.readValue(new String(message.getBody()),
TaxiBookingAcceptedEventDTO.class);
LOGGER.info("Accepted Event {}", taxiBookingAcceptedEventDTO);

taxiService.updateTaxiStatus(taxiBookingAcceptedEventDTO.getTaxiId(),
TaxiStatus.OCCUPIED);
} catch (IOException e) {
LOGGER.error("Error while updating taxi status", e);
}
}
}

The preceding listener's onMessage method will be activated whenever an event is received and will update the status of the Taxi.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.134.90.44