当前位置 : 主页 > 编程语言 > java >

redis 消息发布、订阅及事务

来源:互联网 收集:自由互联 发布时间:2021-06-28
redis message @Test public void testSubstribe() { JedisPubSub jedisPubSub = new JedisPubSub() { @Override public void onMessage(String channel, String message) { System.out.println(channel); System.out.println(message); } }; jedis.subscribe
redis message
@Test
    public void testSubstribe() {
        JedisPubSub jedisPubSub = new JedisPubSub() {
            @Override
            public void onMessage(String channel, String message) {
                System.out.println(channel);
                System.out.println(message);
            }
        };
        jedis.subscribe(jedisPubSub, "channel1");
    }

    //消息发布
    @Test
    public void testPublish() {
        String channel = "channel1";
        String message = "test publish substribe rainhowchan";
        jedis.publish(channel, message);
    }

    //阻塞队列
    @Test
    public  void testBlockedQueue() {
//        while (true) {
            List
 
   brpop = jedis.brpop(0, "queue");
            for (String str : brpop) {
                System.out.println(str);
            }
//        }

    }

    //发送消息
    @Test
    public void testLpush() {
        jedis.lpush("queue", "queue_message1", "queue_message2");
    }

    //通过pipeline一次性发送多条消息
    @Test
    public void testPipeline() throws IOException {
        Pipeline p = jedis.pipelined();
        p.set("fool", "bar");
        p.zadd("foo",1,"barowitch");
        p.zadd("foo", 0, "barinsky");
        p.zadd("foo", 0, "barikoviev");

        p.sync();
        p.close();
        Response
  
    pipeString = p.get("fool"); Response
   
    > sose = p.zrange("foo", 0, -1); // int soseSize = sose.get().size(); Set
    
      setBack =sose.get(); for (String str : setBack) { System.out.println(str); } } //redis事务 @Test public void testTransaction() { // jedis.watch(key1, key2); Transaction t = jedis.multi(); t.set("aa", "bar"); t.exec(); } //redis返回值 @Test public void testBack() { Transaction t = jedis.multi(); t.set("fool", "bar"); Response
     
       result1 = t.get("fool"); t.zadd("foo", 1, "barowitch"); t.zadd("foo", 0, "barinsky"); t.zadd("foo", 0, "barikoviev"); Response
      
       > sose = t.zrange("foo", 0, -1); // get the entire sortedset t.exec(); // dont forget it String foolbar = result1.get(); // use Response.get() to retrieve // things from a Response int soseSize = sose.get().size(); // on sose.get() you can directly }
      
     
    
   
  
 
网友评论