• Redis的消息订阅/发布 Utils工具类


    package cn.cicoding.utils;
    
    import org.json.JSONException;
    import org.json.JSONObject;
    
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisPool;
    import redis.clients.jedis.JedisPoolConfig;
    import redis.clients.jedis.JedisPubSub;
    import redis.clients.jedis.Protocol;
    import redis.clients.jedis.exceptions.JedisConnectionException;
    
    class MQClient {
        public static final int MSG_REALTIME = 1;
        public static final int MSG_CACHED = 2;
        public static final int MSG_SERIALIZABLE = 3;
        public static final String NOTIFY_CHANNEL = "ClientNotify";
    
        private JedisPool pool;
        private boolean exit;
        private JedisPubSub pubsub;
    
        public MQClient(String ip, int port, JedisPubSub pubsub) {
            JedisPoolConfig config = new JedisPoolConfig();   
            config.setMaxWaitMillis(10 * 1000);  
            config.setMaxIdle(1000);  
            config.setTestOnBorrow(true);  
            pool = new JedisPool(config, ip, port, Protocol.DEFAULT_TIMEOUT, null);
            exit = false;
            this.pubsub = pubsub;
        }
    
        public boolean publish(String channels, String message, String content) {
            JSONObject obj = new JSONObject();
            boolean ret = false;
            Jedis jedis = null;
            try {
                jedis = pool.getResource();
                if (message != null) {
                    obj.put("message", message);
                }
                try {
                    JSONObject objCon = new JSONObject(content);
                    obj.put("content", objCon);
                } catch (JSONException e) {
                    obj.put("content", content);
                }
                String[] tmp = channels.split(";");
                for (String channel : tmp) {
                    try {
                        if (jedis.publish(channel, obj.toString()) > 0) {
                            ret = true;
                        }
                    } catch (Exception e) {
                        break;
                    }
                }
            } catch (JSONException e) {
            } finally {
                if (jedis != null){
                    jedis.close();
                }
            }
    
            return ret;
        }
    
        public boolean clientNotify(String clients, String message, String content, int type) {
            if (type == MSG_REALTIME) {
                return publish(clients, message, content);
            }
    
            boolean ret = false;
    
            try {
                JSONObject obj = new JSONObject();
                obj.put("clients", clients);
                obj.put("type", type);
                if (message != null) {
                    obj.put("message", message);
                }
                try {
                    JSONObject objCon = new JSONObject(content);
                    obj.put("content", objCon);
                } catch (JSONException e) {
                    obj.put("content", content);
                }
    
                if (pool.getResource().publish(NOTIFY_CHANNEL, obj.toString()) > 0) {
                    ret = true;
                }
            } catch (JSONException e) {
            }
    
            return ret;
        }
    
        public boolean setValue(String key, String value) {
            try {
                String response = pool.getResource().set(key, value);
                if (response != null && response.equals("OK")) {
                    return true;
                }
            } catch (JedisConnectionException e) {
                e.printStackTrace();
            }
    
            return false;
        }
    
        public String getValue(String key) {
            return pool.getResource().get(key);
        }
    
        public void subscribe(String... channels) {
            while (!exit) {
                try {
                    pool.getResource().subscribe(pubsub, channels);
                } catch (JedisConnectionException e) {
                    e.printStackTrace();
                    System.out.println("try reconnect");
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        }
    }
    
    
    

    Test测试函数:

    package cn.cicoding.utils;
    
    import redis.clients.jedis.JedisPubSub;
    
    public class Test extends JedisPubSub {
        @Override
    	public void onMessage(String channel, String message) {
        	System.out.println(channel + ":" + message);
    	}
    
    	public static void main(String[] args) {
            MQClient client = new MQClient("127.0.0.1", 6379, new Test());
            client.setValue("abc", "java setted");
            System.out.println(client.getValue("abc"));
            System.out.println(client.clientNotify("nodeSubscriber", "message from java", "{\"debug\":0}", MQClient.MSG_REALTIME));
            client.subscribe("testInitiativePerception");
        }
    }
    
    
    
  • 相关阅读:
    c# base64及MD5工具类
    c# dateTime格式转换为Unix时间戳工具类
    c# 金钱大写转小写工具类
    c# bitmap的拷贝及一个图像工具类
    C# DataTable映射成Entity
    Kubernetes---Service(SVC)服务--ingress api
    Kubernetes---Service(SVC)服务
    为网站文字前面添加图标 在线调用 Font Awesome 字体icon小图标 美化网站
    [转]office 2016 4合1/3合1 专业版 增强版 精简绿色安装版
    [转]数据恢复 文件恢复工具 DiskGenius v4.9.1 绿色专业版及单文件
  • 原文地址:https://www.cnblogs.com/zhaokejin/p/15626252.html
Copyright © 2020-2023  润新知