Hello, my friends. I’m a small fat brother.

One of my classmates asked me if I was here today. What did I say?

Send me a screenshot, I have a look! Oh, it was to help with a scheduled task, or dynamic.

He said there are two options, one is to use DelayQueue and the other is to use message queues.

He said, fat brother can you teach me some moves mixed yuan method, help me complete this demand.

I said yes!

I told him you couldn’t use either, and he didn’t believe me.

I said well, write DelayQueue and let’s see, he can’t write it.

He said you probably wouldn’t, and I said I wouldn’t.

This is JUC, the traditional bottom development is to talk about the foundation, must be integrated, I will only switch.

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

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

As soon as I said it, he made a face.

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. The event is not triggered on time. * We can calculate the time interval required to execute as the key failure time, so that we can ensure that the point of execution logic. * /
public class RedisJobEventMessageListener extends KeyExpirationEventMessageListener {

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


    @Override
    protected void doHandleMessage(Message message) {
        String key = message.toString();
        // This is the expired key. After the expiration, that is, after the event is triggered, the corresponding value cannot be retrieved.
        // The business logic is implemented here. If the server cluster is used, the distributed lock is used for preemption.
        System.out.println("key = " + key);
        System.out.println("end = "+ LocalDateTime.now()); }}Copy the code

Configuration of a listener:

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


/** * Redis timed task listener is registered as Bean. **@param redisMessageListenerContainer the redis message listener container
 * @return the redis event message listener
 */
@Bean
public RedisJobEventMessageListener redisEventMessageListener(RedisMessageListenerContainer redisMessageListenerContainer){
    return new RedisJobEventMessageListener(redisMessageListenerContainer);
}
Copy the code

An execution test:

@Test
public void redisJobTest(a) {
    // Call the redisTemplate object to set a key that will expire 10 seconds later. If the key expires 10 seconds later, the event will print the result
    redisTemplate.boundValueOps("job").set("10s".10, TimeUnit.SECONDS);
    System.out.println("begin = " + LocalDateTime.now());
    try {
        // Tests need to sleep to see results
        Thread.sleep(20000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    // --------- Test result ---------
    / / the begin = 2020-11-18 T00:19:09. 272
    // key = job
    / / end = 2020-11-18 T00: used. 369
}
Copy the code

I sent him the code. I decided to relax and touch the fish.

I wanted him to pay attention to me, and I thought he would.

I was careless not to say.

And then ten minutes later he tells me he’s done it, and he’s not following me.

I said classmate you don’t speak rules, you don’t understand.

He said sorry, I don’t know the rules!

I said, young man,

Break the rules,

Come on,

White fuck!

I’ve been a small yard farmer for five years.

This good? This is not good.

I urge the student,

Rattail juice.

Reflect.

Pay attention.

Give it a thumbs up.

Good comment.

Peace is important, discipline is important,

Don’t be a hooker.

A lot of attention: code farmers small fat brother.

Thank you!

Personal blog: https://felord.cn