zookeeper-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Yang <teddyyyy...@gmail.com>
Subject help with bookkeeper code?
Date Tue, 06 Sep 2011 05:02:24 GMT
I was trying to learn how to use BookKeeper, so I started from the test code
BookieClientTest.java, and stripped it down to the
most basic form (code pasted below),

now I just have the readEntry callback print out onto screen, but it doesn't
seem to be executed. there is not much doc available,
where am I doing wrong?

I setup my separate bookie server as instructed by the README.txt , by
java -cp
$MYLIB:\$\{dist.dir\}/contrib/bookeper/zookeeper-dev-bookkeeper.jar:/home/yyang/work/zookeeper/zookeeper-3.3.3/zookeeper-3.3.3.jar
org.apache.bookkeeper.util.LocalBookKeeper 1


Thanks a lot
Yang


///////////////////////////////////////
package org.apache.bookkeeper.test;

/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

import java.io.File;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.concurrent.Executors;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.socket.ClientSocketChannelFactory;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.junit.Test;
import org.apache.bookkeeper.client.BKException;
import org.apache.bookkeeper.proto.BookieClient;
import org.apache.bookkeeper.proto.BookieServer;
import
org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.ReadEntryCallback;
import
org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.WriteCallback;
import org.apache.bookkeeper.util.OrderedSafeExecutor;
import org.apache.log4j.Logger;

import junit.framework.TestCase;

public class SimpleClientTest extends TestCase {
    static Logger LOG = Logger.getLogger(SimpleClientTest.class);
    BookieServer bs;
    File tmpDir;
    int port = 5000;
    ClientSocketChannelFactory channelFactory;
    OrderedSafeExecutor executor;

    @Override
    protected void setUp() throws Exception {

        channelFactory = new
NioClientSocketChannelFactory(Executors.newCachedThreadPool(), Executors
                .newCachedThreadPool());
        executor = new OrderedSafeExecutor(2);
    }


    ReadEntryCallback recb = new ReadEntryCallback() {

        public void readEntryComplete(int rc, long ledgerId, long entryId,
ChannelBuffer bb, Object ctx) {
            System.out.println("read something" + ledgerId + " " + entryId +
" " + bb);
        }

    };

    WriteCallback wrcb = new WriteCallback() {
        public void writeComplete(int rc, long ledgerId, long entryId,
InetSocketAddress addr, Object ctx) {
            if (ctx != null) {
                synchronized (ctx) {
                    ctx.notifyAll();
                }
            }
        }
    };

    @Test
    public void testWriteGaps() throws Exception {
        byte[] passwd = new byte[20];
        Arrays.fill(passwd, (byte) 'a');
        InetSocketAddress addr = new InetSocketAddress("127.0.0.1", port);

        BookieClient bc = new BookieClient(channelFactory, executor);
        ChannelBuffer bb;
        bb = createByteBuffer(1, 1, 1);
        bc.addEntry(addr, 1, passwd, 1, bb, wrcb, null);
            bc.readEntry(addr, 1, 1, recb, null);

    }

    private ChannelBuffer createByteBuffer(int i, long lid, long eid) {
        ByteBuffer bb;
        bb = ByteBuffer.allocate(4 + 16);
        bb.putLong(lid);
        bb.putLong(eid);
        bb.putInt(i);
        bb.flip();
        return ChannelBuffers.wrappedBuffer(bb);
    }
}

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message