Young people don't talk about martial arts and come to white piao, an old comrade like me

Hello friends, I'm the code farmer.

Today a classmate asked me if I was there, what did I say?

Send me a screenshot and I'll take a look! Oh, it turns out that it is to help with a timed task, or it is dynamic.

He said two options, one is to use DelayQueue, the other is to use message queue.

He said, "Fat brother, can you teach me some Hunyuan exercises to help me fulfill this requirement."

I said yes!

I told you that neither of them is easy to use, but he wasn't convinced.

I said that you can write a DelayQueue to see, he can't write it.

He said you probably wouldn't, and I said I really wouldn't.

This is JUC. The traditional bottom-level development is based on the basics and must be integrated. I will only adjust the package.

This kind of timing task is simpler for me to use Redis.

He asked me to write a DEMO and I said yes!

As soon as I said it, he snapped.

Soon, I opened IDEA and a DEMO came out.

A Key invalidation listener that overrides Redis:

 *  When the key in redis expires, an event is triggered, and the event will not be triggered on time, which is suitable for triggering requirements that are not particularly time-sensitive.
 *  We can calculate the time interval that needs to be executed as the key expiration time, so that we can ensure that the logic is executed at the point.
public class RedisJobEventMessageListener extends KeyExpirationEventMessageListener {

     * Instantiates a new Redis event message listener.
     * @param listenerContainer the listener container
    public RedisEventMessageListener(RedisMessageListenerContainer listenerContainer) {

    protected void doHandleMessage(Message message) {
        String key = message.toString();
        // This is the expired key. After the expiration, the corresponding value cannot be obtained after the event is triggered.
        // The business logic is implemented here. If it is a server cluster, distributed locks need to be used for preemptive execution.
        System.out.println("key = " + key);
        System.out.println("end = " +;

A listener configuration:

 * Redis Message listener container.
 * @param redisConnectionFactory the redis connection factory
 * @return the redis message listener container
public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory redisConnectionFactory) {
    RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
    return redisMessageListenerContainer;

 * Redis The scheduled task listener is registered as a Bean.
 * @param redisMessageListenerContainer the redis message listener container
 * @return the redis event message listener
public RedisJobEventMessageListener redisEventMessageListener(RedisMessageListenerContainer redisMessageListenerContainer){
    return new RedisJobEventMessageListener(redisMessageListenerContainer);

An execution test:

public void redisJobTest() {
    // Call the redisTemplate object to set a key that expires after 10s. No accident, after the key expires after 10s, an event will be triggered to print the result.
    redisTemplate.boundValueOps("job").set("10s",10, TimeUnit.SECONDS);
    System.out.println("begin = " +;
    try {
        // Test needs to sleep to see results
    } catch (InterruptedException e) {
    // ---------Test Results---------
    // begin = 2020-11-18T00:19:09.272
    // key = job
    // end = 2020-11-18T00:19:19.369

After writing it, it is natural to stop at the traditional programming ideas. I send the code to him. I plan to relax and feel the fish.

I originally wanted him to follow me, and I thought he would take the initiative to follow.

I was careless and didn't say it.

Then ten minutes later he told me he had it done and didn't follow me.

I said classmates, you don't follow the rules, you don't understand.

He was busy saying sorry, I don't know the rules!

I say young people,

no rules,



My small code farmer with five years of experience.

Is this okay? This is not good.

I advise this student,

Rat tail juice.


Pay attention.

Give a good thumbs up.

Comment well.

To put harmony as the most valuable, to obey the rules,

Don't be a prostitute all the time.

Pay more attention: Code Farmer Little Fat Brother.

Thank you classmates!

Pay attention to the public number: Felordcn for more information

Personal blog:

Tags: Java

Posted by jmicozzi on Sat, 07 May 2022 08:03:11 +0300