While testing some Groovy code that uses the Redis pub/sub functionality I wrote the script below. This is a simple example written in Groovy, it ships as a single file and loads the required dependencies at runtime via the Groovy Grape dependency management functionality.

				
					@GrabResolver(name='Maven Central', root='http://repo1.maven.org/')
@Grab(group='redis.clients', module='jedis', version='3.5.2')
import redis.clients.jedis.Jedis
import redis.clients.jedis.JedisPubSub

class DefaultPubSubImpl extends JedisPubSub {

  @Override
  public void onMessage(String channel, String message) {
    println "channel: $channel, message: $message"
  }

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

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

  @Override
  public void onUnsubscribe(String channel, int subscribedChannels) {}

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

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

def subscriber = new DefaultPubSubImpl ()

Jedis jedis = new Jedis("localhost", 6379, 0)
jedis.connect();

jedis.subscribe (subscriber, "updates")
				
			

Groovy Script execution using Redis and Jedis along with the output.

In the image below we can see the Groovy script running and printing to the console an update of “Hello world!” which was sent on the updates channel.

On the right-hand side of this image we can also see the “Hello world!” message being sent from the console.

The example Groovy script which relies on Jedis and running in the GroovyConsole with output on a channel which was published from the terminal, which is also shown here.

thospfuller

I am a Technical SEO Consultant and WordPress Specialist based in Northern Virginia. I am interested in software development, content engineering, and business. I'm originally from Chicago, IL, and currently reside in Reston, VA.