jfinal集成redis消息队列,波总亲自点赞demo

本人是用jfinal项目启蒙的,所以49vps也会特别关注jfinal项目,因此会转载一些优秀的demo或案例。

Jfinal使用redis消息队列监听

import com.jfinal.kit.StrKit;
import com.migo.base.SystemConfig;
import com.migo.utils.cache.plugin.j2cache.J2CacheKit;
import com.migo.utils.redis.JedisKit;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPubSub;
 
public class RedisCacheMsgListener extends JedisPubSub implements Runnable {
    @Override
    public void unsubscribe() {
        super.unsubscribe();
    }
 
    @Override
    public void unsubscribe(String... channels) {
        super.unsubscribe(channels);
    }
 
    @Override
    public void subscribe(String... channels) {
        super.subscribe(channels);
    }
 
    @Override
    public void psubscribe(String... patterns) {
        super.psubscribe(patterns);
    }
 
    @Override
    public void punsubscribe() {
        super.punsubscribe();
    }
 
    @Override
    public void punsubscribe(String... patterns) {
        super.punsubscribe(patterns);
    }
 
    @Override
    public void onMessage(String channel, String message) {
        System.out.println("channel:" + channel + "receives message :" + message);
        if (StrKit.isBlank(message)) {
            return;
        }
        if ("清理品牌库缓存".equals(message)) {
            J2CacheKit.removeAll(SystemConfig.CACHE_API_FINDSHOP_INDEX);
            J2CacheKit.removeAll(SystemConfig.CACHE_API_FINDSHOP_BRAND);
            J2CacheKit.removeAll(SystemConfig.CACHE_API_FINDSHOP_MARKET);
        } else if (SystemConfig.CACHE_API_HOMEPAGE.equals(message)) {
            J2CacheKit.removeAll(SystemConfig.CACHE_API_HOMEPAGE);
        } else if (SystemConfig.CACHE_API_HOMEPAGE_RAIDERS.equals(message)) {
            J2CacheKit.removeAll(SystemConfig.CACHE_API_HOMEPAGE_RAIDERS);
        } else if (SystemConfig.CACHE_API_HOMEPAGE_SHOP.equals(message)) {
            J2CacheKit.removeAll(SystemConfig.CACHE_API_HOMEPAGE_SHOP);
        } else if (SystemConfig.CACHE_API_HOMEPAGE_ACT.equals(message)) {
            J2CacheKit.removeAll(SystemConfig.CACHE_API_HOMEPAGE_ACT);
        }
    }
 
    @Override
    public void onPMessage(String pattern, String channel, String message) {
 
    }
 
    @Override
    public void onSubscribe(String channel, int subscribedChannels) {
        System.out.println("channel:" + channel + "is been subscribed:" + subscribedChannels);
    }
 
    @Override
    public void onPUnsubscribe(String pattern, int subscribedChannels) {
 
    }
 
    @Override
    public void onPSubscribe(String pattern, int subscribedChannels) {
 
    }
 
    @Override
    public void onUnsubscribe(String channel, int subscribedChannels) {
        System.out.println("channel:" + channel + "is been unsubscribed:" + subscribedChannels);
    }
 
    @Override
    public void run() {
        Jedis jedis = JedisKit.getJedis();
        jedis.select(SystemConfig.REDIS_DATEBASE_MSG);
        jedis.subscribe(new RedisCacheMsgListener(), SystemConfig.REDIS_CACHE_REMOVE_BRAND);
    }
}

JFinalConfig 的子类中调用 public void afterJFinalStart(){} ,开启额外线程

public void afterJFinalStart(){
	new Thread(new RedisCacheMsgListener()).start();
}

调用可以去通过注册的通道发送消息。

调用端:

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.kit.LogKit;
import com.migo.base.SystemConfig;
import com.migo.common.annotation.CacheRemove;
import com.migo.utils.tools.JedisKit;
 
 
public class RedisCacheRemoveInterceptor implements Interceptor {
 
    @Override
    public void intercept(Invocation inv) {
        inv.invoke();
        try {
            CacheRemove cacheRemove = inv.getMethod().getAnnotation(CacheRemove.class);
            if (null != cacheRemove) {
                JedisKit.sendMsgToRedis(SystemConfig.REDIS_CACHE_REMOVE_BRAND, cacheRemove.value());
                System.out.println("发送清理缓存消息:" + cacheRemove.value());
            }
        } catch (Exception e) {
            LogKit.error("发送清理缓存消息失败.", e);
        }
    }
}

原文转载地址jfinal俱乐部分享:https://www.jfinal.com/share/1358

1.本站所有资源收集于互联网,仅用于学习和研究,若用于违法,与本站无关,仅限学习交流请勿用于商业用途。 2.会员在本站下载的VIP素材后,只拥有使用权,著作权归原作者及49vps所有。 3.VIP素材,未经合法授权,会员不得以任何形式发布、传播、复制、转售该素材,否则一律封号处理。 4.如果素材损害你的权益,请联系客服删除。
49资源网 » jfinal集成redis消息队列,波总亲自点赞demo