Skip to content

Instantly share code, notes, and snippets.

@ntung
Forked from FredrikWendt/JedisTest.java
Created July 10, 2020 06:00
Show Gist options
  • Save ntung/2cebdd6295249f2dccdb2c3b059cba5e to your computer and use it in GitHub Desktop.
Save ntung/2cebdd6295249f2dccdb2c3b059cba5e to your computer and use it in GitHub Desktop.

Revisions

  1. @FredrikWendt FredrikWendt created this gist Aug 13, 2012.
    116 changes: 116 additions & 0 deletions JedisTest.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,116 @@
    package se.wendt.statoil.mastercard;

    import java.util.ArrayList;
    import java.util.concurrent.CountDownLatch;

    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisPubSub;

    public class JedisTest {

    private static final String JEDIS_SERVER = "a.server.somewhere";

    private ArrayList<String> messageContainer = new ArrayList<String>();

    private CountDownLatch messageReceivedLatch = new CountDownLatch(1);
    private CountDownLatch publishLatch = new CountDownLatch(1);

    public static void main(String[] args) throws InterruptedException {
    new JedisTest().run();
    }

    private void run() throws InterruptedException {
    setupPublisher();
    JedisPubSub jedisPubSub = setupSubscriber();

    // publish away!
    publishLatch.countDown();

    messageReceivedLatch.await();
    log("Got message: %s", messageContainer.iterator().next());

    jedisPubSub.unsubscribe();
    }

    private void setupPublisher() {
    new Thread(new Runnable() {
    @Override
    public void run() {
    try {
    log("Connecting");
    Jedis jedis = new Jedis(JEDIS_SERVER);
    log("Waiting to publish");
    publishLatch.await();
    log("Ready to publish, waiting one sec");
    Thread.sleep(1000);
    log("publishing");
    jedis.publish("test", "This is a message");
    log("published, closing publishing connection");
    jedis.quit();
    log("publishing connection closed");
    } catch (Exception e) {
    log(">>> OH NOES Pub, " + e.getMessage());
    // e.printStackTrace();
    }
    }
    }, "publisherThread").start();
    }

    private JedisPubSub setupSubscriber() {
    final JedisPubSub jedisPubSub = new JedisPubSub() {
    @Override
    public void onUnsubscribe(String channel, int subscribedChannels) {
    log("onUnsubscribe");
    }

    @Override
    public void onSubscribe(String channel, int subscribedChannels) {
    log("onSubscribe");
    }

    @Override
    public void onPUnsubscribe(String pattern, int subscribedChannels) {
    }

    @Override
    public void onPSubscribe(String pattern, int subscribedChannels) {
    }

    @Override
    public void onPMessage(String pattern, String channel, String message) {
    }

    @Override
    public void onMessage(String channel, String message) {
    messageContainer.add(message);
    log("Message received");
    messageReceivedLatch.countDown();
    }
    };
    new Thread(new Runnable() {
    @Override
    public void run() {
    try {
    log("Connecting");
    Jedis jedis = new Jedis(JEDIS_SERVER);
    log("subscribing");
    jedis.subscribe(jedisPubSub, "test");
    log("subscribe returned, closing down");
    jedis.quit();
    } catch (Exception e) {
    log(">>> OH NOES Sub - " + e.getMessage());
    // e.printStackTrace();
    }
    }
    }, "subscriberThread").start();
    return jedisPubSub;
    }

    static final long startMillis = System.currentTimeMillis();

    private static void log(String string, Object... args) {
    long millisSinceStart = System.currentTimeMillis() - startMillis;
    System.out.printf("%20s %6d %s\n", Thread.currentThread().getName(), millisSinceStart,
    String.format(string, args));
    }
    }
    14 changes: 14 additions & 0 deletions stdout
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    subscriberThread 5 Connecting
    publisherThread 1 Connecting
    publisherThread 40 Waiting to publish
    publisherThread 41 Ready to publish, waiting one sec
    subscriberThread 40 subscribing
    subscriberThread 624 onSubscribe
    publisherThread 1042 publishing
    publisherThread 1048 published, closing publishing connection
    subscriberThread 1049 Message received
    main 1050 Got message: This is a message
    publisherThread 1050 >>> OH NOES Pub, It seems like server has closed the connection.
    subscriberThread 1051 onUnsubscribe
    subscriberThread 1052 subscribe returned, closing down
    subscriberThread 1053 >>> OH NOES Sub - It seems like server has closed the connection.