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。