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

正文完
 0
49ziy
版权声明:本站原创文章,由 49ziy 于2019-01-15发表,共计3151字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
验证码