[JCBC-135] Using Couchbase inside netty server fails Created: 25/Oct/12  Updated: 26/Mar/13  Resolved: 26/Mar/13

Status: Resolved
Project: Couchbase Java Client
Component/s: Core
Affects Version/s: None
Fix Version/s: 1.1.5
Security Level: Public

Type: Bug Priority: Major
Reporter: Mike Wiederhold Assignee: Michael Nitschinger
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   
See this forum question for a description of the issue as well as the fix for it:

http://www.couchbase.com/forums/thread/using-couchbaseclient-inside-netty-server

 Comments   
Comment by Michael Nitschinger [ 16/Nov/12 ]
http://review.couchbase.com/#/c/22589/
Comment by Michael Nitschinger [ 16/Nov/12 ]
I could reproduce this issue by instantiating a couchbase server inside a handler. If you instantiate the connection outside it works. Here is the code to reproduce it:

when you run it in the IDE and telnet to localhost 8080 it will die and throw the exception:

package com.couchbase.nettyexample;

import com.couchbase.client.CouchbaseClient;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URI;
import java.util.Arrays;
import java.util.concurrent.Executors;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;

/**
 * Hello world!
 *
 */
public class App {
  private final int port = 8080;
  private CouchbaseClient client;

  public void run() throws IOException {
    ServerBootstrap bootstrap = new ServerBootstrap(
      new NioServerSocketChannelFactory(
        Executors.newCachedThreadPool(),
        Executors.newCachedThreadPool()
      )
    );

    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
      public ChannelPipeline getPipeline() throws Exception {
        return Channels.pipeline(new EchoServerHandler());
      }
    });

    bootstrap.bind(new InetSocketAddress(port));
  }

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

  class EchoServerHandler extends SimpleChannelUpstreamHandler {

    private CouchbaseClient client;

    public EchoServerHandler() throws IOException {
      this.client = new CouchbaseClient(
        Arrays.asList(URI.create("http://localhost:8091/pools")),
        "default",
        ""
      );

      this.client.set("received", 0, "foo");
    }

    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
      System.out.println("Received Message");
      System.out.println(client.get("received"));
    }
  }
}
Comment by Michael Nitschinger [ 26/Mar/13 ]
will be available in 1.1.5.
Generated at Sun Jul 13 09:41:13 CDT 2014 using JIRA 5.2.4#845-sha1:c9f4cc41abe72fb236945343a1f485c2c844dac9.