Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8451001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:07:16+00:00 2026-06-10T11:07:16+00:00

All I have installed latest Redis 2.4.16 and trying to use its Pub/Sub system

  • 0

All

I have installed latest Redis 2.4.16 and trying to use its Pub/Sub system with java. I am putting a message to a channel every second. There is no issue with Publisher but Subscriber crashes with the message

Exception:

redis.clients.jedis.exceptions.JedisDataException: ERR only (P)SUBSCRIBE / (P)UNSUBSCRIBE / QUIT allowed in this context
at redis.clients.jedis.Protocol.processError(Protocol.java:59)
at redis.clients.jedis.Protocol.process(Protocol.java:66)
at redis.clients.jedis.Protocol.read(Protocol.java:131)
at redis.clients.jedis.Connection.getObjectMultiBulkReply(Connection.java:206)
at redis.clients.jedis.JedisPubSub.process(JedisPubSub.java:88)
at redis.clients.jedis.JedisPubSub.proceed(JedisPubSub.java:83)
at redis.clients.jedis.Jedis.subscribe(Jedis.java:1971)
at com.jedis.test.JedisSub$1.run(JedisSub.java:22)
at java.lang.Thread.run(Thread.java:680)

Here is my Code:

Publisher:

        final Jedis jedis = new Jedis("localhost");

        ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(10);
        newFixedThreadPool.submit(new Runnable() {

            @Override
            public void run() {
                while(true){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    jedis.publish("CC", new Date().toString());
                }

            }
        });

Subscriber:

        JedisPool jedisPool = new JedisPool(poolConfig,"localhost", 6379, 100);
        final Jedis subscriberJedis = jedisPool.getResource();


        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    subscriberJedis.subscribe(new JedisPubSub() …..,"CC");
                } catch (Exception e) {
                   e.printStackTrace();
                }
            }
        }).start();

        jedisPool.returnResource(subscriberJedis);

Pool Configuration:

    JedisPoolConfig poolConfig = new JedisPoolConfig();
    poolConfig.maxActive = 10;
    poolConfig.maxIdle = 5;
    poolConfig.minIdle = 1;
    poolConfig.testOnBorrow = true;
    poolConfig.numTestsPerEvictionRun = 10;
    poolConfig.timeBetweenEvictionRunsMillis = 60000;
    poolConfig.maxWait = 3000;
    poolConfig.whenExhaustedAction = org.apache.commons.pool.impl.GenericObjectPool.WHEN_EXHAUSTED_FAIL;

For installing Redis, I simply used command

make PREFIX=/Users/ggg/dev/dist/redis/ install

After this I did not use ./install_server.sh

Jedis version is 2.1.0, platform is Mac OS X.

Note: What I have noticed is subscriber crashes near about 30 seconds
after start.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:07:18+00:00Added an answer on June 10, 2026 at 11:07 am

    Both the code of the publisher and the subscriber are wrong in their own way.

    The error comes from the fact a Redis connection cannot be shared between publishers and subscribers. Actually you need a connection (or a pool of connections) for publishers, and just one dedicated connection for the subscriber thread. Running a single subscriber thread per process is usually enough.

    Here, you return the subscriberJedis connection to the pool too early, before the subscriber thread is complete, so the connection is shared.

    In the publisher:

    Since you have a pool of 10 threads, you should not share a unique connection across these threads. This is the perfect place to use the connection pool, and the connections have to be grabbed and released in each thread.

        // This should be a global singleton
        JedisPool jedisPool = new JedisPool(poolConfig,"localhost", 6379, 100);
    
        ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(10);
        newFixedThreadPool.submit(new Runnable() {
    
            @Override
            public void run() {
                while(true){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Jedis jedis = jedisPool.getResource();
                    try {
                       jedis.publish("CC", new Date().toString());
                    } catch (Exception e) {
                       e.printStackTrace();
                    } finally {
                       jedisPool.returnResource(jedis);
                    }
                }
    
            }
        });
    

    In the subscriber:

    In the subscriber, you need a dedicated connection.

        new Thread(new Runnable() {
            @Override
            public void run() {
                Jedis subscriberJedis = new Jedis("localhost");
                try {
                    subscriberJedis.subscribe(new JedisPubSub() …..,"CC");
                } catch (Exception e) {
                   e.printStackTrace();
                }
            }
        }).start();
    

    If you need to subscribe to different channels or patterns, it is better to set the other subscriptions in the same thread and for the same connection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to run the Pocket-Island game by Wooga and have installed all prerequisites
i have installed the latest netbeans 6.9 rc2 for all languages, but i only
I have just installed the latest version of Java Development Kit from Sun Technologies.
I'm using Windows 7 (all latest updates installed), and currently have DirectX 11 installed
I am trying to use ViewBag in my application, I have all of the
I have installed an HttpModule into my web app that will handle all requests
I have installed MacVim, and https://github.com/skwp/dotfiles (including all the dependencies such as zsh, etc).
All, I have installed xampp for linux on ubuntu 9.10. The installation directory is
I have all the dependancies installed (or I think I do). I definitely have
I have a blog installed in www.foo.com/wp/ and would like all requests that go

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.