When I recently made a project, I wrote an event push scenario. Before the implementation of the job is to query the database to see if there is no latest news. This way is very not elegant, anyway I can not tolerate, because envy itself relies on redis, just Redis also has the function of message queue, simply use Redis to achieve this message push.
Before referring to the online example, found that all from a place, spring official example, some of the "unofficial example" of the number one, or the same copy of the official example, even if they do not understand the code, intact paste up, this way to mention a majority of the domestic blog is mutual "reference", Some of them do not understand the other person's content Ctrl + C V came over. Here I do not post official code, click here to send an official example. By the way. Countdownlatch object is actually irrelevant to this content, I would like to mention that the specific function I do not say, the official example is to let the program sent to the Redis channel inside the message is read by the program before the end of the program introduced by the class.
Not much to say directly on the code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
/**redis Message Processor * /@Componentpublic class messagereceiver { /** How to receive messages * /public void receiveMessage(String message) { System. Out. println(message); }}@Configurationpublic class redisconfig { @BeanRedismessagelistenercontainer Container(redisconnectionfactory connectionfactory, messagelisteneradapter listeneradapter) { Redismessagelistenercontainer Container = new redismessagelistenercontainer(); container. Setconnectionfactory(connectionfactory); //Subscribe to a channel called chatcontainer. Addmessagelistener(listeneradapter, new patterntopic("chat")); //This container can add multiple MessageListenerreturn container; }@Beanmessagelisteneradapter listeneradapter(messagereceiver receiver) { //This place is for Messagelisteneradapter to pass in a message to accept the processor, using the method of reflection called "ReceiveMessage"///There are several overloaded methods, the method called by default on this side of the handlemessage can be the source of their own to seereturn new messagelisteneradapter(receiver, "ReceiveMessage"); }/**redis Read the content of the template this is not related to this project, you can not configure * /@Beanstringredistemplate Template(redisconnectionfactory connectionfactory) { return new stringredistemplate(connectionfactory); }} |
code slices from codesRedisconfig.java
Above I wrote two classes together, the first class Messagereceiver comment on the note, there is only one way, is to receive the message after processing, this demonstration I will simply print him on the console.
The second class is a few configurations, and I think I should be able to see them in the comments.
OK next is the start program to see the effect
Ok I'm using the command publish to push Helloword to the chat channel (mention that the integer 1 represents the current subscriber)
Perfect in my program also received this message, and printed on the console.
"Go" redis Message Queuing Publish subscription mode spring boot implementation