Databases Reference
In-Depth Information
Delete
We can send a notification about the deleted contacts by making a small change
in the deleteById() method of the RedisContactService class. After the
contact information is deleted, we will call the convertAndSend() method of the
RedisTemplate class, which sends the notification message. The source code of the
modified deleteById() method is given as follows:
@Override
public Contact deleteById(Long id) throws NotFoundException {
Contact deleted = findById(id);
String key = buildKey(id);
redisTemplate.opsForSet().remove("contacts", deleted);
redisTemplate.opsForValue().set(key, null);
redisTemplate.convertAndSend("removedContacts", deleted);
return deleted;
}
Verifying the wanted behaviour
We have now implemented our message listeners and modified our application to
send a notification message every time the contact information is changed. Our next
step is to verify that our implementation is working as expected.
We can confirm this by making changes to the contact information and making sure
that log lines written by our message listeners appear in the application's log. The
loglines that are written when a new contact is added are given as follows:
DEBUG - ContactMessageListener - Received message: {"id":9,"add
ress":{"country":"","streetAddress":"","postCode":"","postOffice":"
","state":""},"emailAddress":"","firstName":"Foo","lastName":"Bar"-
,"phoneNumber":""} on channel: newContacts
DEBUG - ContactPOJOMessageListener - Received contact: com.packtpub.
springdata.redis.model.Contact@543d8ee8[id=9,address=com.packtpub.
springdata.redis.model.Address@15714c8d[country=,streetAddress=,postCo
de=,postOffice=,state=],emailAddress=,firstName=Foo,lastName=Bar,phone
Number=] on channel: null
Note that the channel information passed to a POJO message
handler is always null . This is a known bug of Spring
Data Redis. More information about this is available at
https://jira.springsource.org/browse/DATAREDIS-98 .
 
Search WWH ::




Custom Search