jfinal使用redis消息队列

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);
        }
    }
}

小小demo。

评论区

JFinal

2019-01-04 11:14

代码简洁,功能实用,收藏 + 点赞

erzheyao

2019-04-09 19:21

这里jedisKit能否分享下哈哈

Mr.Gao

2019-05-28 14:37

源码方便分享下吗

吴小小

2019-07-19 15:53

源代码能分享看下吗,我这的不是很懂

flash866

2019-07-31 08:32

枫中孤叶

2019-09-24 09:10

有木有示例给下

aaaaaaa

2019-09-25 10:53

感觉走了弯路了,J2CacheKit.removeAll(SystemConfig.CACHE_API_FINDSHOP_INDEX); 直接会同步删除redis信息

redis用来做mq的可以参考下

flash866

2019-12-02 08:27

请问下这个监听支持多线程吗?如果是单线程的,那岂不是会造成处理效率下降吗?

nommpop

2019-12-18 16:43

代码能不能共享一下,谢谢了

热门分享

扫码入社